Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Error in my android project "no such column in table"

I’m trying to select from my database certain row by column "ID_TABLITSY" by using method "getid" but I get an error "no such column in mytable".

Database class code:

public class dbelper extends SQLiteOpenHelper{

private static final String DATABASE_NAME = "orginizer.db";

private static final int SCHEMA = 2;
static final String TABLE = "mytable";
static final String TABLE_DOM = "table_dom";
static final String TABLE_RAB = "table_rab";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_NOTE = "note";
public static final String COLUMN_DATE = "date";
public static final String ID_TABLITSY = "idtable";

public dbelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA);
}

@Override
public void onCreate(SQLiteDatabase db) {
    Log.d(LOG_TAG, "--- onCreate database ---");

    db.execSQL("create table mytable(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NOTE + " TEXT, " + COLUMN_DATE + " TEXT, " + ID_TABLITSY + " INTEGER);");
    db.execSQL("create table table_dom(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NOTE + " TEXT, " + COLUMN_DATE + " TEXT);");
    db.execSQL("create table table_rab(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NOTE + " TEXT, " + COLUMN_DATE + " TEXT);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE);
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_DOM);
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_RAB);

    onCreate(db);

}
 public Cursor getid(int ID_TABLITS){
    SQLiteDatabase db = this.getReadableDatabase();
    String sqlQuery = "select * from mytable where ID_TABLITSY = " + ID_TABLITS + ";";
    return db.rawQuery(sqlQuery, null);
 }

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and here the way i use this method in the other class

    @Override
    public void onResume() {
    Cursor cursor = dbHelper.getid(1);
    if (cursor.moveToFirst()) {

        super.onResume();
        db = dbHelper.getReadableDatabase();

        userCursor = db.rawQuery("select * from " + dbelper.TABLE, null);
        String[] headers = new String[]{dbelper.COLUMN_NOTE, dbelper.COLUMN_DATE, dbelper.ID_TABLITSY};
        userAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, userCursor, headers, new int[]{android.R.id.text1, android.R.id.text2}, 0);

        listView.setAdapter(userAdapter);
    }
    cursor.close();
}

Error code says that there is no such column but i definitely :

    E/SQLiteLog: (1) no such column: ID_TABLITSY
    D/AndroidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.kp_orginizer, PID: 24010
    java.lang.RuntimeException: Unable to resume activity 
    {com.example.kp_orginizer/com.example.kp_orginizer.MainActivity}: 
    android.database.sqlite.SQLiteException: no such column: ID_TABLITSY (code 1 
    SQLITE_ERROR): , while compiling: select * from mytable where ID_TABLITSY = 1;
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4205)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237)
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: android.database.sqlite.SQLiteException: no such column: ID_TABLITSY (code 1 SQLITE_ERROR): , while compiling: select * from mytable where ID_TABLITSY = 1;
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1443)
    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1382)
    at com.example.kp_orginizer.dbelper.getid(dbelper.java:76)
    at com.example.kp_orginizer.MainActivity.onResume(MainActivity.java:250)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1446)
    at android.app.Activity.performResume(Activity.java:7939)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4195)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4237) 
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

>Solution :

ID_TABLITSY is the name of the variable containing the column’s name, not the name of the column:

String sqlQuery = 
    "select * from mytable where " + ID_TABLITSY + " = " + ID_TABLITS + ";";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading