0

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?

Android Developer Page For Sqlite

neuhaus
  • 3,886
  • 1
  • 10
  • 27
Arun Mani
  • 73
  • 8
  • Please expand what "does not work" means for you here – laalto Oct 11 '17 at 12:34
  • When Ever i call the activity the app just crashes i Just get the "unfortunately this app has stopped" – Arun Mani Oct 11 '17 at 12:35
  • Then there's exception stacktrace in logcat that tells what went wrong and where. https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Oct 11 '17 at 12:36
  • Let Me look that up thank you! – Arun Mani Oct 11 '17 at 12:43
  • Please post the stack trace log. Also it is not clear where these code is called. If they are not inside an activity or fragment, calling `Toast.makeText()` can produce runtime exception. – Akram Oct 11 '17 at 12:44
  • @ArunMani If you want relief from creating dbhelper classes and defining columns and create table statements, I strongly suggest to use `GreenDao` library: http://greenrobot.org/greendao/documentation/introduction/ , http://greenrobot.org/greendao/documentation/ – Akram Oct 11 '17 at 13:02

1 Answers1

0

I think you problem is the method you are using expects a string path to a database file. It may expect a absolute file path to the .db file (I checked the javaDoc and it doesn't mention), and simply passing a name doesn't work because it's doesn't know where that file could be across the devices entire storage. What's you reasoning for not wanting to use a DB Helper class like in the tutorial? It's the standard pattern using SQL databases on Android. Using this pattern is very simple and clear, and may help you when viewing code exmaples on SQL on Android because many tutorials use the same pattern. For example, if you wanted to offer a content provider for your app, the Android tutorial uses the same scheme of using a DBHelper object. Also the DB Helper exposes a method for allowing easy DB upgrades which can get very tedious when doing it one's self. Again, everything you wanna achieve is possible using the SQLiteDatabase directly, just not the common pattern.

cincy_anddeveloper
  • 1,140
  • 1
  • 9
  • 19
  • Ill try adding the absolute path and as to why i don't use classes, I find that classes are too complex to understand at my level right now i can easly comprehend this as of now – Arun Mani Oct 11 '17 at 12:53
  • Are you new to Java or OOP? That is the exact reason you should use the helper. It Also, is also a class with some complex interactions going on underneath. it is as per the api documentation, "A helper class to manage database creation and version management." It will fix the problem you're facing. – cincy_anddeveloper Oct 11 '17 at 12:56