简体   繁体   中英

Suggestions on how to use SQLiteDatabase wisely for Android

I'm thinking of making an app that involves, using a bit of data that is not too large, putting it into an SQLiteDatabase and doing simple operations with the data such as put them into a ListView or display a certain piece of data etc...

So I don't really know that best way of doing this so I've got a few questions to ask.

  1. Is it wise to store my data in a file in my assets folder then extract the data from the file and put it into the SQLiteDatabase?

  2. Is it better to just have the data hard-coded into execute statements when I create my Database?

  3. Should I be using an AsyncTask to populate my SQLiteDatabase with the data I have?

  4. If question 3 is yes, how do I run my Activity such that if I were to simply display the data in my database (in a textview), that the method I call from my Database class that helps me display the data, does so only when the database has been populated (when the AsyncTask has finished).

I hope these questions are clear enough!

Both 1 & 2 approaches work flawlessly. If you want to take approach #1 I really really recommend using Jeff Gifelt's Sqllite asset helper . I found quite easy generating a sqllite db outside the app and embedding it.

Generally speaking, the SQLiteOpenHelper 's onCreate method is created the first time you try to read / write to your db, which is something you always should be doing in another thread. The doc says:

The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.

So the answer is: you should not bother about when the db is created, what you should care about is reading / writing to it from a background thread / asynctask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM