Reputation: 65
"SQLitedatabase operations(specially open or create database)need a lot of time to accomplish,so u should execute Database transaction Asynchronously".
I read this lines in "Professional Android 4 Application Development".
But I try ,It does't need lots of time. Who can tell me the truth?
The below is an example. Ths!
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import db.Books;
public class MainActivity extends Activity
{
private TextView tvTest;
private static final String DB_COLUMN1 = "title";
private static final String DB_COLUMN2 = "value";
private static final String TABLE_NAME = "mytable";
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTest = (TextView)findViewById(R.id.tv_test);
System.out.println("Time1:"+ System.currentTimeMillis());
// The book is a class that extends SQLiteOpenHelper
Books books = new Books(this,"mybook",null,1);
db = books.getReadableDatabase();
System.out.println("Time2:"+ System.currentTimeMillis());
}
}
Upvotes: 2
Views: 79
Reputation: 152817
For a simple database not involving much disk I/O it does not matter, as it is the likely case in your test program.
Opening a database per se does not take much I/O. However, opening the database will create it if it does not already exist.
Creating a database can take a lot of time, especially if there is a lot of I/O involved. Copying a prepopulated database from assets or running a lot of SQL queries to populate the database in code can take a lot of time.
It's a good practice to not do any potentially time-consuming I/O on the UI thread.
Upvotes: 1
Reputation: 634
Usually it does not take too much time that we need to create it Asynchronously. It is a good practice though. But my experience of 12 apps it does not necessarily need to be created in AsyncTask
Upvotes: 0