Libgdx

2013-04-08 7 views
15

में SQLite डेटाबेस का उपयोग करके मैं Libgdx में नया हूं और मुझे अपने गेम पर डेटाबेस का उपयोग करने में परेशानी हो रही है।Libgdx

मैंने लिबडक्स का उपयोग करके एंड्रॉइड और डेस्कटॉप एप्लिकेशन दोनों पर SQLite काम करने के तरीके पर एक ट्यूटोरियल की खोज की लेकिन मुझे एक आसान नहीं मिला।

पिछली बार जब मैंने एंड्रॉइड में डेटाबेस का उपयोग किया, तो मैंने एक कक्षा बनाई जो SQLiteOpenHelper से फैली हुई है।

क्या Libgdx का उपयोग करके ऐसा करने का कोई आसान तरीका है? या कम से कम, क्या कोई मुझे एक चरण-दर-चरण ट्यूटोरियल या कुछ समान बता सकता है?

संपादित

मैं कहना है कि मैं कुछ है कि मुझे SQLiteOpenHelper तरह संस्करणों का प्रबंधन करते हैं के लिए देख रहा हूँ भूल गया। दूसरे शब्दों में, मैं एपीके स्थापना पर एंड्रॉइड में अपना डेटाबेस फिर से बनाना चाहता हूं, जब मैं कोड पर अपने डीबी का संस्करण बदलता हूं।

समाधान

@42n4 जवाब के बाद, मैं डेस्कटॉप एप्लिकेशन पर Android पर आवेदन SQLiteOpenHelper और JDBC का उपयोग कर SQLite डेटाबेस से कनेक्ट करने के लिए कैसे कामयाब रहे।

//General class that needs to be implemented on Android and Desktop Applications 
public abstract class DataBase { 

    protected static String database_name="recycling_separation"; 
    protected static DataBase instance = null; 
    protected static int version=1; 

    //Runs a sql query like "create". 
    public abstract void execute(String sql); 

    //Identical to execute but returns the number of rows affected (useful for updates) 
    public abstract int executeUpdate(String sql); 

    //Runs a query and returns an Object with all the results of the query. [Result Interface is defined below] 
    public abstract Result query(String sql); 

    public void onCreate(){ 
     //Example of Highscore table code (You should change this for your own DB code creation) 
     execute("CREATE TABLE 'highscores' ('_id' INTEGER PRIMARY KEY NOT NULL , 'name' VARCHAR NOT NULL , 'score' INTEGER NOT NULL);"); 
     execute("INSERT INTO 'highscores'(name,score) values ('Cris',1234)"); 
     //Example of query to get DB data of Highscore table 
     Result q=query("SELECT * FROM 'highscores'"); 
     if (!q.isEmpty()){ 
      q.moveToNext(); 
      System.out.println("Highscore of "+q.getString(q.getColumnIndex("name"))+": "+q.getString(q.getColumnIndex("score"))); 
     } 
    } 

    public void onUpgrade(){ 
     //Example code (You should change this for your own DB code) 
     execute("DROP TABLE IF EXISTS 'highscores';"); 
     onCreate(); 
     System.out.println("DB Upgrade maded because I changed DataBase.version on code"); 
    } 

    //Interface to be implemented on both Android and Desktop Applications 
    public interface Result{ 
     public boolean isEmpty(); 
     public boolean moveToNext(); 
     public int getColumnIndex(String name); 
     public float getFloat(int columnIndex); 
     [...] 
    } 
} 

फिर, मैं डेस्कटॉप आवेदन के लिए एक DatabaseDesktop क्लास बनाया:

पहले, मैं दोनों डेस्कटॉप और Android अनुप्रयोगों के लिए एक "सामान्य वर्ग" बनाया

public class DatabaseDesktop extends DataBase{ 
    protected Connection db_connection; 
    protected Statement stmt; 
    protected boolean nodatabase=false; 

    public DatabaseDesktop() { 
     loadDatabase(); 
     if (isNewDatabase()){ 
      onCreate(); 
      upgradeVersion(); 
     } else if (isVersionDifferent()){ 
      onUpgrade(); 
      upgradeVersion(); 
     } 

    } 

    public void execute(String sql){ 
     try { 
      stmt.execute(sql); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 

    public int executeUpdate(String sql){ 
     try { 
      return stmt.executeUpdate(sql); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     return 0; 
    } 

    public Result query(String sql) { 
     try { 
      return new ResultDesktop(stmt.executeQuery(sql)); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    private void loadDatabase(){ 
     File file = new File (database_name+".db"); 
     if(!file.exists()) 
      nodatabase=true; 
     try { 
      Class.forName("org.sqlite.JDBC"); 
      db_connection = DriverManager.getConnection("jdbc:sqlite:"+database_name+".db"); 
      stmt = db_connection.createStatement(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void upgradeVersion() { 
     execute("PRAGMA user_version="+version); 
    } 

    private boolean isNewDatabase() { 
     return nodatabase; 
    } 

    private boolean isVersionDifferent(){ 
     Result q=query("PRAGMA user_version"); 
     if (!q.isEmpty()) 
      return (q.getInt(1)!=version); 
     else 
      return true; 
    } 

    public class ResultDesktop implements Result{ 

     ResultSet res; 
     boolean called_is_empty=false; 

     public ResultDesktop(ResultSet res) { 
      this.res = res; 
     } 

     public boolean isEmpty() { 
      try { 
       if (res.getRow()==0){ 
        called_is_empty=true; 
        return !res.next(); 
       } 
       return res.getRow()==0; 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
      return false; 
     } 

     public boolean moveToNext() { 
      try { 
       if (called_is_empty){ 
        called_is_empty=false; 
        return true; 
       } else 
        return res.next(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
      return false; 
     } 

     public int getColumnIndex(String name) { 
      try { 
       return res.findColumn(name); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
      return 0; 
     } 

     public float getFloat(int columnIndex) { 
      try { 
       return res.getFloat(columnIndex); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
      return 0; 
     } 

     [...] 

    } 

} 

और एक DatabaseAndroid Android के लिए आवेदन

public class DatabaseAndroid extends DataBase{ 
    protected SQLiteOpenHelper db_connection; 
    protected SQLiteDatabase stmt; 

    public DatabaseAndroid(Context context) { 
     db_connection = new AndroidDB(context, database_name, null, version); 
     stmt=db_connection.getWritableDatabase(); 
    } 

    public void execute(String sql){ 
     stmt.execSQL(sql); 
    } 

    public int executeUpdate(String sql){ 
     stmt.execSQL(sql); 
     SQLiteStatement tmp = stmt.compileStatement("SELECT CHANGES()"); 
     return (int) tmp.simpleQueryForLong(); 
    } 

    public Result query(String sql) { 
     ResultAndroid result=new ResultAndroid(stmt.rawQuery(sql,null)); 
     return result; 
    } 

    class AndroidDB extends SQLiteOpenHelper { 

     public AndroidDB(Context context, String name, CursorFactory factory, 
       int version) { 
      super(context, name, factory, version); 
     } 

     public void onCreate(SQLiteDatabase db) { 
      stmt=db; 
      DatabaseAndroid.this.onCreate(); 
     } 

     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      stmt=db; 
      DatabaseAndroid.this.onUpgrade(); 
     } 

    } 

    public class ResultAndroid implements Result{ 
     Cursor cursor; 

     public ResultAndroid(Cursor cursor) { 
      this.cursor=cursor; 
     } 

     public boolean isEmpty() { 
      return cursor.getCount()==0; 
     } 

     public int getColumnIndex(String name) { 
      return cursor.getColumnIndex(name); 
     } 

     public String[] getColumnNames() { 
      return cursor.getColumnNames(); 
     } 

     public float getFloat(int columnIndex) { 
      return cursor.getFloat(columnIndex); 
     } 

     [...] 

    } 

} 

अंत में, मैं चा nged दोनों एंड्रॉयड और डेस्कटॉप एप्लिकेशन के मुख्य क्लास:

public class Main extends AndroidApplication { 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     initialize(new MyGame(new DatabaseAndroid(this.getBaseContext())), false); 
    } 
} 

public class Main { 

    public static void main(String[] args) { 
     new LwjglApplication(new MyGame(new DatabaseDesktop()), "Example", MyGame.SCREEN_WIDTH, MyGame.SCREEN_HEIGHT,false); 
    } 

} 

ध्यान दें कि:

मैं एक है कि SQLiteOpenHelper में होता है PRAGMA user_version का उपयोग कर की तरह एक संस्करण प्रबंधन बनाया है। इस तरह, जब मैं इसे अपग्रेड करना चाहता हूं तो मैं DataBase कक्षा का संस्करण बदलता हूं।

मैंने Result पर किए गए सभी तरीकों को नहीं रखा है, लेकिन मैंने उन लोगों को रखा जो मुझे लगता है कि अधिक महत्वपूर्ण हैं। यह अधिक महत्वपूर्ण है।

उत्तर

3

http://marakana.com/techtv/android_bootcamp_screencast_series.html कक्षा 4, भाग 1: एंड्रॉयड Bootcamp - statusData, libgdx के लिए: http://code.google.com/p/libgdx-users/wiki/SQLite

संपादित करें: मैं Udacity पर libgdx गेम के बारे में के बारे में दो नए कोर्स का उल्लेख करना चाहिए: https://github.com/udacity/ud405

https://github.com/udacity/ud406

+0

तो, एंड्रॉइड और डेस्कटॉप दोनों के लिए, मैं 'स्टेटसडेटा' नामक कक्षा बना रहा हूं? एंड्रॉइड में, मैं 'SQLiteOpenHelper' का उपयोग कर सकता हूं जैसा कि मैंने पहले उपयोग किया था। लेकिन डेस्कटॉप एप्लिकेशन के बारे में क्या? उदाहरण के लिए, मैं डेस्कटॉप एप्लिकेशन को डीबी संस्करण को फिर से कैसे बना सकता हूं जब डीबी संस्करण 'StatusData' पर बदलता है? –

+0

शायद यह मदद करेगा: http://code.google.com/p/libgdx-users/wiki/SQLite – 42n4

+0

तो, बस स्पष्ट होने के लिए, मैं एंड्रॉइड पर 'SQLiteOpenHelper' का उपयोग कर सकता हूं और डेस्कटॉप में मैं वही कर सकता हूं 'जेडीबीसी' का उपयोग करने वाली चीज और उनमें से दोनों 'स्टेटसडेटा' नामक कक्षा पर बनाई गई हैं या कुछ इसी तरह की हैं। सही? –

6

एक एक्सटेंशन (जिसे जीडीएक्स-स्क्लाइट कहा जाता है) है जिसे मैंने लिखा है जो आपको आवश्यक अधिकांश काम करेगा। इस एक्सटेंशन का नवीनतम निर्माण here से डाउनलोड किया जा सकता है।स्रोत कोड और मुझे पढ़ें: https://github.com/mrafayaleem/gdx-sqlite

यह एक्सटेंशन वर्तमान में एंड्रॉइड और डेस्कटॉप प्लेटफॉर्म का समर्थन करता है। साथ ही, Android ऐप के संपत्ति फ़ोल्डर में स्थित डेटाबेस खोलने के लिए कोई समर्थन नहीं है। हालांकि, यह एक लंबित विशेषता है और जल्द ही जोड़ा जाएगा।

डाटाबेस हैंडलिंग के लिए अपनी परियोजनाओं को स्थापित करने के लिए मुझे पढ़ने में निर्देशों का पालन करें। निम्नलिखित उदाहरण कोड है:

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"); 
    } 
}