Websql插入查询

时间:2013-11-13 05:28:16

标签: javascript sql database html5 web-sql

我是新的websql.i无法在websql中插入主键。

创建查询

db.transaction(function (tx) {
        var sql =
         "CREATE TABLE vehicles ( "+
        "id integer primary key autoincrement,"+
        "vehicle_ref_no VARCHAR(255),"+
        "vehicle_plate_no VARCHAR(255),"+
        )";

        tx.executeSql(sql);

    });

插入查询

 db.transaction(
        function (tx) {
            tx.executeSql(
                "INSERT INTO vehicles VALUES (?, ?)",
                 [data.vehicle_ref_no, 
                 data.vehicle_plate_no, 

                 ],
                function (tx, result) {
                    console.log("Query Success");
                },
                function (tx, error) {
                    console.log("Query Error: " + error.message);
                }
            );
        },
        function (error) {

            console.log("Transaction Error: " + error.message);
        },
        function () {
            console.log("Transaction Success");
        }
    );

此代码不添加自动增量值,任何想法? 除了我以外,还有什么好方法吗?

2 个答案:

答案 0 :(得分:5)

您的INSERT声明不正确。您应该包含id

之外的列名称
INSERT INTO vehicles (vehicle_ref_no, vehicle_plate_no) VALUES (?, ?);

或为id

传递NULL
INSERT INTO vehicles (id, vehicle_ref_no, vehicle_plate_no) VALUES (NULL, ?, ?);

或只是

INSERT INTO vehicles VALUES (NULL, ?, ?);

这是 SQLFiddle 演示

答案 1 :(得分:1)

 public insertData(vehicle_details) {
    let insert_query = "INSERT INTO vehicles VALUES (?, ?)";
    this.db.executeSql(insert_query, [vehicle_details.vehicle_ref_no, vehicle_details.vehicle_plate_no]).then((data) => {
        console.log(' records inserted');
    }), (error) => {
        console.log('ERROR occurred in inserting records');

    }
}