Skip to content

LimeSqlite

  • lime-sqlite 参考 plus.sqlite API 实现的sqlite UTS API,兼容uniappx(安卓, ios)
  • 可以打开plus.sqlite参考

文档

sqlite

安装

插件市场入口 导入即可(ios 如果是win开发需要自定义基座)

代码演示

打开数据库

js
import { openDatabase, DatabaseOptions } from '@/uni_modules/lime-sqlite'

openDatabase({
	name: 'text', // 支持路径,例如 '/static/db/test.db', 鸿蒙暂时没有条件处理。web暂不支持路径
	success(res) {
		console.log('openDatabase', res)
	},
	fail(err) {
		console.log('openDatabase', err)
	}
} as DatabaseOptions)

判断数据库是否打开

js
import { isOpenDatabase, DatabaseOptions } from '@/uni_modules/lime-sqlite'

isOpenDatabase({
	name: 'text',
	success(res) {
		console.log('isOpenDatabase', res)
	},
	fail(err) {
		console.log('isOpenDatabase', err)
	}
} as DatabaseOptions)

关闭数据库

js
import { closeDatabase, DatabaseOptions } from '@/uni_modules/lime-sqlite'

closeDatabase({
	name: 'text',
	success(res) {
		console.log('closeDatabase', res)
	},
	fail(err) {
		console.log('closeDatabase', err)
	}
} as DatabaseOptions)

执行事务

js
import { transaction, SqlTransactionOptions } from '@/uni_modules/lime-sqlite'

transaction({
	name: 'text',
	operation: 'begin',
	success(res) {
		console.log('transaction', res)
	},
	fail(err) {
		console.log('transaction', err)
	}
} as SqlTransactionOptions)

执行增删改等操作的SQL语句

js
import { executeSql, SqlOperationOptions } from '@/uni_modules/lime-sqlite'

executeSql({
	name: 'text',
	sql: [
		"CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER)",
		"INSERT INTO test (name, age) VALUES ('Alice', 30)",
		"INSERT INTO test (name, age) VALUES ('Bob', 25)",
		"INSERT INTO test (name, age) VALUES ('Charlie', 35)"
	],
	success(res) {
		console.log('executeSql', res)
	},
	fail(err) {
		console.log('executeSql', err)
	}
} as SqlOperationOptions)

执行查询的SQL语句

js
import { selectSql, SqlOperationOptions } from '@/uni_modules/lime-sqlite'

selectSql({
	name: 'text',
	sql: ['select * from test'],
	success(res) {
		console.log('selectSql', res)
	},
	fail(err) {
		console.log('selectSql', err)
	}
} as SqlOperationOptions)

WEB 使用

web下使用需要安装sql.js,在终端输入

yarn add sql.js

打赏

如果你觉得本插件,解决了你的问题,赠人玫瑰,手留余香。

源代码

组件源码