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.
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.