I am creating an Android Application and I am using sqlite database in it. for that I have placed a sqlite file in assets folder of project and I am copying this file to pho
After a long tries and searches I forced to assume that there should be a bug in OP2 manufacturing as it is working fine in all other devices.
and I changed my approach to create database using query rather than copying database file from assets. like code below
import java.io.File;
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;
public class MySQLiteHelper extends SQLiteOpenHelper implements DBConstants {
private static MySQLiteHelper mInstance = null;
private SQLiteDatabase myDataBase;
private static String DB_PATH = "";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
try {
if (checkDataBase())
openDataBase();
else
myDataBase = this.getReadableDatabase();
} catch (Exception e) {
}
}
public static MySQLiteHelper instance(Context context) {
File outFile = context.getDatabasePath(DATABASE_NAME);
DB_PATH = outFile.getPath();
if (mInstance == null) {
mInstance = new MySQLiteHelper(context);
}
return mInstance;
}
private void openDataBase() throws Exception {
try {
myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLException e) {
// TODO: handle exception
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_PATH, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
/** database does't exist yet. */
} catch (Exception e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v("log_tag", "onCreate");
myDataBase = db;
// creating a sample table
String CREATE_DEVICE_TABLE = "CREATE TABLE " + DEVICE + " ("
+ KEY_DEVICEID + " TEXT, " + KEY_OPERATOR + " TEXT, "
+ KEY_DEVICENAME + " TEXT, " + KEY_DEVICETOTALMEMORY
+ " INTEGER, " + KEY_SCREENWIDTH + " INTEGER, "
+ KEY_SCREENHEIGHT + " INTEGER, " + KEY_OPERATINGSYSTEM
+ " TEXT)";
db.execSQL(CREATE_DEVICE_TABLE);
// other tables also can be created from here.
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DEVICE);
// Create tables again (as per requirement)
this.onCreate(db);
}
public Cursor rawQuery(String qry) {
return myDataBase.rawQuery(qry, null);
}
public long insert(String tableName, ContentValues cv) {
return myDataBase.insert(tableName, null, cv);
}
public void insertWithOnConflict(String tableName, ContentValues cv,
int flag) {
myDataBase.insertWithOnConflict(tableName, null, cv, flag);
}
public long update(String tableName, ContentValues cv, String whereClose) {
return myDataBase.update(tableName, cv, whereClose, null);
}
public int deleteData(String table_name, String whereClause) {
return (int) myDataBase.delete(table_name, whereClause, null);
}
}
and it worked for me
Thanks!
I have found solution for this, I have used below function :
public void createDb() {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// call close() for properly copy database file
this.getReadableDatabase().close();
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Error copying database");
}
}
}
As per this post we need to call close()
and you can push database to Oneplus two devices also.
Database path may be different in different devices.
You need to use Context.getDatabasePath(String)
in order to get database path.
e.g.
File backupDB = context.getDatabasePath(backupDBPath);