我正在開發一個遊戲,其中包含我想要存儲在Android上的SQLite數據庫中的多個世界和級別(每個都有其各自的數據)。我在一些非常基本的應用程序中實現了SQLite,但到目前爲止,我無法將它與libgdx框架配合使用。在libgdx中使用SQLite
我實際上做了什麼,我維護一個Assets類,它有一些靜態成員用於別處的類。我有一個私有靜態嵌套類,名稱爲SQLHelper,它維護查詢字符串和一些私有方法,以便在Android手機/模擬器上運行應用程序時處理SQLite數據庫。
下面是代碼的一瞥:
public class Assets {
//A lot of code (unrelated to SQLHelper class) and to be used in classes elsewhere
SQLiteDatabase database;
//SQLHelper class
private static class SQLHelper {
private static String createTableSettings = "CREATE TABLE IF NOT EXISTS settings(settingID INTEGER PRIMARY KEY, settingName VARCHAR(15) NOT NULL, settingParamOne INTEGER NULL, settingParamTwo INTEGER NULL, settingParamThree INTEGER NULL);";
private static String createTableWorldData = "CREATE TABLE IF NOT EXISTS worldData(worldID INTEGER PRIMARY KEY, worldScore INTEGER DEFAULT 0, worldStars INTEGER DEFAULT 0);";
private static String createTableLevelData = "CREATE TABLE IF NOT EXISTS levelData(levelID INTEGER PRIMARY KEY, levelStars INTEGER DEFAULT 0, worldID INTEGER NOT NULL, FOREIGN KEY(worldID) REFERENCES worldData(worldID));";
@SuppressWarnings("static-access")
private static void openDatabase() {
try {
database.openDatabase("gameName", null, database.CREATE_IF_NECESSARY);
} catch (Exception e) {
Gdx.app.log("Exception in opening databases", "This should not happen on an Android Device (This is fine on java.)");
e.printStackTrace();
}
}
private static void createTables() {
try {
database.execSQL("PRAGMA foreign_keys=ON;");
database.execSQL(createTableSettings, null);
database.execSQL(createTableWorldData, null);
database.execSQL(createTableLevelData, null);
} catch (SQLException e) {
Gdx.app.log("Exception in createTables", "This should not happen on an Android Device (This is fine on java.)");
e.printStackTrace();
}
}
}
}
public static void load() {
myManager = new AssetManager();
SQLHelper.openDatabase();
SQLHelper.createTables();
loadTextures();
}
而且我得到這些類型由logcat的註冊異常:
02-14 22:45:53.261: E/SqliteDatabaseCpp(578): sqlite3_open_v2("gameName", &handle, 6, NULL) failed
Failed to open the database. Closing it.
02-14 22:45:53.291: E/SQLiteDatabase(578):
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
請幫我在這方面,我是新來的libgdx/SQLite的。
我寫了一個擴展,它抽象了所有這些實現細節,並提供了一種簡化的方式來同時處理Android和Desktop中的數據庫。它位於:https://github.com/mrafayaleem/gdx-sqlite – Rafay 2013-09-02 15:45:55