-1

I have just recently started working with SQlite database and have ran into a problem. Whenever I try to update or delete data, the app crashes and displays and error "no such column: s_id". I have tried to fix it for days but have had no luck. I would appreciate if someone could tell me what the problem is and why it is occurring. Thank you.

Here is my database:

public class DatabaseHelper extends SQLiteOpenHelper{

public static final String DATABASE_NAME = "sqlite.db";
public static final int VERSION = 1;

public static final String TABLE_NAME = "tbl_reminder";
public static final String S_ID = "s_id";
public static final String S_TITLE = "s_title";
public static final String S_DATE = "s_date";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            S_ID +" TEXT)";
    db.execSQL(CREATE_TABLE);
}

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

public void insertData (String s_title){
    ContentValues contentValues = new ContentValues();
    contentValues.put(S_TITLE, s_title);

    SQLiteDatabase sqLiteDB = this.getWritableDatabase();
    sqLiteDB.insert(TABLE_NAME, null, contentValues);
    sqLiteDB.close();
}

public ArrayList<ViewHolderHelper> getAllData() {
    ArrayList<ViewHolderHelper> list = new ArrayList<>();
    String SQL = "SELECT * FROM " + TABLE_NAME;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(SQL, null);
    if (cursor.moveToFirst()) {
        do {
            ViewHolderHelper vhh = new ViewHolderHelper();
            vhh.setiD(cursor.getInt(0) + "");
            vhh.setTitle(cursor.getString(1));
            list.add(vhh);
        } while (cursor.moveToNext());
    }
    return list;
}

public void updateData(int id, String title){
    ContentValues contentValues = new ContentValues();
    contentValues.put(S_TITLE, title);

    SQLiteDatabase sqliteDB = this.getWritableDatabase();
    sqliteDB.update(TABLE_NAME, contentValues, S_ID + "=" + id, null);
    sqliteDB.close();
}

public void deleteData(int id){
    SQLiteDatabase sqliteDB = this.getWritableDatabase();
    sqliteDB.delete(TABLE_NAME, S_ID + "=" + id, null);
    sqliteDB.close();
}
}

Here is also the error that is displayed:

FATAL EXCEPTION: main

    Process: json.google_services.newreminderapp, PID: 23419                                                                              
android.database.sqlite.SQLiteException: no such column: s_id (code 1): , while compiling: UPDATE tbl_reminder SET s_title=? WHERE s_id=1
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ItsMeElvis
  • 21
  • 5
  • Seems like your table id is named ID and your textfield is named s_id. And you have no primary key. – user1086500 Aug 20 '17 at 12:36
  • Right, I looked back at the code and Im still unsure how to fix this. I might be able to add a primary key, but I dont know how to go about changing the id's. Thanks. – ItsMeElvis Aug 20 '17 at 12:52
  • there is a PK, its "ID" its just not being searched for. – AnthonyK Aug 20 '17 at 13:28
  • Mind the missing space here: `db.execSQL("DROP_TABLE" + TABLE_NAME);` And remove the extra `_` from DROP_TABLE. 2 blocking errors in a single line... oh, my!! – Phantômaxx Aug 20 '17 at 13:28
  • I removed the ' _ ', but I'm not sure what missing space you are reffering to? – ItsMeElvis Aug 20 '17 at 14:54

3 Answers3

0

Delete

public void deletUser(String name) {
    String selection = NewUserInfo.USER_NAME + " LIKE ?";
    String[] selection_args = {name};

    getWritableDatabase().delete(NewUserInfo.TABLE_NAME, selection, selection_args);
}

Update

public int updateInformation(String oldName, String newName, String newAge, String newMobile, int newGender) {
    String selection = NewUserInfo.USER_NAME + " LIKE ?";
    String[] selection_args = {oldName};

    ContentValues contentValues = new ContentValues();
    contentValues.put(NewUserInfo.USER_NAME, newName);
    contentValues.put(NewUserInfo.USER_AGE, newAge);
    contentValues.put(NewUserInfo.USER_MOB, newMobile);
    contentValues.put(NewUserInfo.USER_GENDER, newGender);

    int count = getWritableDatabase().update(NewUserInfo.TABLE_NAME, contentValues, selection, selection_args);
    return count;
}
Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
0
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("+S_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
        S_ID +" TEXT)";
db.execSQL(CREATE_TABLE);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Chirag Gohil
  • 352
  • 1
  • 2
  • 14
  • Thank you for your input. I have made the changes you recommended, but the issue still occures. You think there might be a problem with the 'updateData' or 'deleteData' also? – ItsMeElvis Aug 20 '17 at 14:16
0

The problem is in your createTable, you defined ID as the primary key and s_id as a seperate column. then later in your insert you never gave any data to you s_id column, so therefore your column never truely exists.

To corrrect this you should use s_id as the auto incremented primary key.

Also you insert S_TITLE and I don't see that column in your onCreate aswell.

Based on the rest of the class i believe your CREATE statement should be:

"CREATE TABLE " +TABLE_NAME + " ( " + S_ID + " INTEGER AUTOINCREMENT PRIMARY KEY + ", " + S_TITLE + " TEXT"

AnthonyK
  • 473
  • 2
  • 11
  • Thank you for your input. I have made the changes you recommended, but the issue still occures. You think there might be a problem with the 'updateData' or 'deleteData' also? – ItsMeElvis Aug 20 '17 at 14:16
  • did you make sure to delete the old table, or increase the version number? – AnthonyK Aug 20 '17 at 14:20
  • I replaced the onCreate content with what you provided and no, i did not change the version number. – ItsMeElvis Aug 20 '17 at 14:53
  • The onCreate is only called is the db doesn't exist. It was created on first run, it wont create again till onUpgrade or explicitly classed. – AnthonyK Aug 20 '17 at 17:00
  • Okay, I've got onUpdate included, but I still don't know how to fix the issue Im very new to coding, if it isn't obvious haha – ItsMeElvis Aug 20 '17 at 21:31
  • Look at the post this is a duplicate of. – AnthonyK Aug 20 '17 at 21:32