[英]'No Such Table' SqliteDatabase
我总是会收到此错误。 我的数据库是我自己创建的。 我不是用我的意思创建代码的。 它是从资产文件夹创建的,如果我通过手机上的Sqlite阅读器应用程序将其打开,它将完美地显示表格和数据。 但是,我无法在项目中读取此数据库。 它创建数据库,但无法从应用程序读取。 我的连接类是.aar文件,并将其添加到gradle中。 这是我的助手代码:
public class Connection extends SQLiteOpenHelper {
private static String _dbPath;
private static String _dbName;
private final Context _context;
public Connection(Context context, int dbVer, String dbPath, String dbName) {
super(context, dbName, null, dbVer);
this._context = context;
_dbPath = dbPath;
_dbName = dbName;
CreateDatabase();
}
public void CreateDatabase() {
boolean dbExist = CheckDatabase();
if (!dbExist) {
this.getReadableDatabase();
try {
CopyDatabase();
} catch (IOException e) {
throw new Error("Error.");
}
}
}
private boolean CheckDatabase() {
SQLiteDatabase checkDB = null;
try {
String myPath = _dbPath + "/" + _dbName;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
if (checkDB != null) {
checkDB.close();
}
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void CopyDatabase() throws IOException {
InputStream myInput = _context.getAssets().open(_dbName);
String outFileName = _dbPath + "/" + _dbName;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}}
这是我的访问代码:
public static Cursor Select(Context context, Select select, ArrayList<Where> whereList) {
String dbPath, dbName;
Integer dbVer;
SQLiteDatabase database;
Cursor cursor;
dbVer = (Integer) TDTools.ReturnResource(context, R.integer.dbVer, TDTools.ResType.Integer);
dbName = (String) TDTools.ReturnResource(context, R.string.dbName, TDTools.ResType.String);
dbPath = Environment.getExternalStorageDirectory() + (String) TDTools.ReturnResource(context, R.string.dbPath, TDTools.ResType.String);
Connection con = new Connection(context, dbVer, dbPath, dbName);
database = con.getReadableDatabase();
WhereArgs whereArgs = WhereArgs.CreateArguments(whereList);
if (select == null) {
select = new Select();
}
//Here is where i get 'No Such Table' error...
cursor = database.query("Category", select.getColumns(), whereArgs.getWhereClauses(), whereArgs.getArguments(), null, null, null, null);
return cursor;
}
UPDATE (帮助程序代码)
public class Connection extends SQLiteOpenHelper {
public static SQLiteDatabase DataBase;
private String _dbPath;
private String _dbName;
private final Context _context;
public Connection(Context context, int dbVer, String dbPath, String dbName) {
super(context, dbName, null, dbVer);
this._context = context;
this._dbPath = dbPath;
this._dbName = dbName;
CreateDatabase();
}
public void CreateDatabase() {
boolean dbExist = CheckDatabase();
if (!dbExist) {
//this.getReadableDatabase();
try {
CopyDatabase();
} catch (IOException e) {
throw new Error("Veritabanı kopyalanamadı...");
}
}
}
private boolean CheckDatabase() {
SQLiteDatabase checkDB = null;
try {
String myPath = this._dbPath + "/" + this._dbName;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
checkDB = null;
}
return checkDB != null ? true : false;
}
private void CopyDatabase() throws IOException {
InputStream myInput = this._context.getAssets().open(this._dbName);
String outFileName = this._dbPath + "/" + this._dbName;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
尝试使用大写的表名“ CATEGORY”。 如果这不起作用,请查看您是否真的在打开创建的数据库或只是打开了一个新数据库。
您可以使用以下命令检查数据库的示例:
SELECT name, sql FROM sqlite_master WHERE type='table' ORDER BY name;
UPDATE
一般建议:将logmesseages添加到您的代码中,以便您可以了解正在发生的情况。
关于您的代码的一些想法:
} catch (SQLiteException e) {
if (checkDB != null) {
checkDB.close();
}
}
可能您想在这里设置chechDB = null,因为to数据库无法打开且已损坏(需要重新复制)。
if (!dbExist) {
this.getReadableDatabase();
try {
CopyDatabase();
} catch (IOException e) {
throw new Error("Error.");
}
}
为什么在复制数据库之前调用this.getReadableDatabase()
?
更新2
您调用super(context, dbName, null, dbVer);
这意味着数据库文件“ dbName”已打开。 但是您将数据库复制到this._dbPath + "/" + this._dbName
! 您必须使用相同的路径。
super(context, dbPath + "/" + dbName, null, dbVer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.