Unable to insert data db table using expo-sqlite

Please provide the following:

  1. SDK Version: 8.2.1
  2. Platforms(Android/iOS/web/all): Android
  3. Add the appropriate “Tag” based on what Expo library you have a question on. expo-sqlite

I have these codes that i wrote to insert or create a new record in a table using

expo-sqlite

but it is not working.

This is what I have done so far

componentDidMount.

I used this to create the table if it doesn’t exist

componentDidMount() {
    db.transaction((tx) => {
      tx.executeSql(
        'CREATE TABLE IF NOT EXISTS Student (studentId INTEGER PRIMARY KEY AUTOINCREMENT, firstname VARCHAR, lastname VARCHAR, middlename VARCHAR, phone VARCHAR, sex VARCHAR, matricNo VARCHAR, email VARCHAR, password VARCHAR, course VARCHAR)'
      );
    });
  }

Then I have this that should save the student record in the table.

Note: I have to hand-coded the values for testing purposes and the alert shpws that nothing is inserted into the Student table

saveStudent = () => {
    db.transaction((tx) => {
      'INSERT INTO Student (firstname, lastname, middlename, phoneNo, email, course, password, sex, matric) VALUES (?, ?, ?, ?, ?, ?, ?, ? , ?)',
        [
          'firstname',
          'lastname',
          'middlename',
          'phoneNo',
          'email',
          'course',
          'password',
          'sex',
          'matric',
        ];

      tx.executeSql('select * from Student', [], (_, { rows }) =>
        alert(JSON.stringify(rows)) // Alert shows that nothing is inserted into the database table
      );
    });
  };

P.S. Expo-sqlite version used is

version 8.2.1

and the expo snack version used is

version 38.0.0

I think you’re missing the executeSql for the insert statement. It should be something like this

saveStudent = () => {
    db.transaction((tx) => {
       tx.executeSql('INSERT INTO Student (firstname, lastname, middlename, phoneNo, email, course, password, sex, matric) VALUES (?, ?, ?, ?, ?, ?, ?, ? , ?)',
        [
          'firstname',
          'lastname',
          'middlename',
          'phoneNo',
          'email',
          'course',
          'password',
          'sex',
          'matric',
        ]);

      tx.executeSql('select * from Student', [], (_, { rows }) =>
        alert(JSON.stringify(rows)) // Alert shows that nothing is inserted into the database table
      );
    });
  };

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