Javafxports 和 SQLite -> 打开失败:EPERM(不允许操作)
Javafxports and SQLite -> open failed: EPERM (Operation not permitted)
我正在尝试使用 javafxports 编写一个简单的 sqlite 代码。
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.6'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
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 'org.xerial:sqlite-jdbc:3.8.11'
compile "com.gluonhq:charm-down-common:$CHARM_DOWN_VERSION"
desktopRuntime "com.gluonhq:charm-down-desktop:$CHARM_DOWN_VERSION"
androidRuntime "com.gluonhq:charm-down-android:$CHARM_DOWN_VERSION"
iosRuntime "com.gluonhq:charm-down-ios:$CHARM_DOWN_VERSION"
}
mainClassName = 'com.gluonapplication.version16'
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
}
ios {
infoPList = file('src/ios/Default-Info.plist')
forceLinkClasses= ['com.gluonhq.**.*', 'org.sqlite.**.*']
}
}
我的Java代码:
public static Label msg = new Label();
@Override
public void start(Stage stage) {
StackPane root = new StackPane();
root.getChildren().add(msg);
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());
stage.getIcons().add(new Image(version16.class.getResourceAsStream("/icon.png")));
stage.setScene(scene);
stage.show();
try {
testSqli();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void testSqli() throws SQLException, ClassNotFoundException {
String driver = "org.sqlite.JDBC";
//Class.forName("SQLite.JDBCDriver");
Class.forName(driver);
String dbName = "mtt8.db";
String dbUrl = "jdbc:sqlite:" + dbName;
//create table
Statement st = null;
Connection conn = DriverManager.getConnection(dbUrl);
st = conn.createStatement();
st.executeUpdate("DROP TABLE IF EXISTS village;");
st.executeUpdate("CREATE table village (id int, name varchar(20))");
//insert row?
st.executeUpdate("INSERT INTO village VALUES (111, 'Concretepage')");
//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);
msg.setText("id:" + id + ", name: " + name);
System.out.println("id:" + id + ", name: " + name);
st.executeUpdate("DELETE from village");
rs.close();
}
}
我用 ./gradlew launchIOSDevice 发送它并收到以下错误:
java.sql.SQLException: opening db: 'mtt8.db': open failed: EPERM (Operation not permitted)
at org.sqlite.core.CoreConnection.open(CoreConnection.java:203)
at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:24)
at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:23)
at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
at org.sqlite.JDBC.createConnection(JDBC.java:114)
at org.sqlite.JDBC.connect(JDBC.java:88)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:144)
at com.gluonapplication.version16.testSqli(version16.java:48)
at com.gluonapplication.version16.start(version16.java:32)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda5.run(Unknown Source)
at java.security.AccessController.doPrivileged(AccessController.java:52)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
at org.robovm.apple.uikit.UIApplication.main(Native Method)
at org.robovm.apple.uikit.UIApplication.main(UIApplication.java:369)
at org.javafxports.jfxmobile.ios.BasicLauncher.main(BasicLauncher.java:115)
IOSWindowSystemInterface : setSwapInterval unimp
setSwapInterval(1)
谁能帮帮我,
我怎样才能在我的代码中提供一个访问权限,以便我可以创建一个 sqlite 数据库?
谢谢
埃尔坎·卡普兰
正如@ItachiUchiha 指出的那样,您的问题与您尝试创建数据库的位置有关:
String dbUrl = "jdbc:sqlite:" + dbName;
Connection conn = DriverManager.getConnection(dbUrl);
您提供的 URL 可以在您的桌面上运行,但不能在移动设备上运行,因为应用程序对存储的访问权限非常有限,并且它只授予对私有本地存储的访问权限.
使用 Gluon 的开源库 Charm-Down,无论应用 运行 在哪个平台上,都可以很容易地获取本地存储的路径。
首先,将这些依赖项添加到您的 build.gradle
脚本中:
ext.CHARM_DOWN_VERSION = "1.0.0"
dependencies {
compile "com.gluonhq:charm-down-common:$CHARM_DOWN_VERSION"
desktopRuntime "com.gluonhq:charm-down-desktop:$CHARM_DOWN_VERSION"
androidRuntime "com.gluonhq:charm-down-android:$CHARM_DOWN_VERSION"
iosRuntime "com.gluonhq:charm-down-ios:$CHARM_DOWN_VERSION"
}
现在,在您的代码中,URL 应该是:
try {
File dir = PlatformFactory.getPlatform().getPrivateStorage();
File db = new File (dir, dbName);
String dbUrl = "jdbc:sqlite:" + db.getAbsolutePath();
Connection conn = DriverManager.getConnection(dbUrl);
...
} catch (Exception e) { }
非常感谢何塞,
这是我的 build.gradle
,所以我可以连接到我局域网上的 mysql-服务器,结果在 Ipad-设备上保存为 sqlite.db;
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 "com.gluonhq:charm-down-common:$CHARM_DOWN_VERSION"
desktopRuntime "com.gluonhq:charm-down-desktop:$CHARM_DOWN_VERSION"
androidRuntime "com.gluonhq:charm-down-android:$CHARM_DOWN_VERSION"
iosRuntime "com.gluonhq:charm-down-ios:$CHARM_DOWN_VERSION"
}
mainClassName = 'com.mtt8.version15'
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
packagingOptions {
exclude 'META-INF/INDEX.LIST'
}
}
ios {
infoPList = file('src/ios/Default-Info.plist')
forceLinkClasses = ['com.mtt8.**.*', 'com.mysql.**.*', 'SQLite.**.*', 'com.gluonhq.**.*']
}
}
和sqlite.java
import com.gluonhq.charm.down.common.PlatformFactory;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Created by erkankaplan on 10.11.15.
*/
public class sqliteDBConnector {
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
st.executeUpdate("INSERT INTO village VALUES (111, 'Concretepage')");
//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();
}
}
}
我正在尝试使用 javafxports 编写一个简单的 sqlite 代码。
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.6'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
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 'org.xerial:sqlite-jdbc:3.8.11'
compile "com.gluonhq:charm-down-common:$CHARM_DOWN_VERSION"
desktopRuntime "com.gluonhq:charm-down-desktop:$CHARM_DOWN_VERSION"
androidRuntime "com.gluonhq:charm-down-android:$CHARM_DOWN_VERSION"
iosRuntime "com.gluonhq:charm-down-ios:$CHARM_DOWN_VERSION"
}
mainClassName = 'com.gluonapplication.version16'
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
}
ios {
infoPList = file('src/ios/Default-Info.plist')
forceLinkClasses= ['com.gluonhq.**.*', 'org.sqlite.**.*']
}
}
我的Java代码:
public static Label msg = new Label();
@Override
public void start(Stage stage) {
StackPane root = new StackPane();
root.getChildren().add(msg);
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());
stage.getIcons().add(new Image(version16.class.getResourceAsStream("/icon.png")));
stage.setScene(scene);
stage.show();
try {
testSqli();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void testSqli() throws SQLException, ClassNotFoundException {
String driver = "org.sqlite.JDBC";
//Class.forName("SQLite.JDBCDriver");
Class.forName(driver);
String dbName = "mtt8.db";
String dbUrl = "jdbc:sqlite:" + dbName;
//create table
Statement st = null;
Connection conn = DriverManager.getConnection(dbUrl);
st = conn.createStatement();
st.executeUpdate("DROP TABLE IF EXISTS village;");
st.executeUpdate("CREATE table village (id int, name varchar(20))");
//insert row?
st.executeUpdate("INSERT INTO village VALUES (111, 'Concretepage')");
//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);
msg.setText("id:" + id + ", name: " + name);
System.out.println("id:" + id + ", name: " + name);
st.executeUpdate("DELETE from village");
rs.close();
}
}
我用 ./gradlew launchIOSDevice 发送它并收到以下错误:
java.sql.SQLException: opening db: 'mtt8.db': open failed: EPERM (Operation not permitted)
at org.sqlite.core.CoreConnection.open(CoreConnection.java:203)
at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:24)
at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:23)
at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
at org.sqlite.JDBC.createConnection(JDBC.java:114)
at org.sqlite.JDBC.connect(JDBC.java:88)
at java.sql.DriverManager.getConnection(DriverManager.java:179)
at java.sql.DriverManager.getConnection(DriverManager.java:144)
at com.gluonapplication.version16.testSqli(version16.java:48)
at com.gluonapplication.version16.start(version16.java:32)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda5.run(Unknown Source)
at java.security.AccessController.doPrivileged(AccessController.java:52)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda.run(Unknown Source)
at org.robovm.apple.uikit.UIApplication.main(Native Method)
at org.robovm.apple.uikit.UIApplication.main(UIApplication.java:369)
at org.javafxports.jfxmobile.ios.BasicLauncher.main(BasicLauncher.java:115)
IOSWindowSystemInterface : setSwapInterval unimp
setSwapInterval(1)
谁能帮帮我, 我怎样才能在我的代码中提供一个访问权限,以便我可以创建一个 sqlite 数据库?
谢谢 埃尔坎·卡普兰
正如@ItachiUchiha 指出的那样,您的问题与您尝试创建数据库的位置有关:
String dbUrl = "jdbc:sqlite:" + dbName;
Connection conn = DriverManager.getConnection(dbUrl);
您提供的 URL 可以在您的桌面上运行,但不能在移动设备上运行,因为应用程序对存储的访问权限非常有限,并且它只授予对私有本地存储的访问权限.
使用 Gluon 的开源库 Charm-Down,无论应用 运行 在哪个平台上,都可以很容易地获取本地存储的路径。
首先,将这些依赖项添加到您的 build.gradle
脚本中:
ext.CHARM_DOWN_VERSION = "1.0.0"
dependencies {
compile "com.gluonhq:charm-down-common:$CHARM_DOWN_VERSION"
desktopRuntime "com.gluonhq:charm-down-desktop:$CHARM_DOWN_VERSION"
androidRuntime "com.gluonhq:charm-down-android:$CHARM_DOWN_VERSION"
iosRuntime "com.gluonhq:charm-down-ios:$CHARM_DOWN_VERSION"
}
现在,在您的代码中,URL 应该是:
try {
File dir = PlatformFactory.getPlatform().getPrivateStorage();
File db = new File (dir, dbName);
String dbUrl = "jdbc:sqlite:" + db.getAbsolutePath();
Connection conn = DriverManager.getConnection(dbUrl);
...
} catch (Exception e) { }
非常感谢何塞,
这是我的 build.gradle
,所以我可以连接到我局域网上的 mysql-服务器,结果在 Ipad-设备上保存为 sqlite.db;
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 "com.gluonhq:charm-down-common:$CHARM_DOWN_VERSION"
desktopRuntime "com.gluonhq:charm-down-desktop:$CHARM_DOWN_VERSION"
androidRuntime "com.gluonhq:charm-down-android:$CHARM_DOWN_VERSION"
iosRuntime "com.gluonhq:charm-down-ios:$CHARM_DOWN_VERSION"
}
mainClassName = 'com.mtt8.version15'
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
packagingOptions {
exclude 'META-INF/INDEX.LIST'
}
}
ios {
infoPList = file('src/ios/Default-Info.plist')
forceLinkClasses = ['com.mtt8.**.*', 'com.mysql.**.*', 'SQLite.**.*', 'com.gluonhq.**.*']
}
}
和sqlite.java
import com.gluonhq.charm.down.common.PlatformFactory;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Created by erkankaplan on 10.11.15.
*/
public class sqliteDBConnector {
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
st.executeUpdate("INSERT INTO village VALUES (111, 'Concretepage')");
//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();
}
}
}