gpt4 book ai didi

java - “没有这样的表”SqliteDatabase

转载 作者:行者123 更新时间:2023-12-02 11:04:25 24 4
gpt4 key购买 nike

我总是收到此错误。我的数据库是我自己创建的。我的意思是,我不是用代码创建它的。它从 Assets 文件夹创建它,如果我通过手机上的 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;
}

更新(帮助程序代码)

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;
<小时/>

更新

一般建议:将 logmeseages 添加到您的代码中,以便您可以了解正在发生的情况。

关于您的代码的一些想法:

} catch (SQLiteException e) {
if (checkDB != null) {
checkDB.close();
}
}

可能您想在此处设置 chechDB = null,因为数据库无法打开并且已损坏(需要重新复制)。

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);

关于java - “没有这样的表”SqliteDatabase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51095282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com