I've had this trouble for multiple times now, and I still don't exactly understand why does it happen. I've searched on Google and read all the related posts here on StackOverflow, and tryed all the sugestions. None off them effectively solve my problem. I've tryed cleaning the project and uninstall the application from my phone, or even from my Android Virtual Device. Which, by the way, are the most common sugestions to solve this. When I removed the database from my phone, to see if there really isn't such table, I saw that my database only contained the "android_metadata" table. For the record, this application have already worked with this database, and have already had this bug too, which sincerely, solved by it self after a lot of research and failed attempts to solve it. Still, after all this times that I've had this problem, I still don't know exactly how to solve it when it come back. Anyone else have already had such a trouble with your android database? If yes, how did you solve it? I work with android and this kind of problem really delays my job. I've created my database in SQLiteStudio.
My database class:
public class DataBaseHelper extends SQLiteOpenHelper {
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat
// window
// destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME = "EasyGarcom";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 6);// 1? its Database Version
if (android.os.Build.VERSION.SDK_INT >= 17) {
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
} else {
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
this.mContext = context;
}
public void createDataBase() throws IOException {
// If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if (!mDataBaseExist) {
this.getReadableDatabase();
this.close();
try {
// Copy the database from assests
copyDataBase();
Log.d(TAG, "createDatabase database created");
} catch (IOException mIOException) {
throw new Error("ErrorCopyingDataBase");
}
}
}
// Check that the database exists here: /data/data/your package/databases/Da
// Name
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
// Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
// Copy the database from assets
private void copyDataBase() throws IOException {
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
// Open the database, so we can query it
public boolean openDataBase() throws SQLException {
String mPath = DB_PATH + DB_NAME;
// Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openDatabase(mPath, null,
SQLiteDatabase.CREATE_IF_NECESSARY);
// mDataBase = SQLiteDatabase.openDatabase(mPath, null,
// SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return mDataBase != null;
}
@Override
public synchronized void close() {
if (mDataBase != null)
mDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
The class I use to handle the database:
public class Controle {
public Controle(Context c) {
this.context = c;
}
Context context;
public void openDB() {
DataBaseHelper myDbHelper = new DataBaseHelper(context);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Não foi possivel criar o banco de dados");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
public void closeDB() {
DataBaseHelper myDbHelper = new DataBaseHelper(context);
try {
myDbHelper.close();
} catch (SQLException sqle) {
throw sqle;
}
}
public Cursor searchDB(String sql, String[] clausula) {
DataBaseHelper myDbHelper = new DataBaseHelper(context);
SQLiteDatabase db = myDbHelper.getWritableDatabase();
myDbHelper.openDataBase();
db = myDbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, clausula);
return cursor;
}
public void updateDB(String table, ContentValues values,
String whereClause, String[] whereArgs) {
DataBaseHelper myDbHelper = new DataBaseHelper(context);
SQLiteDatabase db = myDbHelper.getWritableDatabase();
myDbHelper.openDataBase();
db = myDbHelper.getReadableDatabase();
db.update(table, values, whereClause, whereArgs);
}
public void insert(String tableName, ContentValues values) {
DataBaseHelper myDbHelper = new DataBaseHelper(context);
SQLiteDatabase db = myDbHelper.getWritableDatabase();
// check if the thing is already inserted
String sql = "SELECT * FROM " + tableName + " WHERE _id LIKE ?";
String[] clausula = new String[] { values.getAsString("_id") };
Cursor c = searchDB(sql, clausula);
if (c.getCount() == 0) {// no rows returned
// insert row
// atualizado = true
db.insert(tableName, null, values);
//Log.d("Controle", "Inserted");
} else {
// update row
updateDB(tableName, values,
"_id = " + values.get("_id").toString(), null);
//Log.d("Controle", "Updated");
}
c.close();
}
}
Aucun commentaire:
Enregistrer un commentaire