0

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.

2
  • Could an earlier version of the table tasks exist without the column content? Commented Jul 2, 2020 at 22:49
  • nope, that is all the code, I'm gonna try the wrapper Commented Jul 3, 2020 at 17:21

1 Answer 1

1

Unfortunately, db.transaction does not work like most of programmers would expect it to work - it does not support passing async function or function that returns a promise or anything like that and tx.executeSql just "Enqueues a SQL statement to execute in the transaction".

Related links with some info on subject: https://github.com/expo/expo/issues/1889, https://github.com/expo/expo/issues/3726, https://forums.expo.io/t/are-expo-sqlite-transactions-sync-or-async/7934/5

In order to utilize transactions, you can use db.exec with begin transaction/commit/rollback SQL statements. You need separate connection for transaction. I use my own wrapper for that

Sign up to request clarification or add additional context in comments.

1 Comment

i have some issue with sqlite, i create the insert fetch delete functions and for some reason it doesn't work for me. can you help me please ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.