0

我正在使用 phonegap 访问电话数据库。但是我的创建过程每次都会调用带有错误代码的错误例程:5 和消息:non an error

难道不能在一个事务中执行多个sql语句吗?

SQLite Expert 找不到语法错误...

createDB: function(error, success) {
        if(typeof error != 'function') error = this.errorDB;
        if(typeof success != 'function') success = this.successDB;
        sql = "DROP TABLE IF EXISTS `boiler`; "
            +"CREATE TABLE IF NOT EXISTS `boiler` ( "
            +" `id` int NOT NULL, `object` int NOT NULL, `number` varchar(100) NOT NULL, "
            +" `volume` double DEFAULT NULL, `brand` varchar(100) DEFAULT NULL, `year` year(4) DEFAULT NULL, "
            +" `price_before` float NOT NULL DEFAULT '0', `price_after` float NOT NULL DEFAULT '0',  `description` TEXT DEFAULT NULL, "
            +" `img1` varchar(200) DEFAULT NULL, `img2` varchar(200) DEFAULT NULL, `img3` varchar(200) DEFAULT NULL, "
            +" `img4` varchar(200) DEFAULT NULL, `img5` varchar(200) DEFAULT NULL,  `img6` varchar(200) DEFAULT NULL, "
            +" `img7` varchar(200) DEFAULT NULL, `img8` varchar(200) DEFAULT NULL, `img9` varchar(200) DEFAULT NULL, "
            +"PRIMARY KEY (`id`)); "

            +"DROP TABLE IF EXISTS `counter`; "
            +"CREATE TABLE IF NOT EXISTS `counter` ( "
            +" `number` varchar(100) NOT NULL, `object` int NOT NULL, `type` tinyint NOT NULL DEFAULT '0', "
            +" `value` double DEFAULT NULL, `access` varchar(100) DEFAULT NULL, "
            +"PRIMARY KEY (`number`)); "

            +"DROP TABLE IF EXISTS `link`; "
            +"CREATE TABLE IF NOT EXISTS `link` ( "
            +" `id` int NOT NULL, `boiler` int NOT NULL, `name` varchar(100) DEFAULT NULL, "
            +" `units` tinyint DEFAULT NULL, `price` float NOT NULL DEFAULT '0', "
            +"PRIMARY KEY (`id`)); "

            +"DROP TABLE IF EXISTS `manager`; "
            +"CREATE TABLE IF NOT EXISTS `manager` ( "
            +" `id` int NOT NULL, `company` varchar(100) DEFAULT NULL, `name` varchar(100) NOT NULL, "
            +" `phone` varchar(15) DEFAULT NULL, "
            +"PRIMARY KEY (`id`)); "

            +"DROP TABLE IF EXISTS `object`; "
            +"CREATE TABLE IF NOT EXISTS `object` ( "
            +" `id` int NOT NULL,  `state` tinyint NOT NULL DEFAULT '0', `user` varchar(50) DEFAULT NULL, "
            +" `date` char(15) DEFAULT NULL,  `street` varchar(100) DEFAULT NULL, `number` varchar(16) DEFAULT NULL, "
            +" `zip` char(5) DEFAULT NULL, `city` varchar(100) DEFAULT NULL, `manager` int NOT NULL DEFAULT '0', "
            +" `units` int NOT NULL DEFAULT '0', "
            +"PRIMARY KEY (`id`));";
        console.log(sql);
        this.DB.transaction(function (tx) { tx.executeSql(sql); }, error, success);
    },
4

2 回答 2

1

不知道错误码:5但可以执行多条sql语句

         // Wait for PhoneGap to load

            document.addEventListener("deviceready", onDeviceReady, false);

            // PhoneGap is ready

            function onDeviceReady() {
                var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
                db.transaction(populateDB, errorCB, successCB);
            }

            // Populate the database 

            function populateDB(tx) {
                 tx.executeSql('DROP TABLE IF EXISTS DEMO');
                 tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
                 tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
                 tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
        //here you can do  multiple sql statements.
            }

            // Transaction error callback

            function errorCB(err) {
                alert("Error processing SQL: "+err);
            }

            // Transaction success callback

            function successCB() {
                alert("success!");
            }

链接在这里

于 2012-06-05T10:00:32.440 回答
0

请从文件资源管理器检查您的数据库

仅在创建表时存在问题,例如您正在输入四个值,但在您的表中只有三列,因此会出现此错误

tx.executeSql("CREATE TABLE IF NOT EXISTS quotes(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,name,flag INTEGER,address,description,phone_number,date INTEGER,quote_number,job,images)");

错误是由于如果您直接分配整数值而不是 var 类型值,例如我使用 1 或 0 的 var 类型值,但现在我将此值存储在 var 类型中

于 2012-12-24T06:14:18.990 回答