Hello guy's
I hope this post will help you copy external database in your app on Android Studio.
now you working for here ..copy and paste this code and enjoy.
SqlitedatabaseHelper.java
package bla.bla.bla;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by Samset_singh on 28/08/15.
*/
public class SqliteDatabasehelper extends SQLiteOpenHelper {
Context context;
private static final String ERROR = "SqliteDatabaseHelper";
private String DB_PATH ;
public static String DB_NAME = "name.sqlite";
public static String TABLE_NAME = "sam";
public static int VERSION = 3;
SQLiteDatabase database;
public SqliteDatabasehelper(Context context) {
super(context,DB_NAME,null,VERSION);
this.context = context;
DB_PATH = "/data/data/YOUR_PACKEGE_NAME/databases/";
OR
//DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
/*
* Copy DB
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error al copiar la base de datos");
}
}
}
public 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 boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath,null,LiteDatabase.OPEN_READWRITE);
}catch(SQLiteException e){
//database does't exist yet.
Log.e("Chcek Database", ""+"Database not exist"+e);
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
database = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(database != null)
database.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
I hope this post will help you copy external database in your app on Android Studio.
now you working for here ..copy and paste this code and enjoy.
SqlitedatabaseHelper.java
package bla.bla.bla;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by Samset_singh on 28/08/15.
*/
public class SqliteDatabasehelper extends SQLiteOpenHelper {
Context context;
private static final String ERROR = "SqliteDatabaseHelper";
private String DB_PATH ;
public static String DB_NAME = "name.sqlite";
public static String TABLE_NAME = "sam";
public static int VERSION = 3;
SQLiteDatabase database;
public SqliteDatabasehelper(Context context) {
super(context,DB_NAME,null,VERSION);
this.context = context;
DB_PATH = "/data/data/YOUR_PACKEGE_NAME/databases/";
OR
//DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
/*
* Copy DB
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error al copiar la base de datos");
}
}
}
public 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 boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath,null,LiteDatabase.OPEN_READWRITE);
}catch(SQLiteException e){
//database does't exist yet.
Log.e("Chcek Database", ""+"Database not exist"+e);
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
database = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(database != null)
database.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
DataController.java
public class DataController {
SqliteDatabasehelper eventoConexion;
String name;
String query = null;
List<Bean> models = null;
SQLiteDatabase db;
public DataController(Context context) {
eventoConexion = new SqliteDatabasehelper(context);
}
public void createDb() {
try {
eventoConexion.createDataBase();
} catch (IOException e) {
}
}
public List<Bean> getAllState(Context context, int year_code, String week) {
Cursor cursor = null;
List<Bean> models = new ArrayList<Bean>();
if (eventoConexion.checkDataBase()) {
eventoConexion.openDataBase();
SQLiteDatabase db = eventoConexion.getWritableDatabase();
cursor = db.rawQuery("SELECT rowid,State FROM sam where year_code = " + year_code + " and week=+'"+week+"' group by State", null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Bean t = new Bean();
t.setState(cursor.getString(cursor.getColumnIndex("State")));
t.setRowid(cursor.getInt(cursor.getColumnIndex("rowid")));
// adding to tags list
models.add(t);
}
while (cursor.moveToNext());
} else {
}
eventoConexion.close();
} else {
Log.e("Sms", "Cursor null");
}
}
return models;
}
MainActivity.java
public class MainActivity extends ActionBarActivity { DataController dataController; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dataController = new DataController(this); Log.e("Massege", "SuccessFully Run OnCreate"); dataController.createDb(); }
}
Finally copy your database now enjoying.......
No comments:
Post a Comment