-2

Im making an app, and want to port it to android which im new to. Where should i place the db? and is the code below enough to successfully connect and query the db or do i need more to just test if it works?

 String path = "WHERE_SHOULD_I_PLACE_MY_DB_?";

SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openDatabase(path, null,     
SQLiteDatabase.OPEN_READONLY);

            Cursor cursor = sqLiteDatabase.rawQuery("select * from table", null);

            String something = cursor.getString(1);

            System.out.println(something);
vasper
  • 11
  • 6

2 Answers2

0

I've never done something similar, but if I understood your question that you have a db, I would recommend you to put it in the assets folder of your android project and then move it to the internal sd card of the application. There is usually a /app_internal_sd_card/databases/some_db.db where they are stored, but since you're not creating it yourself you can't put it there straight away.

Potentially you can read directly from the assets, but I would cal that a bad practice. :)

Some info on reading from assets: read file from assets or How to copy files from 'assets' folder to sdcard?

Community
  • 1
  • 1
  • Okay so tried to just create the tables from scratch, and this is so messy compared to sqlite in plain java. I will get back to this later when i figure out how i can even insert something and query it back. this was so painfree in java :P – vasper Jul 21 '16 at 01:43
  • But if you create the tables from scratch you don't need to move it around then just follow a tutorial on setting one up. :) My reply was only if you were to reuse your old db. For example here: https://developer.android.com/training/basics/data-storage/databases.html – Henrik Gyllensvärd Jul 21 '16 at 08:29
  • So i started from scratch and set up a single table for testing purposes. Now I messed up and i get wrong info when querying. How can i even tell how many records are stored in the table when all I get is a number as a result? How can I enter the db that has been created upon launching the app? Do I have to access it from within the emulator or is it possible in IntelliJ which Im using? – vasper Jul 22 '16 at 01:44
0

Android have API for SQL lite, just use it ant it will put db to correct place:

Just like files that you save on the device's internal storage, Android stores your database in private disk space that's associated application. Your data is secure, because by default this area is not accessible to other applications.

https://developer.android.com/training/basics/data-storage/databases.html

dpa
  • 427
  • 4
  • 16