データベース クラスを作成しているときに、no such table エラーが発生してスタックしました。logcat によると、そのようなテーブルはありません。いくつかの方法で解決しようとしましたが、残念ながらまだエラーが発生しています。以下はコードで、エラーは
android.database.sqlite.SQLiteException: そのようなテーブルはありません: tbl_category: 、コンパイル中: 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
}
}