I have written an app that saves some data in sqlite db. Periodically I want to send this data to server and then truncate the tables in sqlite (so that the app does not fill up the space on device).
I am using singleton object of SQLiteOpenHelper (which I have read is thread safe).
So my question is - does the following code looks ok-
SQLiteOpenHelper openHelper = MyDBOpenHelper.getInsance();
SQLiteDatabase database = openHelper.getWritableDatabase();
database.beginTransaction();
Cursor cursor = database.rawQuery(SELECT_ALL_TABLE1_STMT, null);
while( cursor.moveToNext()) {
// save result in tmp list/buffer
}
database.execSQL(DELETE_ALL_TABLE1_STMT);
database.endTransaction();
database.setTransactionSuccessful();
// send data to server
// and repeat the process for rest of the tables.
If there is another thread that is trying to write to the same table (that I am reading and later will truncate), then does the above code looks ok to handle that scenario?
thanks!
You need more input on this from here:
Synchronized Methods
Android Multi-threading
Plus AsyncTask, of course.
Related
In order to avoid blocking the main/UI thread, Android API provides Loader for accessing database. In order to save memory (RAM), the API does not load the whole rows (result) into memory, so that we can have a ListView containing so many items without out-of-memory.
Do OrmLite & greenDAO provides both of these capabilities? If not, is there any approach to do asynchronous query and partial load-and-swap for populating huge data into a ListView?
I don't think ORM-frameworks provide the ability to only load row-parts.
With greendao you can use listLazy() on your Query to load the entities lazily from DB. This introduces great performance benefit over "prefetching" all entities.
I know that greendao also provides some way to asynchronously load entities with an AsyncSession. But I currently don't know how this is to be used.
Normally all the CRUD-operations in greendao happen on the same thread that invoked update and such.
I haven't used OrmLite yet, so I have no information about this.
I can't pronounce over OrmLite but with greenDAO I know that you can execute raw queries on the db. After all under all that generated code lies an SQLITE database.
Take a look over NoteActivity in the greenDAO example project:
https://github.com/greenrobot/greenDAO/blob/master/DaoExample/src/main/java/de/greenrobot/daoexample/NoteActivity.java
In onCreate() you can clearly see that the cursor is fetched like this:
DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "db_name", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
ObjectDao objectDao = daoSession.getObjectDao();
[..]
Cursor cursor = db.query(objectDao.getTablename(), objectDao.getAllColumns(), null, null, null, null, null);
As you can see this cursor can be used as any other cursor to display the data.
Now, doing this on the main thread is wrong and this is done just to expose a functionality. What you can do from this (since you mentioned loaders):
Keep a single instance of db / daoSession per app. Ideally move the code into the application class or create a singleton to handle it
Create a loader and override loadInBackground(). In that method run the db query and return the cursor. Use the loader result with a cursor adapter and display your data.
I use this function to insert data into the SQLite Android data base:
public long insertAccount(String code,String name,int s3,int s4,String s5,String s6,int s7,
int s8,int s9,int s10,int s11,String s12,String s13,int s14,int s15,int s16) {
//container and place in it the information you want inserted, updated, etc.
ContentValues initialValues = new ContentValues();
initialValues.put(Code, code);
initialValues.put(Name,name);
initialValues.put(Type, s3);
initialValues.put(Level1, s4);
initialValues.put(Father, s5);
initialValues.put(ACCCurr,s6);
initialValues.put(AccNat, s7);
initialValues.put(LowLevel, s8);
initialValues.put(DefNum, s9);
initialValues.put(AccClass, s10);
initialValues.put(SubClass, s11);
initialValues.put(SSClass1, s12);
initialValues.put(SSClass2, s13);
initialValues.put(Stype1, s14);
initialValues.put(Stype2, s15);
initialValues.put(Stype3, s16);
return db.insert(DATABASE_TABLE, null, initialValues);
}
But this takes much time when inserting about 70,000+ rows! How can I accelerate the process of insertion into the data base, and after the insert is done, how can I apply Update on it?
Some options:
Prepopulate your database. See "Ship an application with a database"
Use transactions to reduce the time waiting for I/O. See e.g. "Android SQLite database: slow insertion". Likely you cannot wrap all 70k rows in a single transaction but something like 100..1000 inserts per transaction should be doable, cutting the cumulative I/O wait time by orders of magnitude.
Inserting into SQLlite android using PHP? how is it possible using php in android phone, I am sorry I didn't got this.
Anyways I believe you have written the java code up here and you have like 7k+ records that you want to insert in your db.
The style of inserting a bulk of records in any db is called "Bulk Inserts", the idea is to create as less number of transactions as possible and rather do all the inserts in one shot; In case of relational db's like sql server and oracle its done by specific api's as well, but in sqllite the plain old idea is to make a single transaction with a bunch of data
check out this article which uses the same technique http://www.techrepublic.com/blog/software-engineer/turbocharge-your-sqlite-inserts-on-android/ and also explains it quite well.
You have to use transaction to done insertion in 1 time. you can use this:
//before insertion
db.beginTransaction();
//====do insertion
//after insertion
db.setTransactionSuccessful()
db.endTransaction();
For my app, I need to have a database containing one table with 4 columns in it. This tables and its parameters will be static after creation, so that they will stay in the same place with the same data to be listed in a list view.
I have the DatabaseHandler for this purpose, but what I'm asking is how do I define this database in code? Does it build again every launch or is it only with the first launch? How does it work?
There are many ways of doing it. The one i follow is I will create database and tables in launch activity. Then i will insert data by counting the number of records in the table(Only for static table).So if(number of records == 0) then insert data into database. Otherwise do code for your app. It should work.
EDIT
This is the code to get total number of records in the database
In Database Class
YourDatabase
public class YourDatabase extends SQLiteOpenHelper{
//coding for table create and insert records goes here
//Your tables total number of records can be identified by following code
public long yourTableCount()
{
SQLiteDatabase db = getReadableDatabase();
return DatabaseUtils.queryNumEntries(db, YOURTABLE_NAME);
}
}
Your Activity
Calling Database class from your activity
YourDatabase db = new YourDatabase(this);
long numberofrecords = db.yourTableCount();
if(numberofrecords == 0)
{
//Insert your data in to database
//This will happen only in first launch because after that the numberofrecords == total number of records inserted in the database.
}
You can create the database manually using a database manager. Once you have the database defined in your assets folder it will remain there and be compressed within the apk file on build. Try SQLiteManager which has a free trial version that will let you design you database. Or use a firefox addon here: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
i am developing android app, here i am having an huge no of data approximately 10000 records with 10 fields in the server, i need to get this data and store it in the my local db, so for this i tried to implement by getting the data in the form of json parsing it and inserting in db one by one, it is taking less time to download the data but more time to insert to the db, after some time i get to know that i am inserting to the db one by one, so insertion operations looping based on the total no of records which had been got. i tried to look for the alternatives i could not get the way for this, so i request you to give me suggestions and snippets to me achieve this.
Thanking you
use transactions to wrap multiple inserts into one operation, that's a lot faster: Improve INSERT-per-second performance of SQLite?
List<Item> list = getDataFromJson();
SQLiteDatabase db = getDatabase();
db.beginTransaction();
try {
// doing all the inserts (into memory) here
for(Item item : list) {
db.insert(table, null, item.getContentValues());
}
// nothing was actually inserted yet
db.setTransactionSuccessful();
} finally {
// all inserts happen now (if transaction was set to successful)
db.endTransaction();
}
I have an app that uses a database with 3 tables in it. Those 3 tables have data read from and written to them by activities and services.
Having gotten a few "android.database.sqlite.SQLiteException: database is locked" crashes, I went in to the database adapter class and wrapped every write, update, or delete function with a synchronized statement, so like:
public int deleteExpiredAlarms() {
String whereClause = FIELD_EXPIRED + " = 1";
int val = 0;
synchronized(dbWriteLock) {
val = db.delete(ALARM_DATABASE_TABLE, whereClause, null);
}
return val;
}
That seemed to make it better. But lately it's gotten bad again as I've added more services that read and write to different tables.
Do I need to synchronize ALL db access statements, including queries?
The exception is occurring on the attempt to open the writable database via the open helper...should I synchronize that act also?
I've heard that I should only be using one db helper so that there won't be issues with multiple threads accessing the db. How do I use only one db helper? Every example I've seen so far has the db helper as an instantiated value inside the db adapter....so wouldn't that be a separate db helper per db adapter instantiated (one in an activity, one in a service running,etc)
I've looked at using a content provider instead, as it's been claimed to solve problems like this, but it's really more work than I want to do if I should be able to have direct db access without locking issues. And I do not plan to make this db accessible to other apps.
Thanks for the help.