我是 Ionic 的新手,正在尝试构建具有离线支持的移动应用程序。我正在尝试使用cordova sqlite
插件创建数据库。我可以在模拟器的data\data\package\data.db
. 无法执行任何插入语句。
import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
dbHandle : SQLiteObject;
constructor(public navCtrl: NavController, private sqlite: SQLite, public plt: Platform) {
this.plt.ready().then((readySource) => {
console.log('Platform ready from', readySource);
// Platform now ready, execute any required native code
this.handleDbCreation();
});
}
handleDbCreation() {
this.sqlite.create({
name: 'data.db',
location: 'default'
}).then((db: SQLiteObject) => {
this.dbHandle = db;
db.executeSql('create table danceMoves(name VARCHAR(32))', {})
.then(() => {console.log('Executed SQL')
this.insertDataIntoDB();
})
.catch(e => console.log(e));
}).catch(e => console.log(e));
}
insertDataIntoDB(){
this.dbHandle.executeSql("insert into danceMoves values(?)",['testing'])
.then((resultSet)=>{
console.log('resultSet.insertId: ' + resultSet.insertId);
console.log('resultSet.rowsAffected: ' + resultSet.rowsAffected);
})
.catch((error)=>{
console.log('SELECT error: ' + error.message);
});
}
}
我收到下面提到的错误。
D/SystemWebChromeClient(11475):file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js:第179行:打开数据库:data.db-OK [INFO:CONSOLE(179)]"打开数据库:data.db - OK”,来源:file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js (179) java.sql.SQLException: sqlite3_step 失败:无法回滚 - 否事务在 io.sqlc.SQLiteConnectorDatabase.executeSQLiteStatement(SQLiteConnectorDatabase.java:214) 在 io.sqlc.SQLiteConnectorDatabase.executeSqlBatch(SQLiteConnectorDatabase.java:114) 的 io.liteGlue.SQLiteGlueConnection$SQLGStatement.step(SQLiteGlueConnection.java:135) 处于活动状态在 io.sqlc.SQLitePlugin$DBRunner.run(SQLitePlugin.java:340) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 在 java.lang.Thread.run(Thread.java:841) SQLitePlugin.executeSqlBatch:错误=sqlite3_step 失败:无法回滚 - 没有事务处于活动状态 java.sql.SQLException: sqlite3_step 失败:无法回滚 - io.sqlc.SQLiteConnectorDatabase.executeSQLiteStatement(SQLiteConnectorDatabase.java:214) 的 io.liteGlue.SQLiteGlueConnection$SQLGStatement.step(SQLiteGlueConnection.java:135) 上没有事务处于活动状态在 io.sqlc.SQLiteConnectorDatabase.executeSqlBatch(SQLiteConnectorDatabase.java:114) 在 io.sqlc.SQLitePlugin$DBRunner.run(SQLitePlugin.java:340) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在java.util.concurrent.ThreadPoolExecutor$Worker。在 java.lang.Thread.run(Thread.java:841) SQLitePlugin.executeSqlBatch 运行(ThreadPoolExecutor.java:587):SQL 错误代码 = 1 消息 = sqlite3_step 失败:无法回滚 - 没有事务处于活动状态