I was testing SQLite in React Native, I'm using expo, so the package I'm using is expo-sqlite, so here is my code, is very simple because is for testing:
import React from 'react'
import { StyleSheet, Text, View } from 'react-native'
import * as SQLite from 'expo-sqlite'
const db = SQLite.openDatabase('test')
export default function App() {
testDB()
return (
<View style={styles.container}>
<Text>Wello Horld!</Text>
</View>
)
}
async function testDB() {
await db.transaction(async tx => {
console.log('on transaction')
await tx.executeSql(
'create table if not exists tasks (id integer primary key autoincrement, content text );',
[],
(tx, result) => console.log('result on create: ', result),
(tx, err) => console.log('error on create:', err)
)
await tx.executeSql(
'insert into tasks (content) values (?)',
['testing'],
(tx, result) => console.log('result on insert: ', result),
(tx, err) => console.log('error on insert: ', err)
)
await tx.executeSql(
'select * from tasks',
[],
(tx, result) => console.log('result on select: ', result),
(tx, err) => console.log('error on select: ', error)
)
})
}
the output:
result on create: WebSQLResultSet {
"insertId": 0,
"rows": WebSQLRows {
"_array": Array [],
"length": 0,
},
"rowsAffected": 0,
}
error on insert: [Error: table tasks has no column named content (code 1 SQLITE_ERROR): ,
while compiling: insert into tasks (content) values (?)]
result on select: WebSQLResultSet {
"insertId": undefined,
"rows": WebSQLRows {
"_array": Array [],
"length": 0,
},
"rowsAffected": 0,
}
Why the column is not being created when the first executeSQL method is called? I've watched some examples and the way of creating the table is always the same.
tasksexist without the columncontent?