Using a SQLite database in Libgdx

后端 未结 2 1398
半阙折子戏
半阙折子戏 2021-01-30 09:45

I\'m new in Libgdx and I\'m getting trouble on using a database on my game.

I searched for a tutorial on how to make SQLite work on both Android and Desktop applications

相关标签:
2条回答
  • 2021-01-30 10:03

    There is an extension (called gdx-sqlite) that I wrote which will do most of the work you require. Latest build of this extension can be downloaded from here. The source code and read me are located at: https://github.com/mrafayaleem/gdx-sqlite

    This extension currently supports Android and Desktop platforms. Also, there is no support to open databases located in the assets folder of the Android app. However, this is a pending feature and will be added soon.

    Follow the instructions in read me to setup your projects for database handling. Following is an example code:

    package com.mrafayaleem.gdxsqlitetest;
    
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.sql.Database;
    import com.badlogic.gdx.sql.DatabaseCursor;
    import com.badlogic.gdx.sql.DatabaseFactory;
    import com.badlogic.gdx.sql.SQLiteGdxException;
    
    public class DatabaseTest {
    
        Database dbHandler;
    
        public static final String TABLE_COMMENTS = "comments";
        public static final String COLUMN_ID = "_id";
        public static final String COLUMN_COMMENT = "comment";
    
        private static final String DATABASE_NAME = "comments.db";
        private static final int DATABASE_VERSION = 1;
    
        // Database creation sql statement
        private static final String DATABASE_CREATE = "create table if not exists "
                + TABLE_COMMENTS + "(" + COLUMN_ID
                + " integer primary key autoincrement, " + COLUMN_COMMENT
                + " text not null);";
    
        public DatabaseTest() {
            Gdx.app.log("DatabaseTest", "creation started");
            dbHandler = DatabaseFactory.getNewDatabase(DATABASE_NAME,
                    DATABASE_VERSION, DATABASE_CREATE, null);
    
            dbHandler.setupDatabase();
            try {
                dbHandler.openOrCreateDatabase();
                dbHandler.execSQL(DATABASE_CREATE);
            } catch (SQLiteGdxException e) {
                e.printStackTrace();
            }
    
            Gdx.app.log("DatabaseTest", "created successfully");
    
            try {
                dbHandler
                        .execSQL("INSERT INTO comments ('comment') VALUES ('This is a test comment')");
            } catch (SQLiteGdxException e) {
                e.printStackTrace();
            }
    
            DatabaseCursor cursor = null;
    
            try {
                cursor = dbHandler.rawQuery("SELECT * FROM comments");
            } catch (SQLiteGdxException e) {
                e.printStackTrace();
            }
            while (cursor.next()) {
                Gdx.app.log("FromDb", String.valueOf(cursor.getString(1)));
            }
    
            try {
                dbHandler.closeDatabase();
            } catch (SQLiteGdxException e) {
                e.printStackTrace();
            }
            dbHandler = null;
            Gdx.app.log("DatabaseTest", "dispose");
        }
    }
    
    0 讨论(0)
  • 2021-01-30 10:16

    http://marakana.com/techtv/android_bootcamp_screencast_series.html Class 4, Part 1: Android Bootcamp - statusData, for libgdx: http://code.google.com/p/libgdx-users/wiki/SQLite

    EDIT: I should mention about two new courses about libgdx games at Udacity: https://github.com/udacity/ud405

    https://github.com/udacity/ud406

    0 讨论(0)
提交回复
热议问题