Copy SQLite database file from Assets folder in android
This post about how to copy SQLite database file to asset folder android.
WHY?
Put sqllite db file into asset folder
Source Code
SqlLiteDbHelper.class
Screen Shot
Retrive data from Sqlite DB
MainActivity.class
This post about how to copy SQLite database file to asset folder android.
WHY?
Some time we will get manual entry SQLite DB. So that file directly we can’t use it.
Manul entry DB file screen shot
Put sqllite db file into asset folder
SqlLiteDbHelper.class
/**
* Copyright 2013-present
http://iamvijayakumar.blogspot.com/.
*
* Licensed under the Apache License,
Version 2.0 (the "License");
* you may not use this file except in
compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed
to in writing, software
* distributed under the License is distributed
on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.
* See the License for the specific language
governing permissions and
* limitations under the License.
*/
import java.io.File;
import
java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import
java.io.OutputStream;
import java.util.ArrayList;
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.SQLiteOpenHelper;
import android.util.Log;
public class SqlLiteDbHelper extends SQLiteOpenHelper {
//
All Static variables
//
Database Version
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_PATH = "/data/data/com.copy.copydatabasefromasset/databases/";
//
Database Name
private static final String DATABASE_NAME = "mycontacts";
// Contacts table name
private static final String TABLE_CONTACT = "contact";
private SQLiteDatabase db;
//
Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAILID = "emailid";
private static final String KEY_MOBILENO = "mobileno";
Context ctx;
public
SqlLiteDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ctx = context;
}
//
Getting single contact
public Contact
Get_ContactDetails(String name) {
SQLiteDatabase
db = this.getReadableDatabase();
Cursor
cursor = db.query(TABLE_CONTACT, new String[] { KEY_ID,
KEY_NAME, KEY_EMAILID, KEY_MOBILENO }, KEY_NAME + "=?",
new String[] { name }, null, null, null, null);
if (cursor != null &&
cursor.moveToFirst()){
Contact
cont = new
Contact(cursor.getString(1), cursor.getString(2), cursor.getString(3));
// return contact
cursor.close();
db.close();
return cont;
}
return null;
}
public void
CopyDataBaseFromAsset() throws IOException{
InputStream in = ctx.getAssets().open("mycontacts");
Log.e("sample", "Starting
copying" );
String outputFileName = DATABASE_PATH+DATABASE_NAME;
File databaseFile = new File( "/data/data/com.copy.copydatabasefromasset/databases");
// check if databases folder exists, if not
create one and its subfolders
if
(!databaseFile.exists()){
databaseFile.mkdir();
}
OutputStream out = new
FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while ((length =
in.read(buffer))>0){
out.write(buffer,0,length);
}
Log.e("sample", "Completed" );
out.flush();
out.close();
in.close();
}
public void openDataBase () throws SQLException{
String path = DATABASE_PATH+DATABASE_NAME;
db = SQLiteDatabase.openDatabase(path,
null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
}
@Override
public void
onCreate(SQLiteDatabase db) {
}
@Override
public void
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated
method stub
}
}
Contact.class
package
com.copy.copydatabasefromasset;
/**
* Copyright 2013-present
http://iamvijayakumar.blogspot.com/.
*
* Licensed under the Apache License,
Version 2.0 (the "License");
* you may not use this file except in
compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed
to in writing, software
* distributed under the License is distributed
on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.
* See the License for the specific language
governing permissions and
* limitations under the License.
*/
public class Contact {
public String name = "";
public String email = "";
public String mobileNo = "";
public String getName() {
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String
email) {
this.email = email;
}
public String getMobileNo()
{
return mobileNo;
}
public void setMobileNo(String
mobileNo) {
this.mobileNo = mobileNo;
}
// constructor
public Contact (String
name,String email,String mobileNo){
this.email = email;
this.mobileNo = mobileNo;
this.name = name;
}
public Contact (){
}
}
package
com.copy.copydatabasefromasset;
/**
* Copyright 2013-present
http://iamvijayakumar.blogspot.com/.
*
* Licensed under the Apache License,
Version 2.0 (the "License");
* you may not use this file except in
compliance with the License.
* You may obtain a copy of the License at
*
*
http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed
to in writing, software
* distributed under the License is distributed
on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.
* See the License for the specific language
governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import android.os.Bundle;
import
android.app.Activity;
import android.view.Menu;
import
android.widget.TextView;
public class MainActivity extends Activity {
TextView contact ;
SqlLiteDbHelper dbHelper = new SqlLiteDbHelper(this);;
Contact contacts ;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contact =
(TextView)findViewById(R.id.contact);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.CopyDataBaseFromAsset();
}
catch (IOException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
dbHelper.openDataBase();
contacts = new Contact();
contacts = dbHelper.Get_ContactDetails("vijay");
contact.setText(contacts.name +"\n" +contacts.email +"\n" +contacts.mobileNo);
}
}
Error!
ReplyDeleteNo errors but it just don't work
ReplyDeleteit Works! thanks a lot
ReplyDeletehow? it doesnot work! can you help me please
Delete