Android: How to use own sqlite database
September 22, 2010 32 Comments
Android has built-in support for using Sqlite database to keep persistent data.
You can also use your own database in your application copying from external file that are saved in assets directory of that application. You may go through following steps:
- Modify your database schema/structure to make it compatible with Android OS. Change primary key fields of all tables to “_id“.
- Create a table “android_metadata(…)” and insert one row. Create a meta-data table - CREATE TABLE “android_metadata” (“locale” TEXT DEFAULT ‘en_US’) and INSERT INTO “android_metadata” VALUES (‘en_US’)
- Copy your database file to … yourapp/assets/
- Read this file from MyDatabaseHelper and write it to application’s database directory
- Open and Read your database from your application using MyDatabaseHelper extends SQLiteOpenHelper
- Query on that database
- That’s all
For Example, application name is SearchSqlite:
- You may open your database file using SQLite Database Browser and then modify it
- Add new table by SQLite Database Browser
- Let your sqlite database file name is zaman_sqlite. Copy it into C:\Users\zaman\workspace\SearchSqlite\assets\zaman_sqlite (change it as your apps directory)
- Read zaman_sqlite and write into databases directory.
- Sample partial code as following
public class MyDataBaseHelper extends SQLiteOpenHelper { //singleton/ single instance reference of database instance private static MyDataBaseHelper _dbHelper; //The Android's default system path of your application database. //private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/"; private static String DB_PATH = "/data/data/com.searchsqlite.droidclient/databases/"; //database name private static String DB_NAME = "zaman_sqlite"; //reference of database private SQLiteDatabase _zamanSqliteDb; // private String _searchToken; // private final Context _context; //Contains search result when user search with any text private Vector<Object> _searchResultVec; private MyDataBaseHelper(Context context) { super(context, DB_NAME, null, 1); this._context = context; _searchResultVec = new Vector<Object>(); _searchToken = ""; } public static MyDataBaseHelper getInstance(Context context) { if(_dbHelper == null) { _dbHelper = new MyDataBaseHelper(context); } return _dbHelper; } public String getLastSearchToken() { return _searchToken; } public void createDataBase() throws IOException { boolean dbExist = checkDataBase(); if(dbExist) { //do nothing - database already exist }else{ this.getReadableDatabase(); try { copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } } } private boolean checkDataBase() { SQLiteDatabase checkDB = null; try { String 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; } private void copyDataBase() throws IOException { //Open your local db as the input stream InputStream myInput = _context.getAssets().open(DB_NAME); // 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); //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); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } public Vector<Object> search(String text) { //list of Versioni, search result with query text _searchResultVec = new Vector<Object>(); _searchToken = text; try { //open database to query openDataBase(); Cursor cursor = _zamanSqliteDb.query("TextTable", new String[] { "Column1"}, "Column1" + " like '%"+text+"%'", null , null, null, null); //mapped all rows to data object if (cursor.moveToFirst()) { do { //create objects } while (cursor.moveToNext()); } //close cursor cursor.close(); } catch(Exception ex) { System.out.println("DatabaseHelper.search()- : ex " + ex.getClass() +", "+ ex.getMessage()); } // return _searchResultVec; } }References:
public class MyDataBaseHelper extends SQLiteOpenHelper
{//singleton/ single instance reference of database instance
private static MyDataBaseHelper _dbHelper;//The Android's default system path of your application database.
//private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
private static String DB_PATH = "/data/data/com.searchsqlite.droidclient/databases/";
//database name
private static String DB_NAME = "zaman_sqlite";
//reference of database
private SQLiteDatabase _zamanSqliteDb;
//
private String _searchToken;//
private final Context _context;
//Contains search result when user search with any text
private Vector<Object> _searchResultVec;private MyDataBaseHelper(Context context)
{
super(context, DB_NAME, null, 1);
this._context = context;
_searchResultVec = new Vector<Object>();
_searchToken = "";
}
public static MyDataBaseHelper getInstance(Context context)
{
if(_dbHelper == null)
{
_dbHelper = new MyDataBaseHelper(context);
}
return _dbHelper;
}
public String getLastSearchToken()
{
return _searchToken;
}public void createDataBase() throws IOException
{
boolean dbExist = checkDataBase();if(dbExist)
{
//do nothing - database already exist
}else{this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}}
private boolean checkDataBase()
{
SQLiteDatabase checkDB = null;try
{
String 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;
}
private void copyDataBase() throws IOException
{
//Open your local db as the input stream
InputStream myInput = _context.getAssets().open(DB_NAME);// 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);//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);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}public Vector<Object> search(String text)
{
//list of Versioni, search result with query text
_searchResultVec = new Vector<Object>();
_searchToken = text;try
{
//open database to query
openDataBase();Cursor cursor = _zamanSqliteDb.query("TextTable",
new String[] { "Column1"},
"Column1" + " like '%"+text+"%'",
null ,
null,
null,
null);//mapped all rows to data object
if (cursor.moveToFirst())
{
do
{
//create objects
} while (cursor.moveToNext());
}
//close cursor
cursor.close();
}
catch(Exception ex)
{
System.out.println("DatabaseHelper.search()- : ex " + ex.getClass() +", "+ ex.getMessage());
}
//
return _searchResultVec;
}
}
I created database in the sqlite database browser.
How do i integrate in to my android application and how do compare it to my editview textbox. if user enters some value in the textbox, it wants to check in the database. If it is correct i want to go next page.
suggest me any ideas and give some sample code
thanks in advance
kumar
Hi Kumar,
1. you need to open your own database using MyDatabaseHelper extends SQLiteOpenHelper
2. when user enters value in textbox , query into database and then compare these.
3. you may use method search()
The post is updated with some code samples, you may find it whether it is helpful.
Hi, This procedure is very much useful for our applications…
Munirasu.V
Hi Zaman,
Good morning.. I need some suggestion from u newly i am learning android and developing my mca final year project in android. so can u give me some URLs which will be useful to learn Android..?
Basically am from mathematical background, i know java somehow better but don’t have that much experience..so based on this plz suggest me..
Hi munirasu,
Thanks, Sorry for late, I have not reviewed it before.
I think android developer site is most useful resource. You will find lots of examples there — http://developer.android.com/guide/index.html
Thanks,
Zaman
Hello there
I already create database and put it into assets folder under path
data/data/my_package_name/database_name..and in my DB there is only one table ,i want to fetch table field on text view with click event.. in my application…
but it gives error like force to close application…if possible please
explain with small example….
Thanks
You may call any method of reference variable without instantiating the object.
For example,
MyDbHelper db;
db.search(…) //call method before create new instance
this is one reason to cause that exception.
thanks,
Zaman
Hi,
Firstly, this is a great tutorial. However, I am facing some issues. My database file in my android device only contains the android_metadata table I know this because I pulled it from eclipse after it was transfered to the device. I don’t understand why it is not copying my other table named words into the database. Any help would be appreciated. Thanks in advance.
Hi ared,
I think you need to be sure about some concerns as following:
1. Make sure your Sqlite db file is okay. First of all, browse it using Sqlite Db browser (link is in article) and add some tables there and data also.
2. Check columns and data types of android_metadata are correct.
3. Then be sure whether raw file is created in android correctly
4. Then do other tasks on that Db
i’m not able to retrieve data from tables application gives exception while retrieving data from DB tables , application forcefully closed.While quering from a table it show no. of rows and well as coloums in result but i am not able to retrieve the data.
Hi Ankur,
“application gives exception” according this pointer I think this is the same problem your are facing like Bhushan [April - 12-2011], you may review that previous answers. please make sure that all the objects are instantiated before call its any methods.
Thanks,
Moniruzzaman
hi thx for the tutorial…
how if, i push the file onto the device, should i import file to asset and write the path too?
as u said we have to change the primary key name to “_id”, if that primary key become foreign key how we identify it, it will have 2 “_id” in 1 table
Hi rika,
1. you can copy paste the sqlite db file into to assets directory or folder of your project as stated in step 3 in article
2. you may create another column for foreign key, for example, it may be named as “id” or “tablename_id” etc.
you may review the following documentation for further insight about SQLite helper http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
thanks
hi M Moniruzzaman,
I am having the same problem of fetching the data from the database,like i created a xml file having 2 buttons(next and previous button) and a textview.by clicking on next button …the textview will be changed to the next row which is in DB..i reviewed this problem as mentioned by other people above but cant able to find the solution..
how to link SQLite database browser in android eclipse?on click of a button i must invoke database to open a file from the database?
Hi!!!
its really good tutorial,
i wanna some ready source code for my mca project, like
- Personal Inventory Management
- Personal Expense Manager
- user’s PhoneBook fatcher
with android – or phonegap.
if any one have such source code then please share with me at my email : info@kmwebsoft.in
Thanks in advance.
its good tutorial,,,But its missing opendatabase method code,,,,please add that method also
Thanks in advance
Yes, I also noticed the missing opendatabase method. Can anyone post that please?
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
how to retieve the single column value in external database and display it in list view
Hello
I already create database and put it into assets folder under path and its size is more than 10 mb. There are multiple tables in database. I am having the particular table fetching data problem.
plz help me.
hi zaman,
i need some help i hv inserted data base in asset folder and all operation are performing well but after then when i change my database structure and add one column then it gives null pointer exception.after modification i again performed metadata query on database.any suggestion y it happens.
I haven’t found that if databases directory is missing, I have to put my DB-copy slice of code inside this block:
String outputFile = this.getDatabasePath(“base.sqlite”).getAbsolutePath();
File f = new File(outputFile);
if (f.getParentFile().mkdirs()) {
// copy database to databases dir
}
This is exactly what I’m looking for! I’m trying to use the code, but when I put it into eclipse, the one method it can’t find is openDataBase();
I’m importing the following:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import android.content.ContentValues;
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;
Nevermind I see an answer up there.
Thanks Zaman , it is quiet useful to my project development
Hi zaman,
I have created a bar chart how to connect my app to the database so that the graph is plotted using the values from the database and also how to refresh my app so that it plots a new graph if there is a change in the database. I am using achartengine to plot the graph. My graph displays top 10 travel destinations and i have created the database using sqlite databrowser.
Pingback: Using External database in android : Android Community - For Application Development
Hi. This post is very informative. Thanks . But i am getting an error. I am trying to execute a query in the same class. But i’m getting an error saying that no such table “XYZ” found. My code is
{
String query=”SELECT _id,Name,PhoneNo FROM ContactDtls “;
database=SQLiteDatabase.openDatabase(DB_PATH+DB_NAME,null,SQLiteDatabase.OPEN_READWRITE);
if(database!=null)
database.rawQuery(query, null);
if(result!=null)
{
if(result.moveToFirst())
{
do
{
System.out.println(“The Name is”+result.getString(result.getColumnIndex(“Name”)));
}while(result.moveToNext());
}
}
}
I made my database in this way and it works without problem.. EXCEPT, when I have DB update and I want to deliever new DB version with new APK..
Help / ideas / suggestions?
I have used your search method and I keep on getting [] as the output on the new activity
Pingback: how to use sqlite file with android : Android Community - For Application Development