This question already has an answer here:
I have one quotes application with local database from assets. I am not developer of android,but I know basic modification only. My some of user have told me that they are unable to install application after update....Many user have not any issue. I have also checked on my 3 device but never faced any issue. But some user have reported that they can not use application because whenever they try to open application they get crashed. I think there some wrong in my database helper class. I have attached it here....Please look and let me know if you find any bug in it.
public class DataBaseHandler extends SQLiteOpenHelper {
private static String DB_PATH;
private static String DB_NAME = "TestData";
private SQLiteDatabase myDataBase;
private static int DATABASE_VERSION = 9;
private final Context myContext;
public DataBaseHandler(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
this.myContext = context;
DB_PATH = context.getDatabasePath(DB_NAME).toString();
Log.e("path", DB_PATH);
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(dbExist)
{
if(DATABASE_VERSION == 1)
{
try {
copyDataBase();
DATABASE_VERSION = 9;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 2)
{
try {
copyDataBase();
DATABASE_VERSION = 9;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 3)
{
try {
copyDataBase();
DATABASE_VERSION = 9;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 4)
{
try {
copyDataBase();
DATABASE_VERSION = 9;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 5)
{
try {
copyDataBase();
DATABASE_VERSION = 9;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 6)
{
try {
copyDataBase();
DATABASE_VERSION = 9;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 7)
{
try {
copyDataBase();
DATABASE_VERSION = 9;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else if (DATABASE_VERSION == 8)
{
try {
copyDataBase();
DATABASE_VERSION = 9;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
else
{
SQLiteDatabase database = null;
database = this.getWritableDatabase();
String query_count = "SELECT version FROM users";
Cursor c_count = database.rawQuery(query_count, null);
c_count.moveToFirst();
Integer count = c_count.getInt(c_count.getColumnIndex("version"));
if(count == DATABASE_VERSION)
{
}else
{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
}else
{
this.getReadableDatabase();
try {
copyDataBase();
DATABASE_VERSION = 9;
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH;
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 = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH;
// 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 void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
// ==============================================================================
@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) {
}
}
Aucun commentaire:
Enregistrer un commentaire