Created a table in sqlite but could not insert data

Here I created a table called account. But when I try to insert data into it ,it shows an error

import React, { Component } from 'react'
import { AsyncStorage, Text, View, TextInput, StyleSheet, Button } from 'react-native'
import Expo, { SQLite } from 'expo';
const db = SQLite.openDatabase('wallet');
class AsyncStorageExample extends Component {
   state = {
      'balance': 0,
      'value':0,
      'sourceName' :'',
   }
   componentDidMount() {
    db.transaction(tx => {
      tx.executeSql(
        'create table if not exists account (id integer primary key not null, name text, balance int);',[],()=>console.log("creeeated"),(a,b)=>console.log(b)
      );
      console.log("created table account ");
      tx.executeSql(
        'create table if not exists transaction (id integer primary key not null, relatedAccount_id int, type int, amount int, balanceAfterTransaction int, dateTime text, description text, FOREIGN KEY(relatedAccount_id) REFERENCES account(id) );'
      );
    });
  }
   source = (name) =>{
     console.log('hca');
     console.log(name);
     this.setState({ 'sourceName' : name});
   } 
   
     
   addSource = ()=>{
    console.log("entered addedSource");
    db.transaction(
      tx => {
        console.log(tx.executeSql('insert into account (name , balance) values (?,0)',[this.state.sourceName],()=>console.log("sucess"),(a,b)=>console.log(b)));
        console.log("inserted new account"+this.state.sourceName+"fin");
        tx.executeSql('select * from account', [], (_, { rows }) =>
          console.log(JSON.stringify(rows)),(a,b)=>console.log(b)
        );
        console.log("exit");
      }
    )
   };
   render() {
      return (
         <View style = {styles.container}>
            <TextInput style = {styles.textInput} autoCapitalize = 'none' placeholder = 'sourceName'
               onChangeText = { this.source}/>
            <Button title="Add Source" onPress = {this.addSource}/>
         </View>
      )
   }
}
export default AsyncStorageExample

const styles = StyleSheet.create ({
   container: {
      flex: 1,
      alignItems: 'center',
      marginTop: 50
   },
   textInput: {
      margin: 15,
      height: 35,
      borderWidth: 1,
      backgroundColor: '#7685ed'
   }
})

When I try to insert data into account it shows error ( no such table: account (code 1):slight_smile:

1 Like

executeSql() enqueues some sql for execution; it does not synchronously execute it. So I recommend removing this line:

console.log("created table account ");

Because it doesn’t relate to whether the table was actually created. Instead, you should debug the success/error callbacks to the create table statement. Right now you provided this:

()=>console.log("creeeated"),
(a,b)=>console.log(b)

Are you sure “creeeated” is being logged? That callback also provides a ResultSet object, have you inspected the value of that object?

1 Like

Hi ben thanks for looking into this. I figured out the problem. The problem was with the word transaction. If I changed the table name to transaxtion it is working fine. Do you know why this happens?

I would guess it’s because transaction is a reserved word in this flavor of sql.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.