sqflite in android : no such column: name (code 1 SQLITE_ERROR)

Issue

This Content is from Stack Overflow. Question asked by Ahmed Khaled

Hello i’ve created 2 projects one is to do list and is working fine and this one quiz app while trying to update the question i get this error in logcat

    android.database.sqlite.SQLiteException: no such column: name (code 1 SQLITE_ERROR): , while compiling: UPDATE QUESTIONS_TABLE SET Questions=? WHERE  name=?
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1804)
    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1752)
    at com.example.doyouknow.DB.DBHandler.updateCourse(DBHandler.java:83)
    at com.example.doyouknow.EditActivity.onOptionsItemSelected(EditActivity.java:61)
    at android.app.Activity.onMenuItemSelected(Activity.java:4269)
    at androidx.activity.ComponentActivity.onMenuItemSelected(ComponentActivity.java:514)
    at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:352)
    at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:266)
    at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:110)
    at androidx.appcompat.app.AppCompatDelegateImpl.onMenuItemSelected(AppCompatDelegateImpl.java:1189)
    at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:836)
    at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:159)
    at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:987)
    at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:977)
    at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:625)
    at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:156)
    at android.view.View.performClick(View.java:7448)
    at android.view.View.performClickInternal(View.java:7425)
    at android.view.View.access$3600(View.java:810)
    at android.view.View$PerformClick.run(View.java:28305)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

and this is my DBHandler class

public class DBHandler extends SQLiteOpenHelper {

final private static String DB_NAME = "QUESTIONS_LIST.db";
final private static int DB_VERSION = 2;

public static final String TABLE_NAME = "QUESTIONS_TABLE";
public static final String COL_ID = "ID";
public static final String COL_QUESTION = "Questions";
public static final String COL_ANSWER = "Answers";

public static final String QUERY = "CREATE TABLE " + TABLE_NAME + " (" +
        COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COL_QUESTION + " TEXT, " +
        COL_ANSWER + " TEXT) ";

private static int mCurrentPosition;

public DBHandler(Context context) {
    super(context, DB_NAME,null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(QUERY);
}

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

public void addQuestion(String question,String answer) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COL_QUESTION,question);
    values.put(COL_ANSWER,answer);

    db.insert(TABLE_NAME,null,values);

    db.close();
}

public List<QuestionModel> readQuestion() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursorQuestions = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    List<QuestionModel> questionModelList = new ArrayList<>();

    if (cursorQuestions.moveToFirst()) {
        do {
            questionModelList.add(new QuestionModel(cursorQuestions.getString(1),
                    Boolean.parseBoolean(cursorQuestions.getString(2))));
        } while (cursorQuestions.moveToNext());
    }
    cursorQuestions.close();
    return questionModelList;
}

public void updateCourse(String originalQuestion, String newQuestion) {

    // calling a method to get writable database.
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(COL_QUESTION, newQuestion);

    // on below line we are calling a update method to update our database and passing our values.
    // and we are comparing it with name of our course which is stored in original name variable.
    db.update(TABLE_NAME, values,"name=?", new String[] {originalQuestion});

    db.close();
}

public static int getCurrentPosition() {
    return mCurrentPosition;
}

public static void setCurrentPosition(int currentPosition) {
    mCurrentPosition = currentPosition;
}

}

I have tried figuring out by my self and tried to solve it but i failed also i searched and tried all given solution but didnt work also and reinstalled the app and updated the db still the same

this is the function that call the method

    @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.save_edit_action) {
        String newQuestion = mQuestionEditBox.getText().toString();
        mDBHandler.updateCourse(originalQuestion,newQuestion);
        finish();
    }
    return super.onOptionsItemSelected(item);
}



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?