How to use my own sqlite database?

I put my database field in the assets folder. And use the code from this blog to copy the database to "/ data / data / my_packname / databases /", (I will run this copy code in the onCreate () method when I run this application), then use select * from. .. to receive data. But that gives me an exception: no such table.

Someone told me that if I try to copy a file to SQLiteOpenHelper onCreate (), it is too late. Therefore, the copy file code cannot copy the complete file.

So, do I need to use adb or ddms to pull out the database first?

So can anyone teach me how to use my own data? Could you tell me the settings?

+7
android database sqlite
Mar 05 '10 at 14:33
source share
7 answers

I used the instructions on this blog and found them on the right track to seriously complicate the problem by unnecessarily expanding SQLiteOpenHelper . I was more fortunate:

  • Create a utility class that creates static db by copying it to the correct directory from the assets, but without getting stuck in the following SQLiteOpenHelper format.

  • Using the same utility class to open db using SQLiteDatabase.openDatabase()

Edit: here is the version of this utility class that I created; it is not completely completed, but you will get a drift.

 public class DbUtils { private static final String DB_PATH = "/data/data/com.mypackage.myapp/databases/"; private static final String DB_NAME = "my.db"; public static void createDatabaseIfNotExists(Context context) throws IOException { boolean createDb = false; File dbDir = new File(DB_PATH); File dbFile = new File(DB_PATH + DB_NAME); if (!dbDir.exists()) { dbDir.mkdir(); createDb = true; } else if (!dbFile.exists()) { createDb = true; } else { // Check that we have the latest version of the db boolean doUpgrade = false; // Insert your own logic here on whether to upgrade the db; I personally // just store the db version # in a text file, but you can do whatever // you want. I've tried MD5 hashing the db before, but that takes a while. // If we are doing an upgrade, basically we just delete the db then // flip the switch to create a new one if (doUpgrade) { dbFile.delete(); createDb = true; } } if (createDb) { // Open your local db as the input stream InputStream myInput = context.getAssets().open(DB_NAME); // Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(dbFile); // transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } // Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } } public static SQLiteDatabase getStaticDb() { return SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY); } } 
+11
Mar 05
source share

After you have copied the database, you should try to close and reopen the SQLiteDatabase object before executing any query. I had a similar problem with copying db from the input stream and having it resolved for me.

+1
Mar 05
source share

here is my version from the code "Silvio Donnini" :), now you can easily update the database.

 private static final String DB_PATH = "/data/data/pakagename/databases/"; private static final String DB_NAME = "databaseName"; private static SQLiteDatabase db; public static void createDatabaseIfNotExists(Context context,int version) throws IOException { boolean createDb = false; File dbDir = new File(DB_PATH); File dbFile = new File(DB_PATH + DB_NAME); if (!dbDir.exists()) { dbDir.mkdir(); createDb = true; } else if (!dbFile.exists()) { createDb = true; } else { // Check that we have the latest version of the db db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY); if (db.getVersion() != version) { dbFile.delete(); createDb = true; } } if (createDb) { // Open your local db as the input stream InputStream myInput = context.getResources().openRawResource(R.raw.database); // Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(dbFile); // transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } // Close the streams myOutput.flush(); myOutput.close(); myInput.close(); SQLiteDatabase dbwrite = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE); dbwrite.setVersion(version); dbwrite.close(); if (db != null) db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY); } } public static SQLiteDatabase getStaticDb() { if (db != null) return db; return SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY); } 
0
Dec 23
source share

I know this is an old question, but I lost a lot of time figuring it out with all the answers.




The problem is that the device stores the database in its database data / data /.../. Thus, when you change sth about the database (name, add android metadata table, size ..), it will not make any difference due to the saved database and the method that checked the existing database.




To get the latest database, after changing it, you should run the program without checking the existence of existing data (for example, instead of dbExist = checkDataBase(); make it simply false)

 dbExist = checkDataBase(); 

Change to:

 dbExists = false; 

After you have raised the β€œnew” data file, you can return to checking the existing ones.

Hope this helps someone, dina

0
Mar 01 '13 at 13:07 on
source share

I know this is an old post, but for those who are still here, after searching Google or Bing, this is the solution to this problem:

in createDataBase () the following check exists:

 this.getReadableDatabase(); 

This checks if there is already a database with the specified name and if an empty database is not created so that it can be overwritten using the one in the resource folder. On new devices, this works flawlessly, but there are some devices on which it does not work. Mostly old devices. I don’t know exactly why, but it seems that the getReadableDatabase () function not only gets the database, but also opens it. If you then copy the database from the asset folder on top of it, it still has a pointer to an empty database, and you will get a table that does not contain errors.

So, for it to work on all devices, you must change it to the following lines:

 SQLiteDatabase db = this.getReadableDatabase(); if (db.isOpen()){ db.close(); } 

Even if the database is open on the check, it closes after that, and this will not give you more problems.

0
Jul 21 '13 at 0:07
source share

Calm down guys, After much research, finally found a stupid mistake for the error "there is no such table"

Check the database name in the Assets folder if it looks like DATABASE_NAME.EXTENSION, and then put the full name in the Helper class with the extension, and solved my problem.

For example, in Assets, the database name is login.sqlite or login.db. put DB_NAME = login.sqlite completely with the extension. this tutorial now works great.

0
Jan 08 '14 at
source share

The way you create a database from an article you posted is a little different than how it is done in Android examples (I don't want to say whether this is good or bad).

I learned how to use databases from Example SDK for NotePad

This is a good example to start with, because it covers the topic of creating a database and accessing the database through ContentProvider (this is really the only good way to get data from db, otherwise you will have problems trying to get data from many places at the same time your code).

You should notice that SQLiteOpenHelper is really powerful and β€œit will help you” if you use it correctly. For example, it stores the current database version (not the sqlite version, but the number that you specify in the database schema version), and when you create a new version of the application with a new database structure, you can update the current schema to the new version in onUpdate.

-one
Mar 05
source share



All Articles