angularjs - 语句字符串中“?”的数量与参数计数不匹配

标签 angularjs sqlite cordova ionic-framework web-sql

我正在尝试使用WebSQL与我的API进行同步,但是出现以下错误:

语句字符串中'?'的数量与参数计数不匹配。

我正在关注this教程

我不知道发生了什么,我在这里想念什么?

function createTable() {
            db.transaction(
                function (tx) {

                    tx.executeSql('CREATE TABLE IF NOT EXISTS COUNTRY (id, countryName, countryCode)');
                    tx.executeSql('CREATE TABLE IF NOT EXISTS LOCATION (id, locationName)');
                    tx.executeSql("CREATE TABLE IF NOT EXISTS clientes (" +
                    "clientes_id Integer PRIMARY KEY AUTOINCREMENT, " +
                    "_criado Text, " +
                    "_modificado Text, " +
                    "_status Text, " +
                    "id_rm Integer, " +
                    "credencial_id Integer, " +
                    "informacao_adicional, " +
                    "nome, " +
                    "tipo, " +
                    "CONSTRAINT unique_clientes_id UNIQUE ('clientes_id'))");
                    tx.executeSql('CREATE INDEX IF NOT EXISTS "clientes.index_clientes_id" ON "clientes"("clientes_id");');



                },
                txErrorHandler,
                function () {
                    log('clientes table created successfully');
                }
            );
        }
function getLastSync() {
            db.transaction(
                function (tx) {
                    var sql = "SELECT MAX(_modificado) as lastS FROM clientes";


                    tx.executeSql(sql,
                        function (tx, results) {


                            var lastSync = results.rows.item(0).lastS;
                            console.log(lastSync);

                        }
                    );

                },
                txErrorHandler,
                function () {
                    log('error');
                }
            );
        }

        function getChanges(syncURL, modifiedSince) {


            ServiceClientes.getAll(syncURL, modifiedSince).success(function (data) {
                log("The server returned " + data.length + " changes that occurred after " + modifiedSince);
            }).error(function (error) {
                console.log(error);
            });


        }

        function applyChanges() {

            angular.forEach(data.dados, function (item) {
                db.transaction(
                    function (tx) {
                        log(item.nome + " : " + item.tipo_pessoa);

                        tx.executeSql('INSERT OR REPLACE INTO clientes (nome, tipo, ' +
                        '_criado,' +
                        '_modificado , ' +
                        '_status, ' +
                        'id_rm, ' +
                        'informacao_adicional ) VALUES (?,?,?,?,?,?,?)',
                        [item.nome, item.tipo_pessoa, item.criado, item.modificado, item.status, item.id, item.informacoes_adicionais]);
                    },
                    txErrorHandler,
                    function (tx) {
                    }
                );
            });
        }

        function sync(syncURL) {


            getLastSync(function (lastSync) {
                getChanges(syncURL, lastSync, function (changes) {
                        if (changes.length > 0) {
                            applyChanges(changes);
                        } else {
                            console.log('Nothing to synchronize');
                        }

                    }
                );
            });

        }

最佳答案

您应该利用executeSql的第二个参数将数据传递到参数化的SQL查询中,而不是将它们连接在一起。串联是一种不好的做法,并且其中一个字段中可能带有引号或问号,这可能会导致您描述的错误。

因此,使用类似:

tx.executeSql('INSERT OR REPLACE INTO clientes (nome, tipo, ' +
              '_criado,' +
              '_modificado , ' +
              '_status, ' +
              'id_rm, ' +
              'informacao_adicional ) VALUES (?,?,?,?,?,?,?)',
       [item.nome, item.tipo_pessoa, item.criado, item.modificado, item.status, item.id, item.informacoes_adicionais]);

关于angularjs - 语句字符串中“?”的数量与参数计数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28649574/

相关文章:

javascript - AngularFire 检查是否存在相同标题的项目

javascript - Cordova如何旋转相机中的图片

c# - sqlite, mono, C# 跨平台

ios - InAppBrowser 在带有 cordova 3.3.0 的 ios6 上不显示工具栏

android - 未知的 Chrome 错误 : 0

javascript - 避免在嵌套 ng-repeat 中进行索引包装以进行路由

javascript - Angular : Access scope from within <script> tag

javascript - AngularJS ng-repeat 删除对象中重复键的问题

java - CursorIndexOutOfBoundsException : Index 1 requested,,大小为 1。错误 - Android

android - 如何将 double 和 float 值插入 sqlite?