1 - SQLiteQueryBuilder.query() vs SQLiteDatabase.query()
What's the difference between these two approaches? For me it seems that the only benefit of SQLiteDatabase is its ability to work with different databases. Am I right?
Answer :
The primary method is SQLiteDatabase#rawQuery(). Both SQLiteDatabase#query() and SQLiteQueryBuilder are just helpers to compose the SQL.
The SQLiteDatabase#query() can only compose simple queries from one table. The SQLiteQueryBuilder can create joins, unions and such. Since SQLiteQueryBuilder is an extra object,
you'd only construct it if you need it's power.
Personally I think that any non-trivial SQL is easier to read as SQL than as pieces composed with helper like this, so I'd use rawQuery over SQLiteQueryBuilder,
but that's a matter of taste and how well you know SQL. The SQLiteQueryBuilder might also be useful if you have some common subqueries that you want to compose together in different ways.
In fact I would prefer (I use sqlite mainly using it's C API) to use prepared statements, because compilation of SQL is slow compared to it's execution, but I don't see that option in the Java interface.
2 - I found that onCreate of ContentProvider gets called first, even before the onCreate of my Activity. eg: Vogella Android SqlLite.
But I wanted to know how does it happen and why?
Is it because we register it as a provider in the Manifest?
Answer :
onCreate is to initialize your content provider on startup. This method is called for all registered content providers on the application main thread at application launch time.. yes ,it is because you register them in manifest...
while launching the app.. the manifest is checked for any contentproviders.. and if any..their oncreate is called.. so that they are available for your application as soon as it is created..
3 - Difference between GetWritableDatabase and getReadableDatabase
getWritableDatabase () method'u çağırdığımızda, tanımladığımız class'ın(SQLiteOpenHelper class'ından extend eden bir class tanımlamıştık) onCreate() method'u çağırılır otomatik olarak.
GetWritableDatabase() method is invoked to open the database in read-write mode, once the database disk space is full, the database will only be read but not write.
( Veritabanını read-write mode'da açmak için GetWritableDatabase() method'unu çağırırız. Eğer disk space full ise(veya başka bir hata olursa), artık disk'e daha fazla veri yazılamayacağı için veritabanı read write modda açılamaz, read modda açılır. )
-------------------
SQLiteDatabase getReadableDatabase ()
Create and/or open a database. This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.
Like getWritableDatabase(), this method may take a long time to return, so you should not call it from the application main thread, including from ContentProvider.onCreate().
Returns
SQLiteDatabase a database object valid until getWritableDatabase() or close() is called.
Throws
SQLiteException if the database cannot be opened
( GetWritableDatabase() method'unu çağırdığımızda, veritabanı read-write mode'da açılmaya çalışılır. Eğer disk space full ise(veya başka bir hata olursa), artık disk'e daha fazla veri yazılamayacağı için veritabanı read write modda açılamaz, read modda açılır. )
-------------------
Difference between GetWritableDatabase and getReadableDatabase
They return the same object unless the disk is full or there is some permission error that forces to open the database in read-only mode. The name is a bit confusing though :)
( Disk full değilse ve başka herhangi bir hata oluşmazsa, her iki method'da read/write modda bir veritabanı yaratır veya böyle bir vertabanı zaten varsa bu veritabanını açar. )
As a rule of thumb you should call these methods outside the UI thread. Both can take a long time to return.
If you are not going to write the database just use getReadableDatabase as it will contribute to your code clarity and intention.
( Veritabanına yazmayacaksak, kodun daha temiz ve anlaşılır gözükmesi için getReadableDatabase() method'unu çağırmalıyız. )
( Öenmli bir ayrıntı da şudur. getReadableDatabase() method'unda getWritableDatabase() method'u çağırılır. Yani getReadableDatabase() 'nu çağırırsak otomatik olarak getWritableDatabase() method'u da çağırılacaktır, db writable açılamazsa readable açılacaktır. )
Her iki method da SQLiteDatabase object'e reference return eder.
ContentProvider class'ını extend eden BirthProvider isimli class'ın onCreate() method'unun implementation'ı aşağıdadır inceleyelim.
public boolean onCreate() {
// TODO Auto-generated method stub
Context context = getContext();
dbHelper = new DBHelper(context);
// permissions to be writable
database = dbHelper.getWritableDatabase();
if(database == null)
return false;
else
return true;
}
Hiç yorum yok:
Yorum Gönder