Cordova的SQLite插件:代码反向运行 [英] SQLite plugin for Cordova: Code running backwards

查看:206
本文介绍了Cordova的SQLite插件:代码反向运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的使用Cordova和移动开发一般,但我有一个非常奇怪的行为在我的代码。我使用SQLite插件ngCordova(我使用Ionic),我想做的是非常简单:如果一个表存在,然后删除或创建,如果它不存在。

I'm new using Cordova and Mobile development in general, but I have a really strange behavior in my code. I'm using the SQLite plugin with ngCordova (I'm using Ionic) and what I want to do is very simple: If a table exist, then drop it or create if it doesn't exists.

我为数据库操作创建了一个服务(我不知道是否是最好的方法,但它保持那种逻辑与控制器分离)。

I've created a service for the database operations (I don't know if is the best way to do it, but it keeps that kind of logic separated from controllers).

逻辑是:

app.js

angular.module('starter', ['ionic', 'ngCordova', 'starter.controllers', 'starter.services'])
.run(function($ionicPlatform, $ionicLoading, $timeout, initialService) {
    $ionicPlatform.ready(function() {
        if (!initialService.hasUsers()) { // If there's no table called Usuarios
            initialService.createDefaultUser(); // Create table and a record
        } else {
            $ionicLoading.show({
                template: 'Restableciendo...'
            });
            initialService.resetDatabase(); // Droping table

            $timeout(function() {
                $ionicLoading.hide();
            }, 3000);
      }
  });
})

services.js b
$ b

services.js

angular.module('starter.services', ['ionic', 'ngCordova']).service('initialService', function($cordovaSQLite, $ionicPopup) {
    return {

        // Check if Usuarios table exists
        hasUsers: function() {
            if (ionic.Platform.isAndroid()) {
                db = $cordovaSQLite.openDB({
                    name: "com.pos.db",
                    iosDatabaseLocation: 'default'
                });
            } else {
                db = $cordovaSQLite.openDB({
                    name: "com.pos.db",
                    location: 2,
                    createFromLocation: 1
                });
            }

            var returnValue;
            db.transaction(
                function(tx) {
                    tx.executeSql("SELECT name FROM sqlite_master WHERE type='table' AND name='Usuarios'", [], function(tx, result) {
                        console.log('Existen ' + result.rows.length + ' tablas con el nombre Usuarios');
                        returnValue = (result.rows.length) ? true : false;
                    });
                }
            );
            return returnValue;

        },

        // Creates the Usuarios table and a testing record
        createDefaultUser: function() {
            var returnValue;
            console.log("creando tabla de usuarios");
            if (ionic.Platform.isAndroid()) {
                db = $cordovaSQLite.openDB({
                    name: "com.pos.db",
                    iosDatabaseLocation: 'default'
                });
            } else {
                db = $cordovaSQLite.openDB({
                    name: "com.pos.db",
                    location: 2,
                    createFromLocation: 1
                });
            }

            db.sqlBatch([
                'DROP TABLE IF EXISTS Usuarios',
                'CREATE TABLE Usuarios (idUsuario INTEGER PRIMARY KEY AUTOINCREMENT, usuario TEXT NOT NULL, tipoUsuario NUMERIC NOT NULL DEFAULT 0, password TEXT)',
                "INSERT INTO Usuarios (idUsuario,usuario,tipoUsuario,password) VALUES (1,'mike',0,'123');",
            ], function() {
                returnValue = true;
            }, function(error) {
                returnValue = false;
            });
            return returnValue;
        },

        // Drops the table
        resetDatabase: function() {
            var returnValue = false;
            console.log("Eliminando tabla de usuarios");
            db.transaction(
                function(tx) {
                    tx.executeSql("DROP TABLE IF EXISTS Usuarios", [], function(tx, result) {
                        returnValue = true;
                    });
                }
            );
            return returnValue;
        }
    };
});

我使用我的手机和chrome控制台进行调试,代码顺序不是与执行顺序相同:

I'm debugging with my cellphone and the chrome console and the order of the code isn't the same as the execution order:

< img src =https://i.stack.imgur.com/Ui67z.pngalt =错误>

如何确定

推荐答案

我想你的项目嵌入jquery。

I suppose your project embeds jquery.

如果你想链接查询的执行,你必须使用$ .deferred()(和promise())(读取它首先了解基本) 。

You have to use the $.deferred() (and promise()) if you want to chain the execution of the queries as you want it (read about it first to understand the basic).

然后我建议你使用JS对象方法。

Then I suggest you to use the JS object approach.

JS对象方法和$ .deferred适用于WebSQL(非常类似于SQLite)显示为此链接中问题的答案

这篇关于Cordova的SQLite插件:代码反向运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆