[英]“no such table” issue in android on some devices
I am using an external database in android app and it is working fine on all emulators and on samsung real devices. 我在Android应用程序中使用外部数据库,它在所有模拟器和三星真实设备上运行良好。 But when I am checking on the Acer smart phone, my application going crash with following exception: 但是当我检查Acer智能手机时,我的应用程序崩溃了以下异常:
android.database.sqlite.SQLiteException: no such table:
It is strange to me. 这对我来说很奇怪。 I have checked under the data folder in file explorer where my databases and all tables are present. 我检查了文件资源管理器中的数据文件夹,其中存在我的数据库和所有表。
I am not getting why it is happening. 我不知道为什么会发生这种情况。
Please guide me. 请指导我。 Thanks in advance 提前致谢
Code of sql helper class is as following: sql helper类的代码如下:
public class MyDatabaseHelper extends SQLiteOpenHelper {
// System path of application database.
private static String DB_PATH = MyApplication.getAppContext()
.getFilesDir().getParentFile().getPath()
+ "/databases/";
private static String DB_NAME = "myDB";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public MyDatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with own database.
*
*/
public void createDataBase() throws IOException {
checkDataBase();
// Creates empty database default system path
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
/**
* Checks if the database already exist to avoid re-copying the file each
* time whenever the application opened.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies database from local assets-folder to the just created empty
* database in the system folder and from where it can be accessed and
* handled using byte stream transferring.
*
*/
private void copyDataBase() throws IOException {
// Open local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME + ".db");
// Path of 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 input file to the output file
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 SQLiteDatabase openDataBase() throws SQLException {
// Opens the database
String myPath = DB_PATH + DB_NAME;
return SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
Finally I resolved my question. 最后我解决了我的问题。
I just get readable database into the database and close it just after open it again. 我只是将可读数据库放入数据库并在再次打开后关闭它。 My complete code is as follows: 我的完整代码如下:
public class MyDatabaseHelper extends SQLiteOpenHelper {
// System path of application database.
private static String DB_PATH = MyApplication.getAppContext()
.getFilesDir().getParentFile().getPath()
+ "/databases/";
private static String DB_NAME = "myDB";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public MyDatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with own database.
*
*/
public void createDataBase() throws IOException {
checkDataBase();
SQLiteDatabase db_Read = null;
// Creates empty database default system path
db_Read = this.getReadableDatabase();
db_Read.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
/**
* Checks if the database already exist to avoid re-copying the file each
* time whenever the application opened.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies database from local assets-folder to the just created empty
* database in the system folder and from where it can be accessed and
* handled using byte stream transferring.
*
*/
private void copyDataBase() throws IOException {
// Open local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME + ".db");
// Path of 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 input file to the output file
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 SQLiteDatabase openDataBase() throws SQLException {
// Opens the database
String myPath = DB_PATH + DB_NAME;
return SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
Probably you have hardcoded the path to the database. 可能你已经硬编码了数据库的路径。 For different devices it may be different. 对于不同的设备,它可能是不同的。 Use Envirenment.getExternalStorageDirectory()
to create the path to your database. 使用Envirenment.getExternalStorageDirectory()
创建数据库的路径。
尝试使用此私有静态String DB_PATH =“/ data / data / your package name / databases /”;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.