我以前也遇到过这个问题,但最后我可以让它工作。以下是我为其他需要完整方法的人做的参考:
- 我在这里下载了数据库浏览器来创建和管理我的 sqlite 数据库设计。
- 如果此路径上不存在,则创建一个新文件夹
root\android\app\src\main\assets\www
并将创建的数据库放在www
文件夹中。
- 在项目中,我为 sqlite 目的创建了一个单独的类/文件。
样品名称sqlite-db.js
:
import { openDatabase, deleteDatabase } from 'react-native-sqlite-storage';
var db = openDatabase(
{ name: 'sqlite.db', createFromLocation: 1 },
() => {
console.log('Connection success!');
},
error => {
console.log(error);
},
);
/// Below function is use in-case the db object is null
async function OpenDatabase() {
db = openDatabase(
{ name: 'sqlite.db', createFromLocation: 1 },
() => {
console.log('Re-open connection success!');
},
error => {
console.log(error);
},
);
}
function SQLSelect(query, params = []) {
return new Promise(async (resolve, reject) => {
if (db === null) {
await OpenDatabase();
}
db.transaction(function (txn) {
console.log('SQLITE SELECT QUERY:', query, ',Value:', params);
txn.executeSql(query, params,
function (tx, res) {
var temp = [];
if (res.rows.length == 0) {
resolve(temp);
}
for (let i = 0; i < res.rows.length; ++i) temp.push(res.rows.item(i));
console.log('SQLITE SELECT RESULT:', temp)
resolve(temp);
},
function (error) {
console.log('Failed to select:', error);
reject();
},
);
});
});
}
function SQLInsert(query, params = []) {
return new Promise(async (resolve, reject) => {
if (db === null) {
await OpenDatabase();
}
db.transaction(function (txn) {
console.log('SQLITE INSERT QUERY:', query, ',Value:', params);
txn.executeSql(query, params,
function (tx, res) {
console.log('SQLITE INSERT RESULT:', res)
resolve(res);
},
function (error) {
console.log('Failed to insert:', error);
reject();
},
);
});
});
}
function SQLUpdate(query, params = []) {
return new Promise(async (resolve, reject) => {
if (db === null) {
await OpenDatabase();
}
await db.transaction(function (txn) {
console.log('SQLITE UPDATE QUERY:', query, ',Value:', params);
txn.executeSql(query, params,
function (tx, res) {
console.log('SQLITE UPDATE RESULT:', res);
if (res.rowsAffected > 0) {
// Data updated
resolve(res.rowsAffected);
} else {
// Data not updated
resolve(0);
}
},
function (error) {
console.log('Failed to update:', error);
reject();
},
);
});
});
}
function SQLDelete(query, params = []) {
return new Promise(async (resolve, reject) => {
if (db === null) {
await OpenDatabase();
}
await db.transaction(function (txn) {
console.log('SQLITE DELETE QUERY:', query, ',Value:', params);
txn.executeSql(query, params,
function (tx, res) {
console.log('SQLITE DELETE RESULT:', res)
if (res.rowsAffected > 0) {
// Data deleted
resolve(res.rowsAffected);
} else {
// Data not delete
reject();
}
},
function (error) {
console.log('Failed to delete:', error);
reject();
},
);
});
});
}
async function GetMeSomethingOnDB(data) {
return new Promise(async (resolve, reject) => {
try {
var SQData = {
"TABLERESULT": []
}
let params = [];
let queries = '';
params = [data.TableID];
queries = `SELECT * FROM [TABLE] WHERE TableID=?`;
await SQLSelect(queries, params).then(results => SQData.TABLERESULT = results).catch((error) => console.log('Error retrieve TABLERESULT. ', error));
resolve(SQData);
} catch (error) {
console.log('ERROR GET SOMETHING:', error);
reject();
}
});
}
你怎么能调用函数sqlite-db.js
?示例如下: import * as ApxSQLite from "./sqlite-db";
React.useEffect(() => {
//Get data directly to function
let params = [];
let queries = '';
// You can add the parameter in the query or use ? and
// add the parameter ordinance in params array.
queries = 'SELECT * FROM [OTHERTABLE] WHERE OtherID=1';
ApxSQLite.SQLSelect(queries, params)
.then((data) => {
console.log('Mounted:', data);
})
.catch((error) => console.log('OTHERTABLE ERROR:', error));
// Call the function in sqlite-db if you do not want to
// flood your screen script with all the things like ApxSQLite.SQLSelect code.
var data = {TableID : 13}
ApxSQLite.GetMeSomethingOnDB(data)
.then((data) => {
console.log('Mounted 2:', data);
})
.catch((error) => console.log('GETSOMETHING ERROR:', error));
}, []);
顺便说一句,我没有添加SQLite.DEBUG(false);
和SQLite.enablePromise(false);