0

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;
         }
    }
1

1 Answer 1

3

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.

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

2 Comments

Thank You. I replaced it by 'Tablename'. But it is still showing the same error.
Did you change it as public static String Table="tablename"? tablename is not a keyword, so it should work. Try something like public static String Table="table1".

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.