Use sqlite database for iOS (robovm) with libgdx
Asked Answered
B

2

6

I have a libgdx project (Android) where I use a sqlite database. I'm developing the same project in iOS version (robovm) and I can't find anything about sqlite or database for iOS version.

Is it possible to use the same sqlite database ?

Thanks.

Butcher answered 17/6, 2014 at 14:54 Comment(2)
The iOS way of doing this is to use the CoreData APIs which use Sqlite internally.Sanguineous
You can use Sqlite directly but it's not an Apple API, do you would need to go to their website sqlite.org and download the needed files.Sanguineous
H
5

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>
Helpmate answered 21/2, 2015 at 20:48 Comment(9)
Did you ever post the complete code mate, I am new to LibGDXHageman
Yes its above.. If you need help with something specific, just askHelpmate
Hey dude, trying to build this now. What is this.phonenum. Also could you paste your declaration for KEY_ID, etc and m.getId()Hageman
Hey dude, trying to build this now. What is formatDate code? I would love to see your Utils. My messageFromResult wont compile either I think thats cause Message does not have a constructore. What gradle imports do I need? I get Error:(25, 20) Gradle: error: cannot find symbol class JDBCDriverHageman
@Hageman m.getID() is specific to my Message object which is what is stored in my database. Until.format date() simply gets a string from a Date object using SimpleDataFormatHelpmate
I could not get mine to build due to it not finding the JDBCDriverHageman
@Hageman i believe it is a jar you need to download and add to your buildHelpmate
What was the jar called and where did you put it? In each projectHageman
Dont' forget to add <pattern>SQLite.**</pattern> to robovm.xml 's forceLinkClassesTitoism
P
2

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
Pallet answered 8/10, 2014 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.