Search code examples
androidsqliteandroid-sqlite

SQLite not working with some table names?


I am learning to make Android apps using Java so sorry if this question comes out as noob.

So, I made a ListView and linked an SQLite database to it.

When I open the app, the list view loads.

Now, if my table names are TITLE, LOCATION and DESCRIPTION, the all crashes at startup saying the TITLE and LOCATION tables do not exist.

But if my table names are NAME, ABC and DESCRIPTION, the app loads, even though those tables do not exist.

SQLiteDatabaseHandler.java:

public class SQLiteDatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "remindersdb";
private static final String TABLE_NAME = "reminders";
private static final String KEY_ID = "ID";
private static final String KEY_NAME = "TITLE";
private static final String KEY_LOC = "LOCATION";
private static final String KEY_DESCRIPTION = "DESCRIPTION";
private static final String[] COLUMNS = {KEY_ID, KEY_NAME, KEY_LOC, KEY_DESCRIPTION};

public SQLiteDatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    //db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,ABC TEXT,DESCRIPTION TEXT)");
db.execSQL("create table reminders (ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, LOCATION TEXT, DESCRIPTION TEXT)");

}


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


public void insertData(String name, String location1, String description) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cValues = new ContentValues();
    cValues.put(KEY_NAME, name);
    cValues.put(KEY_LOC, location1);
    cValues.put(KEY_DESCRIPTION, description);
    long newRowId = db.insert(TABLE_NAME, null, cValues);
    db.close();

    /*SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_NAME,name);
    contentValues.put(KEY_POSITION,position);
    contentValues.put(KEY_DESCRIPTION,description);
    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;*/
}


public boolean updateData(String id, String name, String location1, String description) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_ID, id);
    contentValues.put(KEY_NAME, name);
    contentValues.put(KEY_LOC, location1);
    contentValues.put(KEY_DESCRIPTION, description);
    db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id});
    return true;
}



public ArrayList<HashMap<String, String>> getData() {
    SQLiteDatabase db = this.getWritableDatabase();
    ArrayList<HashMap<String, String>> userList = new ArrayList<>();
    String query = "SELECT TITLE, LOCATION, DESCRIPTION FROM " + TABLE_NAME;
    Cursor cursor = db.rawQuery(query, null);
    while (cursor.moveToNext()) {
        HashMap<String, String> user = new HashMap<>();
        user.put("name", cursor.getString(cursor.getColumnIndex(KEY_NAME)));
        user.put("location1", cursor.getString(cursor.getColumnIndex(KEY_LOC)));
        userList.add(user);
    }
    return userList;
}



/*public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
    return res;
}*/


public void deleteData(String id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME, KEY_ID + " = ?", new String[]{String.valueOf(id)});
    db.close();

}

MainActivity.java:

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SQLiteDatabaseHandler db = new SQLiteDatabaseHandler(this);

    ArrayList<HashMap<String, String>> userList = db.getData();


    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.content_main, R.id.textview1 , mobileArray);

    ListView lv = (ListView) findViewById(R.id.listView1);

    ListAdapter adapter = new SimpleAdapter(MainActivity.this, userList, R.layout.list_row,new String[]{"TITLE","LOCATION","DESCRIPTION"}, new int[]{R.id.name, R.id.designation, R.id.location});lv.setAdapter(adapter);





    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();*/
            Intent intent = new Intent(MainActivity.this, CreateActivity.class);
            startActivity(intent);


        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

I snipped some basic code due to formatting issues, like import statements.


Solution

  • You mean column names and not table names.
    Probably when you first created your db the columnn names where NAME, ABC and now you want to change them to TITLE, LOCATION by simply changing these values in SQLiteDatabaseHandler.
    Well it is not so easy.
    Uninstall your app from the emulator/device where you test it so the db is deleted and then rerun the modified code. This way the db will recreate the tables with the new names.
    You see the onCreate() method of SQLiteDatabaseHandleris not triggered every time you run the app, but only if there is no db or there is an upgrade of the version of the db.