Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Download
Standard view
Full view
of .
Add note
Save to My Library
Sync to mobile
Look up keyword
Like this
2Activity
×
0 of .
Results for:
No results containing your search query
P. 1
SQLite

SQLite

Ratings: (0)|Views: 199|Likes:
Published by Selva Iyyam Perumal
SQLite
SQLite

More info:

Categories:Types, Reviews
Published by: Selva Iyyam Perumal on Jan 05, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, DOC, TXT or read online from Scribd
See More
See less

01/10/2013

pdf

text

original

 
OpenDatabase
 public
 voidopenDataBase() 
throws
 
{ 
//Open the database
 
myPath = DB_PATH + DB_NAME;myDataBase = SQLiteDatabase.openDatabase(myPath,
null
,SQLiteDatabase.OPEN_READONLY);  }
Close
@Override
 public
 
synchronized 
 voidclose() {  if(myDataBase !=
null
)myDataBase.close();  
super
.close();  } 
Copy Database
 private
 voidcopyDataBase() 
throws
 
{ 
//Open your local db as the input stream
 
myInput = myContext.getAssets().open(DB_NAME);  
// Path to the just created empty db
 
outFileName = DB_PATH + DB_NAME; 
//Open the empty db as the output stream
 
myOutput =
new
 
(outFileName);  
//transfer bytes from the inputfile to the outputfile
 byte[]buffer =
new
 byte[1024];  intlength; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer,0, length);  } 
//Close the streams
myOutput.flush(); myOutput.close(); myInput.close();  }
Check Data Base private
 booleancheckDataBase(){  SQLiteDatabase checkDB =
null
; 
 
 
try
{ 
myPath = DB_PATH + DB_NAME;checkDB = SQLiteDatabase.openDatabase(myPath,
null
,SQLiteDatabase.OPEN_READONLY);  }
catch
(SQLiteException e){ 
//database does't exist yet.
 } if(checkDB !=
null
){ checkDB.close();  } 
return
checkDB !=
null
?
true
:
false
; } 
2. Copying, opening and accessing your database in your Android application.
 Now just put your database file in the "assets" folder of your project and create aDatabase Helper class by extending theSQLiteOpenHelper  class from the "android.database.sqlite" package.Make your DataBaseHelper class look like this:
 public
 
class
DataBaseHelper
extends
SQLiteOpenHelper{ 
//The Android's default system path of your application database.
 
 private
 
static
 
DB_PATH ="/data/data/YOUR_PACKAGE/databases/"; 
 private
 
static
 
DB_NAME ="myDBName"; 
 private
SQLiteDatabase myDataBase;
 private
 
final
 
myContext; 
/*** Constructor * Takes and keeps a reference of the passed context in order toaccess to the application assets and resources.* @param context*/ 
 
 public
DataBaseHelper(
context) {  
super
(context, DB_NAME,
null
,1);  
this
.myContext= context; } 
/**
 
* Creates a empty database on the system and rewrites it with your own database.* */ 
 
 public
 voidcreateDataBase() 
throws
 
{ booleandbExist = checkDataBase();  if(dbExist){  
//do nothing - database already exist
 }else{  
//By calling this method and empty database will becreated into the default system path
 
//of your application so we are gonna be able tooverwrite that database with our database.
 
this
.getReadableDatabase();  
try
 { copyDataBase(); } 
catch
 (
e) {  
throw
 
new
 
("Error copying database");  } } } 
/*** Check if the database already exist to avoid re-copying the fileeach time you open the application.* @return true if it exists, false if it doesn't*/ 
 
 private
 booleancheckDataBase(){  SQLiteDatabase checkDB =
null
; 
try
{ 
myPath = DB_PATH + DB_NAME;checkDB = SQLiteDatabase.openDatabase(myPath,
null
,SQLiteDatabase.OPEN_READONLY);  }
catch
(SQLiteException e){ 
//database does't exist yet.
 } if(checkDB !=
null
){ checkDB.close();  } 
return
checkDB !=
null
?
true
:
false
;

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->