Hey i'm creating an app which caching it's data on sqllite Database but i don't want to create my database programmatically i have created the database using SQLLite Browser and then i imported it into my assets folder but each time i try to perform any operation on this database i'm getting no such table exception.
I know there are many question about this problem but non of them helped me.
this is my SQLiteOpenHelper
public class MySQLiteHelper extends SQLiteOpenHelper {
public SQLiteDatabase myDataBase;
private final Context myContext;
private static final String DATABASE_NAME = "VadDB";
public static String DATABASE_PATH = "";
public static final int DATABASE_VERSION = 2;
// public static final int DATABASE_VERSION_old = 1;
// Constructor
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
if(android.os.Build.VERSION.SDK_INT >= 17){
DATABASE_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
else
{
DATABASE_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
this.myContext = context;
}
// Create a empty database on the system
public void createDataBase() throws IOException {
// boolean dbExist = checkDataBase();
//
// if (dbExist) {
// Log.v("DB Exists", "db exists");
// // By calling this method here onUpgrade will be called on a
// // writeable database, but only if the version number has been
// // bumped
// // onUpgrade(myDataBase, DATABASE_VERSION_old, DATABASE_VERSION);
// }
boolean dbExist1 = checkDataBase();
if (!dbExist1) {
this.getReadableDatabase();
try {
this.close();
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
// Check database already exist or not
private boolean checkDataBase() {
boolean checkDB = false;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
File dbfile = new File(myPath);
checkDB = dbfile.exists();
} catch (SQLiteException e) {
}
return checkDB;
}
// Copies your database from your local assets-folder to the just created
// empty database in the system folder
private void copyDataBase() throws IOException {
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
// delete database
public void db_delete() {
File file = new File(DATABASE_PATH + DATABASE_NAME);
if (file.exists()) {
file.delete();
}
}
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy) {
return myDataBase.query("data_table", null, null, null, null, null,
null);
}
public Cursor qureyDatabase(String sql) {
Cursor cursor = myDataBase.rawQuery(sql, null);
return cursor;
}
// Open database
public void openDataBase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void closeDataBase() throws SQLException {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
db_delete();
}
}
And this is how i use it
MySQLiteHelper helper=new MySQLiteHelper(context);
SQLiteDatabase db1 = helper.getWritableDatabase();
System.out.println(db1.getPath());
for(int i=0;i<faqlist.size();i++){
ContentValues values = new ContentValues();
values.put("ModuleId", faqlist.get(i).getModuleId());
values.put("Title",faqlist.get(i).getTitle() );
values.put("Category", faqlist.get(i).getCategory());
values.put("CetegoryId", faqlist.get(i).getCategoryId());
values.put("Question", faqlist.get(i).getQuestion());
values.put("Answer", faqlist.get(i).getAnswer());
db1.insert("Faq1", null, values);
}
with no success.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire