• Announcements

    • Intermittent usage issues   08/31/2015

      If you are experiencing intermittent issues with using the website, do the following: Clear your browser cache.Clear your browser cookies, especially all those relating to this website.Login again and use as normal.If issues still persist, update your web browser to its latest version and try again.

Working with SQLite in libgdx


2 posts in this topic

Posted (edited)

I was wondering if there are any users who have worked with libgdx for developing games for the Android platform. I am relatively new to this framework and I am currently stuck with the following problem:

The question was originally posted by me here and is reproduced as is:

I am in the process of developing a game with multiple worlds and levels (each having their individual data) which I want to store in SQLite database on Android. I have implemented SQLite in some very basic apps but so far I am unable to get it working along side the libgdx framework.

What I have actually done is that I am maintaining an Assets class that has some static members for use in classes elsewhere. I have a private static nested class by the name of SQLHelper that maintains the query strings and some private methods to handle the SQLite database when the app runs on an Android phone/Emulator.

Here is a glimpse of the code:


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


}

And I am getting these type of exceptions registered by the 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

Please help me in this regard as I am new to libgdx/SQLite.

Edited by rafay18

Share this post


Link to post
Share on other sites

Posted

I havent used SQLite but I think you sm to have issue with the DB name and schema

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now