Storing and Accessing databases from Assets folder
In the project structure of your Android project in
Eclipse IDE, you can see a folder named ‘assets’. Have you ever used the same?
Below mentioned application provides you with an option to use the assets
folder.
If you want to use any of the databases you have used in another
application in your currently working app, then one option is to store the db
file into the assets folder. For that, export the database file from the first
application to any of the system location. The file would be in
data/data/your_package_name/databases/ location. Now open your current project
and copy that exported database file to the assets folder.
The database includes firstname and secondname of some
students. While entering the firstname in edit area, this sample application
will display the fullname of person by reading the corresponding data from
database file.
MainActivity.java
package com.febi.db;
import java.io.IOException;
import android.app.Activity;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when
the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void
click(View v)
{
DataBaseHelper myDbHelper = new
DataBaseHelper(this);
try {
myDbHelper.createDataBase();
Log.i("DB", "database
created");
} catch (IOException ioe) {
throw new Error("Unable
to create database");
}
try
{
myDbHelper.openDataBase();
Log.i("DB",
"database opened");
EditText
et=(EditText)findViewById(R.id.editText1);
String type=et.getText().toString();
String name=myDbHelper.getName(type);
Toast.makeText(this,
"Your Name is "+name, Toast.LENGTH_LONG).show();
}
catch(SQLException sqle){
Log.i("DB",
"database opened"+sqle);
throw sqle;
}
}
}
DataBaseHelper.class
package com.febi.db;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper{
//The Android's
default system path of your application database.
private static
String DB_PATH = "/data/data/com.febi.db/databases/";
private static
String DB_NAME = "MYFRIENDS";
private
SQLiteDatabase myDataBase;
private final
Context myContext;
/**
* Constructor
* Takes and
keeps a reference of the passed context in order to access to the application
assets and resources.
* @param
context
*/
public
DataBaseHelper(Context 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 void
createDataBase() throws IOException{
boolean dbExist = checkDataBase();
Log.i("CreateDatabase",
""+dbExist);
if(dbExist){
Log.i("DBexists If",
"Database exists");
try {
copyDataBase();
} catch (IOException e) {
Log.i("DBCOpy
exception", ""+e.getMessage());
throw new Error("Error copying
database");
}
//do nothing - database already
exist
}else{
Log.i("DBexists Else",
"DB not exists");
//By calling this method and
empty database will be created into the default system path
//of your application so we are gonna
be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.i("DBCOpy
exception", ""+e.getMessage());
throw new Error("Error copying
database");
}
}
}
/**
* Check if the
database already exist to avoid re-copying the file each time you open the
application.
* @return true
if it exists, false if it doesn't
*/
private boolean
checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH +
DB_NAME;
checkDB =
SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Log.i("CheckDatabase
Try", "database"+checkDB);
}catch(SQLiteException e){
//database does't exist yet.
Log.i("CheckDatabase
Catch", ""+e.getMessage());
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your
database from your local assets-folder to the just created empty database in
the
* system
folder, from where it can be accessed and handled.
* This is done
by transfering bytestream.
* */
private void
copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput =
myContext.getAssets().open("MYFRIENDS");
Log.i("DBCOPY",
""+myInput);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new
FileOutputStream(outFileName);
Log.i("DBCOPY2",
""+myOutput);
//transfer bytes from the inputfile to the
outputfile
byte[] buffer = new byte[1024];
int length;
while ((length =
myInput.read(buffer))>0){
myOutput.write(buffer, 0,
length);
}
String data=myOutput.toString();
Log.i("DBWRITE", data);
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void
openDataBase() throws SQLException{
Log.i("DBopen", "in
openDatabase");
//Open the database
String
myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath,
null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
}
public String
getName(String type)
{
Log.i("DB",
"DBcountry"+type);
try {
copyDataBase();
}
catch (Exception e) {
Log.i("IOEXCEPTION",
""+e.getMessage());
// TODO Auto-generated catch block
e.printStackTrace();
}
myDataBase=this.getReadableDatabase();
myDataBase.beginTransaction();
boolean bool=myDataBase.isOpen();
Log.i("DBACTVITY",
""+bool);
String path=myDataBase.getPath();
Log.i("DBACTVITY",
""+path);
Cursor
cur=myDataBase.rawQuery("Select lastname from friends where
firstname='"+type+"'",null);
int count=cur.getCount();
Log.i("DB", "No of rows
"+count);
cur.moveToFirst();
String name=type+" "+cur.getString(0);
Log.i("DB",
"DB"+name);
cur.close();
myDataBase.close();
return name;
}
@Override
public
synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void
onCreate(SQLiteDatabase db) {
}
@Override
public void
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your
public helper methods to access and get content from the database.
// You could return cursors by doing
"return myDataBase.query(....)" so it'd be easy
// to you to
create adapters for your views.
}
main.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter first name"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate Result"
android:layout_marginTop="40dp"
android:layout_gravity="center"
android:onClick="click"/>
</LinearLayout>
No comments:
Post a Comment