react-native - 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?
解决方案
推荐阅读
- graphql - 基于许多内容引用使用 Gatsby 动态创建页面
- reactjs - react redux 中的调度类型使用
- java - 使用连接池的多个数据库连接与使用多个语句的单个连接
- algorithm - 谁能解释 CodeChef 中的隐藏序列问题?
- android - Jetpack Compose 视图中的提前返回会因“java.lang.IllegalStateException:开始/结束不平衡”而崩溃?
- python - 如何使用 OOP 编程修复方法中的错误
- php - 试图在 PHP 中获取 rethinkdb 实例的状态
- php - PHP reg 表达式检测字符串中的变量
- python - Python 内存分析
- python - 使用 pd.read_sql_query 方法时出现“内存错误”