首页 > 解决方案 > React Native DB Sqlite Transaction Not Working

问题描述

I am staring out with React Native. There are two transactions I am trying to perform using the react-native-sqlite-storage package. These are literally copy/pasted (of course after understanding and to ensure no syntax error from my end) from a website as I am learning. The first one is to create a table and second one is to insert a row of data. The creation is working fine and I don't know why the data insertion, there is nothing happening (console log doesn't happen, no alert, no error). Here are the two:

    db.transaction(function (txn) {
      txn.executeSql(
        "SELECT name FROM sqlite_master WHERE type='table' AND name='Student_Table'",
        [],
        function (tx, res) {
          console.log('item:', res.rows.length);
          if (res.rows.length == 0) {
            txn.executeSql('DROP TABLE IF EXISTS Student_Table', []);
            txn.executeSql(
              'CREATE TABLE IF NOT EXISTS Student_Table(student_id INTEGER PRIMARY KEY AUTOINCREMENT, student_name VARCHAR(30), student_phone INT(15), student_address VARCHAR(255))',
              []
            );
          }
        }
      );
    })
    Alert.alert('SQLite Database and Table Successfully Created...');
  };

Here is my second transaction:

const insertData = () => {

    db.transaction(function (tx) {
      tx.executeSql(
        'INSERT INTO Student_Table (student_name, student_phone, student_address) VALUES (?,?,?)',
        ['ajay',1234567890 , 'bengaluru'],
        (tx, results) => {
          console.log('Results', results.rowsAffected);
          if (results.rowsAffected > 0) {
            Alert.alert('Data Inserted Successfully....');
          } else Alert.alert('Failed....');
        }
      );
    });
  }

Can someone please guide me?

标签: react-nativesqlite

解决方案


推荐阅读