storesafe / cordova-sqlite-storage

A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API
Other
2.15k stars 714 forks source link

SQLITE Prepare statement error [INSERT] #310

Closed subu1979 closed 8 years ago

subu1979 commented 9 years ago

When i'am trying to insert into the database, iam getting following error

I/chromium( 1475): [INFO:CONSOLE(167)] "database already open: msfa.db", source: file:///android_asset/www/plugins/io.litehelpers.cordova.sqlite/www/SQLitePlugin.js (167) I/chromium( 1475): [INFO:CONSOLE(79)] "DB opened: msfa.db", source: file:///android_asset/www/plugins/io.litehelpers.cordova.sqlite/www/SQLitePlugin.js (79) V/sqlg ( 1475): prepare db 0xa1264088 sql BEGIN V/sqlg ( 1475): sqlc_st_finish 0xa121e6e8 V/sqlg ( 1475): prepare db 0xa1264088 sql INSERT INTO leads_master(orgname,contact,mobile,email,requirement,brand) VALUES ('C2il ','Subbu' , '7299003593', 'Subramaniyam.k@c2il.com ','Demo','CRM'); W/System.err( 1475): java.sql.SQLException: prepare statement failed with error: 1 W/System.err( 1475): at io.liteglue.SQLiteGlueConnection.prepareStatement(SQLiteGlueConnection.java:45) W/System.err( 1475): at io.liteglue.SQLitePlugin$SQLiteDatabaseNDK.executeSqlStatementNDK(SQLitePlugin.java:493) W/System.err( 1475): at io.liteglue.SQLitePlugin$SQLiteDatabaseNDK.executeSqlBatch(SQLitePlugin.java:434) W/System.err( 1475): at io.liteglue.SQLitePlugin$DBRunner.run(SQLitePlugin.java:618) W/System.err( 1475): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) W/System.err( 1475): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) W/System.err( 1475): at java.lang.Thread.run(Thread.java:818) V/executeSqlBatch( 1475): SQLitePlugin.executeSql[Batch](): Error=prepare statement failed with error: 1 V/sqlg ( 1475): prepare db 0xa1264088 sql ROLLBACK V/sqlg ( 1475): sqlc_st_finish 0xa121e6e8

brodycj commented 9 years ago

SQLite error code 1 was returned from the C-interface function call, which means there was an error with the SQL statement (ref: https://www.sqlite.org/c3ref/c_abort.html). The sql statement looks OK by itself, so I suspect either the table was not created or the fields did not match. But I cannot say for sure without looking at your program.

I really need to see the code you used to open the database, create the table, and add the record to help you any further. You can either post it or send it to: info@litehelpers.net

brodycj commented 9 years ago

A common cause is the case where a table has certain NOT NULL field(s) that are forgotten in the INSERT statement. This should be reflected in error.message that is returned in the error callback, needs to be tested.

SP1966 commented 9 years ago

I was experiencing the same error and after pulling out my last remaining hair follicles I found the issue was the SQL to create the table was wrong, I was trying to use "CREATE TABLE IF NOT EXIST..." rather than "EXISTS".

Leaving this here in case somebody else has the same issue.

brodycj commented 8 years ago

Thanks @SP1966.

inceptiveKshitij commented 8 years ago

Hello @brodybits, I created the database successfully by following your given. Also i saw the database as well as table in my chrome browser resource tab. Then i try to insert some data in my created table, but it give me an error which is "Could not prepare statement, 1 column not found." Please refer below code `if (testing == "browser") { db = window.openDatabase("myfriend.db", "1.0", "myfriend Database", -1); } else { db = window.sqlitePlugin.openDatabase({ name: "myfriend.db", location: 'default' }); } db.transaction(function(tx) { tx.executeSql("INSERT INTO friends (first_name, last_name, city, mobile_No) VALUES (?,?,?,?)", [$scope.registerUser.firstname, $scope.registerUser.lastname, $scope.registerUser.city, $scope.registerUser.mobNo], function(tx, res) { console.log("INSERT ID -> " + res.insertId); $location.path("templates/playlists");

        }, function(err) {
            console.log(err);

        });

    });` 

Please help me for this issue. Thanks....

brodycj commented 8 years ago

@inceptiveKshitij it looks like either the table does not exist or you are using an incorrect column name.

DipakMahapurkar commented 8 years ago

Please check your table creation: tx.executeSql('CREATE TABLE IF NOT EXISTS friends(id integer primary key autoincrement, first_name text, last_name text, emailAddress text, city text, mobile_No text)'); replace it with tx.executeSql('CREATE TABLE IF NOT EXISTS friends(id integer primary key autoincrement NOT NULL, first_name text, last_name text, emailAddress text, city text, mobile_No text)');