Websql Insert Query

Gopesh picture Gopesh · Nov 13, 2013 · Viewed 13.5k times · Source

I am new websql.i cannot insert primary key in websql.

Create Query

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);

    });

Insert Query

 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");
        }
    );

This code doesnot add the autoincrement value ,Any ideas? Is there any good method for doing this other than what i do?

Answer

peterm picture peterm · Nov 13, 2013

Your INSERT statement is incorrect. You should either include names of columns other then id

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

or pass NULL for id

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

or just

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

Here is SQLFiddle demo