So i was reading up on the android developer page and they talked about a method by which you could execute a query using the rawQuery() or execSql(). I haven't seen this particular method on a lot of other tutorial sites.
I tried to implement the same
Heres the code:
Creating:
SQLiteDatabase db1 = openOrCreateDatabase( "Station.db", SQLiteDatabase.CREATE_IF_NECESSARY , null);
try{
String query = "CREATE TABLE IF NOT EXISTS Station ("
+ "Station_name VARCHAR);";
db1.execSQL(query);
Toast.makeText(MainActivity.this, "Table created", Toast.LENGTH_LONG).show();
for(i=0;i<10;i++)
{
Toast.makeText(MainActivity.this, stations[i][0],Toast.LENGTH_SHORT).show();
query="INSERT INTO Station (Station_name) VALUES('"+stations[i][0]+"');";
Toast.makeText(MainActivity.this, query,Toast.LENGTH_LONG).show();
db1.execSQL(query);
}
}catch (Exception e){
Toast.makeText(MainActivity.this, "An Error has occured", Toast.LENGTH_LONG).show();
}
Retrieving:
SQLiteDatabase db = openOrCreateDatabase("Station.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
setContentView(R.layout.activity_list_view);
Cursor data_fetch = db.rawQuery("Select * From Station", null);
String[] station_array = new String[data_fetch.getCount()];
int i = 0;
while (data_fetch.moveToNext()) {
String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name"));
station_array[i] = name;
i++;
}
ArrayAdapter station_adapter = new ArrayAdapter<String>(this, R.layout.activity_list_view, station_array);
ListView station_listView = (ListView) findViewById(R.id.station_list);
station_listView.setAdapter(station_adapter);
}
While the creation code works the one to retrieve does not. Any idea why?