4

我是新 websql。我无法在 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");
        }
    );

此代码不添加自动增量值,有什么想法吗?除了我所做的之外,还有什么好的方法可以做到这一点吗?

4

2 回答 2

6

你的INSERT说法不正确。您应该包括其他列的名称id

INSERT INTO vehicles (vehicle_ref_no, vehicle_plate_no) VALUES (?, ?);

或将 NULL 传递给id

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

要不就

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

这是SQLFiddle演示

于 2013-11-13T06:08:50.097 回答
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');

    }
}
于 2017-07-19T07:23:48.770 回答