我将用 sqlite 编写一个示例代码,它必须同时适用于 Android 和 IOS(以及桌面)
这是我的build.gradle
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies { classpath 'org.javafxports:jfxmobile-plugin:1.0.6' }
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://oss.sonatype.org/content/repositories/releases' }
}
ext.CHARM_DOWN_VERSION = "1.0.0"
dependencies {
compile 'mysql:mysql-connector-java:3.1.12'
//compile 'org.xerial:sqlite-jdbc:3.9.0-SNAPSHOT'
compile 'org.sqldroid:sqldroid:1.0.3'
compile "com.gluonhq:charm-down-common:$CHARM_DOWN_VERSION"
desktopRuntime "com.gluonhq:charm-down-desktop:$CHARM_DOWN_VERSION"
//desktopRuntime 'org.xerial:sqlite-jdbc:3.9.0-SNAPSHOT'
androidRuntime "com.gluonhq:charm-down-android:$CHARM_DOWN_VERSION"
androidRuntime 'org.sqldroid:sqldroid:1.0.3'
iosRuntime "com.gluonhq:charm-down-ios:$CHARM_DOWN_VERSION"
}
mainClassName = 'com.version17.Version17'
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
packagingOptions {
exclude 'META-INF/INDEX.LIST'
}
}
ios {
infoPList = file('src/ios/Default-Info.plist')
forceLinkClasses = ['com.version17.**.*', 'com.mysql.**.*', 'SQLite.**.*', 'com.gluonhq.**.*']
}
}
sqliteHelper.java
public static void testSqli() throws SQLException, ClassNotFoundException{
Class.forName("SQLite.JDBCDriver");
String dbName = "mtt8.db";
File dir = null;
try {
dir = PlatformFactory.getPlatform().getPrivateStorage();
} catch (IOException e) {
e.printStackTrace();
}
File db = new File (dir, dbName);
String dbUrl = "jdbc:sqlite:" + db.getAbsolutePath();
Connection conn = DriverManager.getConnection(dbUrl);
//create table
Statement st=null;
st = conn.createStatement();
st.executeUpdate("DROP TABLE IF EXISTS village;");
st.executeUpdate("CREATE table village (id int, name varchar(20))");
//insert row
for (int i=0; i<50; i++){
st.executeUpdate("INSERT INTO village VALUES (" +i+ ", 'Erkan Kaplan')");
}
//select
String query = "SELECT id, name from village";
ResultSet rs = null;
rs = st.executeQuery(query);
while(rs.next()) {
int id = 0;
id = rs.getInt(1);
String name = null;
name = rs.getString(2);
System.out.println("id:"+ id+ ", name: "+ name);
st.executeUpdate("DELETE from village");
rs.close();
}
}
这适用于 Ipad 设备和桌面,但不适用于 Android 设备(如三星平板电脑)。
谁能告诉我为什么上面的代码不适用于三星平板电脑?或者我必须在我的代码中添加哪个?
感谢 Erkan Kaplan