Expo SQLite不向表中插入值

x8goxv8g  于 2023-06-06  发布在  SQLite
关注(0)|答案(1)|浏览(151)

我正在使用expo-sqlite在数据库中创建一个表。数据库成功创建/打开,表也成功创建,但当我向其中插入一些值时,日志中出现奇怪的错误。
下面是我的代码

import * as SQLite from 'expo-sqlite';

const Onboarding = ({ navigation }) => {

      const db = SQLite.openDatabase('custom.db');
const createTable = () => {
     ;
  db.transaction((tx) => {
    tx.executeSql(
      'CREATE TABLE IF NOT EXISTS currencies (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, sign TEXT);',
      [],
      () => console.log('Table created successfully'),
      (error) => console.log('Error creating table: ', error)
    );
  });
};

   

   const insertCurrency = (name, sign) => {

      
      db.transaction((tx) => {
         tx.executeSql(
            'INSERT INTO currencies (name, sign) VALUES (?, ?);',
            [name, sign],
            () => {
               console.log('CURRENCY INSERTED');
            },
            (error) => {
               console.log('Error occurred while inserting the currency:', error);
            }
         );
      });
   };

   const getAllEntries = () => {

  db.transaction((tx) => {
    tx.executeSql(
      'SELECT * FROM currencies;',
      [],
      (_, result) => {
        const entries = result.rows._array;
        console.log("These are the entries:" + entries);
      },
      (_, error) => {
        console.error(error);
      }
    );
  });
};

useEffect (()=>{
   createTable();
   insertCurrency('US Dollar', '$');
   getAllEntries();
})
   
return(
//rest of my code here
  )

随附错误的屏幕截图。console_error
插入货币时出错:{"_complete”:false,“_error”:null,“_running”:true,“_runningTimeout”:false,“_sqlQueue”:{“first”:undefined,“last”:undefined,“length”:0},“_websqlDatabase”:{"_currentTask”:{“errorCallback”:[函数anonymous],“readOnly”:false,“successCallback”:[函数anonymous],“txnCallback”:[函数anonymous]},“_db”:{"_closed”:false,“_name”:“custom.db”},“_running”:true,“_txnQueue”:{“first”:[Object],“last”:[Object],“length”:1},“closeAsync”:[函数绑定关闭],“deleteAsync”:[函数绑定deleteAsync],“exec”:[Function bound exec],“version”:“1.0”}}
我不知道这个错误意味着什么,也不知道应该怎么做。任何帮助将不胜感激。谢谢

kpbpu008

kpbpu0081#

INSERT查询的末尾缺少一个分号。

相关问题