Mayank Jindal
Mayank Jindal

Reputation: 35

android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling:

I have created an SQLite Helper class to handle my database.

I am constantly getting this error while running my code but I am not able to find anything wrong in my Create Table syntax.

ERROR: android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling:CREATE TABLE table (ID INTEGER PRIMARY KEY AUTOINCREMENT, ACCOUNT_HOLDER TEXT, DEBIT INTEGER, CREDIT INTEGER, BALANCE INTEGER)

    package com.example.user.balancesheet;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;

    public class DatabaseHelper extends SQLiteOpenHelper{
    private static final int DB_Version=1;
    private static final String Database="BalanceSheet.db";
    public static String Table="table";

    public DatabaseHelper(Context context) {
            super(context, Database, null, DB_Version);
            SQLiteDatabase db=this.getWritableDatabase();
        }

        @Override
        public void onCreate(SQLiteDatabase db) {       
             String query="CREATE TABLE " + Table +" (ID INTEGER PRIMARY KEY 
    AUTOINCREMENT, ACCOUNT_HOLDER TEXT, DEBIT INTEGER, CREDIT INTEGER, 
    BALANCE INTEGER)";
            db.execSQL(query);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int 
    newVersion) {
                    db.execSQL("DROP IF EXIST " + Table);
            onCreate(db);
        }

        public Boolean insertdata(String name, int deb, int cred, int bal){
            ContentValues val=new ContentValues();
            val.put("ACCOUNT_HOLDER", name);
            val.put("DEBIT", deb);
            val.put("CREDIT_HOLDER", cred);
            val.put("BALANCE", bal);
            SQLiteDatabase db=this.getWritableDatabase();
            long result=db.insert(Table, null, val);
            Log.d("METHOD","ADDLEDGER");
            if(result==-1)
                return false;
            else
                return true;
         }
    }

Upvotes: 0

Views: 1439

Answers (1)

Renzo
Renzo

Reputation: 27434

The reason is the use of the word table as table name.

Table is a keyword so you cannot use it as identifier.

See the documentation.

Upvotes: 3

Related Questions