英文:
Expo SQLite not inserting values into the table
问题
我使用 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('表格创建成功'),
(error) => console.log('创建表格时出错:', error)
);
});
};
const insertCurrency = (name, sign) => {
db.transaction((tx) => {
tx.executeSql(
'INSERT INTO currencies (name, sign) VALUES (?, ?);',
[name, sign],
() => {
console.log('货币已插入');
},
(error) => {
console.log('插入货币时出错:', error);
}
);
});
};
const getAllEntries = () => {
db.transaction((tx) => {
tx.executeSql(
'SELECT * FROM currencies;',
[],
(_, result) => {
const entries = result.rows._array;
console.log("这些是条目:" + entries);
},
(_, error) => {
console.error(error);
}
);
});
};
useEffect(() => {
createTable();
insertCurrency('US Dollar', '$');
getAllEntries();
})
return (
// 代码的其余部分
)
错误截图已附上。
console_error
插入货币时出错: {"_complete":false,"_error":null,"_running":true,"_runningTimeout":false,"_sqlQueue":{"first":undefined,"last":undefined,"length":0},"_websqlDatabase":{"_currentTask":{"errorCallback":[Function anonymous],"readOnly":false,"successCallback":[Function anonymous],"txnCallback":[Function anonymous]},"_db":{"_closed":false,"_name":"custom.db"},"_running":true,"_txnQueue":{"first":[Object],"last":[Object],"length":1},"closeAsync":[Function bound close],"deleteAsync":[Function bound deleteAsync],"exec":[Function bound exec],"version":"1.0"}}
我不知道这个错误是什么意思,或者应该怎么做。任何帮助将不胜感激。谢谢。
英文:
I am using expo-sqlite to create a table in a database. The database gets created/opened successfully and the table gets created too but when I insert some values into it, there's a strange error in log.
Following is my code
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
)
Screenshot of the error is attached.
console_error
> Error occurred while inserting the currency: {"_complete": false, "_error": null, "_running": true, "_runningTimeout": false, "_sqlQueue": {"first": undefined, "last": undefined, "length": 0}, "_websqlDatabase": {"_currentTask": {"errorCallback": [Function anonymous], "readOnly": false, "successCallback": [Function anonymous], "txnCallback": [Function anonymous]}, "_db": {"_closed": false, "_name": "custom.db"}, "_running": true, "_txnQueue": {"first": [Object], "last": [Object], "length": 1}, "closeAsync": [Function bound close], "deleteAsync": [Function bound deleteAsync], "exec": [Function bound exec], "version": "1.0"}}
I don't know what this error means or what should be done. Any help would be appreciated. Thanks.
答案1
得分: 0
我使用 Promise 来显示解析和拒绝,当我发现实际错误时。错误信息是"没有名为 sign 的列"。然后我使用了一个查询来显示表结构,结果发现 SQLite 将单词 "sign" 转换为 "code",因此我的列被命名为 code。
我删除了表格,创建了一个新的,列名为 (currency_id, currency_name 和 currency_sign),现在它完全正常运行!
希望对某人有所帮助。
英文:
I used Promise to show the resolve and reject where I found the actual error. It was "There is no column named sign". Then I used a query to show me the table structure and as it turned out, SQLite was converting the word "sign" to "code" and my column was being named code.
I deleted the table and made a new one with columns named (currency_id, currency_name, and currency_sign) and it works perfectly fine!
I hope it helps someone.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论