sqlite 在我的Reaction本机中设置的SQL Lite存储

kx5bkwkv  于 2022-11-14  发布在  SQLite
关注(0)|答案(1)|浏览(176)

我很难设置这个SQL Lite存储。我已经做了好几个星期了。使用EXPO并更改为正常的原生React。还是有问题。this is when i try to console.log the opendatabase
当尝试类似于“db.Transaction(tx=>console.log(Tx))”时,它返回tx,但当我尝试此代码db.action(tx=>{tx.ecuteSql(‘SELECTFROM USERS’,[],(tx,Results)=>{console.log(‘here’)}})*时,没有任何React。

5n0oy7gb

5n0oy7gb1#

请按照下面给出的示例代码一步一步地操作,也分享一些更多的参考:打开数据库:

import SQLite from 'react-native-sqlite-storage';
const db = SQLite.openDatabase(
{
 name: ‘exampleDB',
 location: 'default',
 },
 (success: any) => { console.log(success) },
 (error: any) => { console.log(error) }
);

创建表

const CreateUserTable = () => {

db.transaction((tx: any) => {

tx.executeSql(

 "CREATE TABLE IF NOT EXISTS "

 + "user "

 + "(id INTEGER PRIMARY KEY, email VARCHAR, username VARCHAR, display_name VARCHAR, last_login VARCHAR, role_id INTEGER);"

 )

  })

 };


插入数据

const StoreUserDataToTable = async (data) => {

try {

   await db.transaction(async (tx) => {

   await tx.executeSql(

   "INSERT INTO user (id, email, username, display_name, last_login, role_id) VALUES (?,?,?,?,?,?)",

  [data.id, data.email, data.username, data.display_name, data.last_login, data.role_id]

  );

});

} catch (error) {

 console.log(" StoreUserDataToTable ==>> ", error);

  }

 }

获取数据:

db.transaction(tx => {

tx.executeSql(

 "SELECT * FROM user",

 [],

 (tx, results) => {

 console.log(" results ==>> ", results);

    },

   );

 });

欲了解更多详情,请访问以下链接:https://infinitbility.com/react-native-sqlite-storage-examples-of-query/#create-table-query

相关问题