Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.database.sqlite.SQLiteException: near " ": syntax error

I'm having a problem while adding records in SQLite.

This is the Error:

09-18 17:47:47.586: E/AndroidRuntime(1039):
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.capstone.pinoygoodies/com.capstone.pinoygoodies.GroceryView}:
   android.database.sqlite.SQLiteException: near " ":
   syntax error:
   CREATE TABLE tblItem 
                 (_id INTEGER PRIMARY KEY AUTOINCREMENT,
                  grocery_item TEXT NOT NULL,
                  grocery_qty TEXT NOT NULL 

My CREATE TABLE

db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
KEY_ITEMID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ITEM + " TEXT NOT NULL, " + 
KEY_QTY + " TEXT NOT NULL ");

Whenever I hit the add button, this error is being triggered.

like image 247
secre.swallowtail Avatar asked Nov 30 '25 15:11

secre.swallowtail


2 Answers

db.execSQL("
    CREATE TABLE " + DATABASE_TABLE + "(" +
    KEY_ITEMID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
    KEY_ITEM + " TEXT NOT NULL, " + 
    KEY_QTY + " TEXT NOT NULL );"
);

There are easy to mix up. You just need the close parenthesis.

Suggestion:

String createStatement = 
    String.format("CREATE TABLE %s ( %s INTEGER PRIMARY KEY
                   AUTOINCREMENT, %s TEXT NOT NULL, 
                   %s TEXT NOT NULL);",
                        DATABASE_TABLE,
                        KEY_ITEMID,
                        KEY_ITEM,
                        KEY_QTY);

If you construct your table like this, I personally think it makes the statement much easier to read for things like SQL Syntax and then you can bind the data later.

like image 116
Greg Giacovelli Avatar answered Dec 03 '25 09:12

Greg Giacovelli


Append ")" at the end of Create Table Query.

like image 43
jeet Avatar answered Dec 03 '25 10:12

jeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!