1

I have seen many solutions which are code oriented, like if you have to create a table, you would have to create it using code, but this is not good for complex applications, as we have SQLite Browser to create database and its tables, and it generates a database file.

Now the question is, where to place that file in my project, there is no data folder in eclipse project, I dont know what to do, its interaction is not like MySQL where we use driver or connection?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • Hi Muhammad, I had the same issue. If you want to just copy/paste some db code and get started with your existing db, check out my solution here. http://stackoverflow.com/questions/3548533/full-android-database-helper-class-for-existing-sqlite-database – Biff MaGriff May 05 '11 at 20:31

1 Answers1

0

You can follow this steps to use an existing database in your application:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

I've also made a blog post some time ago which combined the solution above with the use of ormlite:

http://www.b-fil.com/blog/2011/01/20/android-repository-ormlite-existing-sqlite-db/

BFil
  • 12,966
  • 3
  • 44
  • 48
  • I have read on many tutorials that this database is saved in the directory "DATA/data/APP_NAME/databases/FILENAME". But i cannot find any Data folder in my project folder which is in eclipse workspace. Where it is? – Muhammad Atif Agha Apr 15 '11 at 07:02
  • Yes, that should be the right place to put the database into the device. Under your project directory, you can put your database under an "assets" folder, then with the first solution it will copy the database from the "assets" folder of your application to your package database folder in the device – BFil Apr 15 '11 at 07:06
  • right, that makes sense, so till i am developing on pc, i can give the path of assets, and when i would deploy it, i would finally change the path to that, given above. Application will automatically pick from assets or i would have to mention this path somewhere? – Muhammad Atif Agha Apr 15 '11 at 07:08
  • assets folder will be present in your application apk, so on runtime, if you read the tutorial, there's a code line: myContext.getAssets().open(DB_NAME); this line referencec the database file in your assets folder, then it will copy (if still not present) under the filepath specified in the class (variables: DB_PATH + DB_NAME). – BFil Apr 15 '11 at 07:12
  • there is OpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } and i have given the DATABASE_NAME as "db" it is the name of that file which i placed in assets and made by SQLite Browser. Right? – Muhammad Atif Agha Apr 15 '11 at 07:23
  • yes, it is, DB_NAME is the name of the file in your assets folder (with the file extension), and it will be copied with the same name under the databases folder of your application in your device – BFil Apr 15 '11 at 07:28
  • I am very near, in DATABASE_NAME attribute, would we write assets/dbfile or only db file name, i am using SQLite browser which generates db file without any extension, and when i refer it, it does not work, if you know any GUI based tool, please let me know also, many thanks!!! – Muhammad Atif Agha Apr 15 '11 at 09:37
  • DATABASE_NAME is without the assets path, just the filename.. a couple of possibilities: have you created a table "android_metadata" with a locale column? (it is needed). Have you enabled permissions to write to external storage for your application? Anyway, if you follow the first link i provided step by step, you should be able to make it work without problems – BFil Apr 15 '11 at 09:56
  • no i did not add metadata or anything like that, i was following some other link, but you are right, i must follow your given link, then if i would stuck, i may need your help, thanks a lot. – Muhammad Atif Agha Apr 15 '11 at 10:00