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.
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>
There no docs (at least I am aware of) on how to use SQLite on RoboVM. Following links can be useful though:
来源:https://stackoverflow.com/questions/24267005/use-sqlite-database-for-ios-robovm-with-libgdx