没有这样的表 - 在离子应用程序中使用SQLitePlugin

时间:2016-12-20 12:01:50

标签: sqlite ionic-framework cordova-plugins

我是离子的新手,正在处理我需要将数据存储到本地数据库的应用程序。我正在使用SQLite插件(“https://github.com/litehelpers/Cordova-sqlite-storage.git”)。但是在向表中插入值时会出错 - “没有这样的表存在”。代码如下所述。请告诉我这是什么问题。谢谢。

app.js -

app.run(function($ionicPlatform, $cordovaSQLite) {
        $ionicPlatform.ready(function() {



                             if(window.cordova && window.cordova.plugins.Keyboard) {

                             cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                             cordova.plugins.Keyboard.disableScroll(true);
                             }
                             if(window.StatusBar) {
                             StatusBar.styleDefault();
                             }

                             db = window.sqlitePlugin.openDatabase({name:"nextflow.db", location:'default'});

                            $cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS Registration (id INTEGER PRIMARY KEY AUTOINCREMENT, baseUrl TEXT, loginId TEXT, id TEXT)');

                             });
        });

controller.js -

$cordovaSQLite.execute(db, 'INSERT INTO Registration (baseUrl) VALUES (?)', [baseurl])
                                                     .then(function(result) {
                                                           console.log("Message saved successful, cheers!");


                                                           }, function(error) {
                                                           alert("Error on     saving: " + error.message);
                                                           });

1 个答案:

答案 0 :(得分:0)

尝试将app.js代码更改为以下内容。我相信你的语法可能不正确。另一种可能性是,在调用INSERT之前,您的CREATE没有完成执行。

app.run(function($ionicPlatform, $cordovaSQLite) {
    $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }

        db = window.sqlitePlugin.openDatabase({name:"nextflow.db", location:'default'});

        db.transaction(function(tx) {
            tx.execute('CREATE TABLE IF NOT EXISTS Registration (id INTEGER PRIMARY KEY AUTOINCREMENT, baseUrl TEXT, loginId TEXT, id TEXT)');
        }, function(error) {
            console.log('Transaction ERROR: ' + error.message);
        }, function() {
            console.log('Created table OK');
        });

    });
});

将你的controler.js改为下面,

db.transaction(function(tx) {
            tx.execute('INSERT INTO Registration (baseUrl) VALUES (?)', [baseurl]);
        }, function(error) {
            console.log('Error on     saving: ' + error.message);
        }, function() {
            console.log('Message saved successful, cheers!');
        });

确保可以从代码的两个部分访问db变量。通常情况下,我会让它变得更加全球化。

<强> - 编辑 -

当您尝试使用tx.execute执行SQL时,必须将执行调用包装在db.transatcion函数中。如下所示,

db.transaction(function(tx) {
    tx.execute('CREATE TABLE IF NOT EXISTS ProfileDetail (id INTEGER PRIMARY KEY AUTOINCREMENT, baseUrl TEXT, loginId TEXT, id TEXT)')
}, function(error) {
    console.log('Transaction ERROR: ' + error.message);
}, function() {
    console.log('Created table OK');
});

另外,我通常在程序的一个区域中一起完成所有表创建。以下代码将在一个事务中创建两个表,

db.transaction(function(tx) {
    tx.execute('CREATE TABLE IF NOT EXISTS Registration (id INTEGER PRIMARY KEY AUTOINCREMENT, baseUrl TEXT, loginId TEXT, id TEXT)');
    tx.execute('CREATE TABLE IF NOT EXISTS ProfileDetail (id INTEGER PRIMARY KEY AUTOINCREMENT, baseUrl TEXT, loginId TEXT, id TEXT)')
}, function(error) {
    console.log('Transaction ERROR: ' + error.message);
}, function() {
    console.log('Created tables OK');
});