Use sqlite database for iOS (robovm) with libgdx

后端 未结 2 920
轻奢々
轻奢々 2021-01-03 02:58

I have a libgdx project (Android) where I use a sqlite database. I\'m developing the same project in iOS version (robovm)

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

    There no docs (at least I am aware of) on how to use SQLite on RoboVM. Following links can be useful though:

    1. https://groups.google.com/forum/#!topic/robovm/5KF_nQJqHR8
    2. Does Android Support JDBC
    0 讨论(0)
  • 2021-01-03 03:28

    I know this is an old thread but this worked for me

    You dont have to do it EXACTLY like this of course, but this is how i did it

    First i made a helper class for SQLite called.. SQLiteHelper This would initialize things

    package com.hamzahrmalik.mitto.sqlite;
    
    import java.io.File;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import SQLite.JDBCDriver;
    
    import com.hamzahrmalik.mitto.utils.Util;
    
    public class SQLiteHelper {
    
        private String DATABASE_NAME;
        private JDBCDriver driv;
        private Connection connection;
    
        final String DB_PATH = new File(System.getenv("HOME"), "Library/")
                .getAbsolutePath();
    
        public SQLiteHelper(String DATABASE_NAME) {
            this.DATABASE_NAME = DATABASE_NAME;
            driv = new JDBCDriver();
            try {
                connection = driv.connect("sqlite:/" + DB_PATH + this.DATABASE_NAME, null);
            } catch (SQLException e) {
            }
        }
    
        public void execUpdate(String sql) {
            Util.log("Running SQL " + sql);
            try {
                Statement statement = connection.createStatement();
                statement.executeUpdate(sql);
                //connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public ResultSet execQuery(String query) {
            Util.log("Running SQL query " + query);
            try {
                Statement statement = connection.createStatement();
                ResultSet rs = statement.executeQuery(query);
                return rs;
            } catch (SQLException e) {
                return null;
            }
        }
    
    }
    

    Then each database extends this class For example, i have a database of Messages

    I make a SQLiteMessages class which extends my Helper class i made

    Then in the constructor i make the table

    final String CREATE_MESSAGES_TABLE = "CREATE TABLE IF NOT EXISTS "
                    + this.phonenum + "(" + KEY_ID
                    + " INTEGER PRIMARY KEY NOT NULL," + KEY_MESSAGE + " TEXT,"
                    + KEY_FROM + " TEXT," + KEY_TO + " TEXT," + KEY_DATE + " TEXT,"
                    + KEY_STATUS + " INTEGER)";
    
            execUpdate(CREATE_MESSAGES_TABLE);
    

    Then when i can add and delete and update as normal in SQL

    public void addMessage(Message m) {
            final String ADD_MESSAGES = "INSERT into " + phonenum + " VALUES('"
                    + m.getId() + "','" + m.getMessage() + "', '" + m.getSender()
                    + "', '" + m.getRecipient() + "', '"
                    + Util.formatDate(m.getTime()) + "', '" + m.getStatus()
                    + "')";
            execUpdate(ADD_MESSAGES);
        }
    

    Obviously you need to have a Message object but thats not really related to question

    To retrive data simply:

    public Message getMessage(int id) {
            ResultSet rs = execQuery("SELECT * FROM " + phonenum + " WHERE "
                    + KEY_ID + " = " + String.valueOf(id));
            try {
                if (rs.next()) {
                    Message m = messageFromResult(rs);
                    return m;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    public Message messageFromResult(ResultSet rs) {
            try {
                return new Message(rs.getInt(KEY_ID), rs.getString(KEY_MESSAGE),
                        rs.getString(KEY_FROM), rs.getString(KEY_TO),
                        Util.parseDate(rs.getString(KEY_DATE)),
                        rs.getInt(KEY_STATUS));
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
    

    No external libraries are needed for this Remember to add this to your robovm.xml

    <forceLinkClasses>
        <pattern>SQLite.**</pattern>
    </forceLinkClasses>
    
    0 讨论(0)
提交回复
热议问题