4

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?

2
  • Why add primary key when it's already autoincremented???? Commented Nov 13, 2013 at 5:36
  • I dont need to add primary key,the autoincrement field is not inserted by this code. Commented Nov 13, 2013 at 5:39

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

@peterm - Do you have to add the NULL keyword? (null, ?,?) - My webSQL queries work fine just leaving it out.
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');

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.