2

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.

2
  • 1
    Could post the code where you are creating the table? Commented Sep 19, 2012 at 4:45
  • you are missing the ) at the end. You need to include it too Commented Sep 19, 2012 at 5:12

5 Answers 5

7
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.

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

Comments

3

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

Comments

1

error is throw by create table, not get data.

check your create table sql statement

Comments

0

You are missing the right parenthesis of the Create Table Syntax, correct it as follows,

CREATE TABLE tblItem 
                 (_id INTEGER PRIMARY KEY AUTOINCREMENT,
                  grocery_item TEXT NOT NULL,
                  grocery_qty TEXT NOT NULL ); 

Comments

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

You need to add ")" at last in your create table.

Insert Query

    String qty = "QTY";
    String item = "ITEM";
    String sql = "INSERT or replace INTO "+ DATABASE_TABLE +" (qty, item) VALUES('"+ qty +"','"+ item + "')";
    db.execSQL(sql);

1 Comment

This works fine with me but I'm having new problem now. android.database.sqlite.SQLiteException: table tblItem has no column named qty: , while compiling: INSERT INTO tblItem(qty, item) VALUES(?, ?); I have declared the column for qty.

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.