问题描述
我有一个 libgdx 项目 (Android),我在其中使用 sqlite 数据库.我正在 iOS 版本 (robovm) 中开发相同的项目,但我找不到任何关于 iOS 版本的 sqlite 或数据库的信息.
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.
是否可以使用同一个sqlite数据库?
Is it possible to use the same sqlite database ?
谢谢.
推荐答案
我知道这是一个旧线程,但这对我有用
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
首先我为 SQLite 创建了一个辅助类,名为.. SQLiteHelper这将初始化事物
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
我创建了一个 SQLiteMessages 类,它扩展了我创建的 Helper 类
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);
那么当我可以在 SQL 中正常添加、删除和更新时
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);
}
显然您需要有一个 Message 对象,但这与问题无关
Obviously you need to have a Message object but thats not really related to question
简单地检索数据:
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;
}
}
不需要任何外部库请记住将其添加到您的 robovm.xml
No external libraries are needed for this Remember to add this to your robovm.xml
<forceLinkClasses>
<pattern>SQLite.**</pattern>
</forceLinkClasses>
这篇关于使用带有 libgdx 的 iOS 的 sqlite 数据库(robovm)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!