0

I have created a database in SQLiteStudio. The database consists of a single table, with columns 'Name', 'Age' and 'Occupation', and several rows of data.

I want to transfer that database from SQLiteStudio to an Android phone. How would I do this?

DinnerPlate
  • 17
  • 1
  • 8

2 Answers2

1

Well, the simplest way if there are only a few rows would probably be to create the schema and populate it in SQLiteOpenHelper.onCreate(db) using SQL statements executed with db.execSQL().

If you would like to avoid that however, and just directly take the database that you've created externally over into your app, you could look into Android SQLiteAssetHelper

chuz
  • 506
  • 1
  • 4
  • 10
  • Thanks. However, I do need the database stored on an actual phone, not in Android Studio. All I see is a line here: super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION); to store it on the phone. I can't make sense of that, and I have no idea what path name it should be. – DinnerPlate Apr 04 '16 at 14:49
1

Yes, the Android SQLiteAssetHelper is really simple compared to other guides that uses hundred of lines with codes. Tried numerous guide and tutorials before I found out about that helper. Here is a really simple guide to follow where he's using it and explains it pretty good. Import and use external database in Android

Halalbin
  • 95
  • 12
  • Thanks. However, I do need the database stored on an actual phone, not in Android Studio. All I see is a line here: super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION); to store it on the phone. I can't make sense of that, and I have no idea what path name it should be. – DinnerPlate Apr 04 '16 at 14:50
  • If i understand you correctly: put the .db file in asset/databases as described in the guide. Then you take the phone and connect it to your computer (be sure USB-debugging is enabled) and run your app via the Run meny, select your phone and press run. Then you have the .db file in your phone. I think the actual .db files is stored in /data/data/com.example.dinnerplate.myapp/databases or something similar. But you need root access to view those files. – Halalbin Apr 04 '16 at 15:23
  • Or are you not using an IDE to make an app or something like that? Are you going too use the .db file for an app or just have it there? There is apps in the play store that can be used to CRUD sqlite databases. In that case I think you just transfer it to the phone as an regular filer. Here are a few of them https://play.google.com/store/search?q=asqlitemanager – Halalbin Apr 04 '16 at 15:30
  • I did try using that asqlitemanager app to create the database on the phone. Unfortunately, it requested a root shell - I am not using my own phone, so I don't think rooting the phone is a good idea. I have got USB debugging enabled and I connected a USB cable between my PC and the phone, but I can't find any option to upload files (other than uploading photos). – DinnerPlate Apr 04 '16 at 18:37
  • I havnt used a PC and Android in a couple of years so thing may have changed. But i want to remember that the phones memory card shows up in the file explorer and you can simply drag and drop the file in the location on the phone. On mac there is an app called Android file transfer you install on the computer, then you can drag and drop to and from the phone. Look for something similar for windows. Or you could use ES file explorer and create a stfp connection(or similar) to the PC if, then you can browse the PC and copy the files, you need to be on the same wifi. – Halalbin Apr 04 '16 at 19:30
  • Or put the file online(dropbox, google drive etc) and download it to the phone or use bluetooth if the PC have it. BTW...What are you going to use the db file on the phone for? This seems like a normal file transfer to me. – Halalbin Apr 04 '16 at 19:32