gpt4 book ai didi

android没有这样的表错误

转载 作者:行者123 更新时间:2023-11-29 13:59:20 24 4
gpt4 key购买 nike

我在创建数据库类时遇到了没有这样的表错误。 logcat 说没有这样的表。我一直在尝试通过多种方式解决,但不幸的是仍然出现错误。以下是代码,错误是

android.database.sqlite.SQLiteException: no such table: tbl_category: , while compiling: SELECT * FROM tbl_category WHERE idx = 1

public class DatabaseManager extends SQLiteOpenHelper {
private SQLiteDatabase db;
private static Context myContext;
public static final String DB_NAME = "ykko.db";

public static final String name = "name";
public static String DB_PATH = "/data/data/com.bit.ykko/databases/";

public DatabaseManager(Context context) {
super(context, DB_NAME, null, 1);
DatabaseManager.myContext = context;
}

public void createDataBase() throws IOException {
boolean dbExit = checkDataBase();

if (dbExit) {

} else {
this.getReadableDatabase().getPath();

try {

copyDataBase();

} catch (IOException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
}
}
}

private boolean checkDataBase() {
SQLiteDatabase checkDb = null;

try {
String myPath = DB_PATH + DB_NAME;
Log.v("Path", myPath);
checkDb = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {

}

if (checkDb != null) {
checkDb.close();
}
return checkDb != null ? true : false;
}

public void copyDataBase() throws IOException {

InputStream databaseInput = null;
String outFileName = DB_PATH + DB_NAME;
OutputStream databaseOutput = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
int length;

databaseInput = myContext.getAssets().open("ykko.sqlite");
while ((length = databaseInput.read(buffer)) > 0) {
databaseOutput.write(buffer, 0, length);
databaseOutput.flush();
}
databaseInput.close();

databaseOutput.flush();
databaseOutput.close();
}

public void openDatabase() throws SQLiteException {

String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);

}

public ArrayList<ChineseInfo> getCatByIdx(int idx) {
ArrayList<ChineseInfo> list = new ArrayList<ChineseInfo>();

String query = "SELECT * FROM tbl_category WHERE idx = " + idx;

Cursor c = this.db.rawQuery(query, null);

if (c.moveToFirst()) {
do {
ChineseInfo ch = new ChineseInfo();
ch.idx = c.getInt(0);
ch.name = c.getString(1);
ch.mm_name = c.getString(2);
list.add(ch);

} while (c.moveToNext());
}
if (c != null && !c.isClosed()) {
c.close();
}
return list;
}

@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

}

最佳答案

为什么要在内存中创建数据库然后复制粘贴呢?如果你想在 sd 卡上有一个数据库,只需从一开始就在 sd 卡中创建一个数据库:

File sdpath = Environment.getExternalStorageDirectory();
String dbPath = sdpath.getAbsolutePath() + "/my_dir/mydb.sqlitedb";
SQLiteDatabase database = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);

希望对你有帮助。

关于android没有这样的表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10296442/

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