简体   繁体   English

在Raspberry pi上使用sqlite和Java

[英]Use sqlite with Java on Raspberry pi

I have created small code of sqlite database in java. 我在java中创建了sqlite数据库的小代码。

Below is the code for connection with database, 以下是与数据库连接的代码,

Class.forName("org.sqlite.JDBC");

objConnection=DriverManager.getConnection("jdbc:sqlite:/etc/javaData/Test.db");

In this code the exception thrown in second line for getConnection(). 在此代码中,getConnection()的第二行抛出异常。 I am trying this code on Raspberry pi. 我在Raspberry pi上尝试这个代码。 How to fix the exceptionrelated to this 如何修复与此相关的异常

Exception 例外

SQLITE addDeviceInfo()-[SQLITE] SQL Exception occured. RetryCount:1 (MAX Retry: 2) ErrorCode: 27154 Exception Message: Error opening connection Stack Trace: java.sql.SQLException: Error opening connection
  at org.sqlite.core.CoreConnection.open(CoreConnection.java:140)
  at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:66)
  at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:21)
  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:571)
  at java.sql.DriverManager.getConnection(DriverManager.java:215)
  at kmbt.DCA.SQLite.DCASQLiteUtility.getSQliteConnection(DCASQLiteUtility.java:152)
  at kmbt.DCA.SQLite.ManagedDeviceSetting.SQLite_ManagedDeviceSettingManager.addDeviceInfo(SQLite_ManagedDeviceSettingManager.java:981)
  at kmbt.DCA.DCAChildProcessMng.InitManagedDeviceInfo(DCAChildProcessMng.java:5726)
  at kmbt.DCA.DCAChildProcessMng.InitSQLite(DCAChildProcessMng.java:4387)
  at kmbt.DCA.DCAChildProcessMng.start(DCAChildProcessMng.java:365)
  at kmbt.DCA.DCAChildProcess.main(DCAChildProcess.java:23)
 Caused by: java.lang.Exception: Error loading native library: /org/sqlite/native/Linux/arm/libsqlitejdbc.so
  at org.sqlite.SQLiteJDBCLoader.loadSQLiteNativeLibrary(SQLiteJDBCLoader.java:243)  at org.sqlite.SQLiteJDBCLoader.initialize(SQLiteJDBCLoader.java:65)
  at org.sqlite.core.NativeDB.load(NativeDB.java:53)
  at org.sqlite.core.CoreConnection.open(CoreConnection.java:136)
  ... 14 more

Your code seems correct and the exception thrown seems don't have nothing to do with some library's problem. 你的代码似乎是正确的,抛出的异常似乎与某些库的问题没有任何关系。 The only thing that sound me a little bit strange, is that you put your database in /etc folder. 唯一让我觉得有点奇怪的是你将数据库放在/ etc文件夹中。 First of all it is not very correct, because the /etc folder is used to store config file and not data. 首先它不是很正确,因为/ etc文件夹用于存储配置文件而不是数据。 In addition to this, maybe there could be some kind of issue with r/w folder's permissions. 除此之外,r / w文件夹的权限可能存在某种问题。 Try to move your database to your home directory or to other any directory owned by your user and not from root. 尝试将数据库移动到主目录或用户拥有的其他任何目录,而不是root用户。

Hope this will be usefull for you. 希望这对你有用。

-- EDIT -- - 编辑 -

Ok, the problem is that you have downloaded the wrong library: 好的,问题是你下载了错误的库:

Error loading native library: /org/sqlite/native/Linux/arm/libsqlitejdbc.so

You have to use the ARM build of the sqlite library. 您必须使用sqlite库的ARM构建。 Try to take a look at this: Error opening connection SQLite on Raspberry pi and this: Sqlite4java on Raspberry Pi 试着看看这个: 在Raspberry pi上打开连接SQLite时出错 ,这个: Raspberry Pi上的Sqlite4java

I also faced java.lang.Exception: Error loading native library: /org/sqlite/native/Linux/arm/libsqlitejdbc.so 我还遇到了java.lang.Exception: Error loading native library: /org/sqlite/native/Linux/arm/libsqlitejdbc.so

Looking into sqlite-jdbc-3.8.6.jar I found that /org/sqlite/native/Linux/ contains no arm folder -> sqlite is not compiled for arm. 查看sqlite-jdbc-3.8.6.jar我发现/org/sqlite/native/Linux/包含arm文件夹 - > sqlite不是为arm编译的。

With these changes I've managed to build on the pi: https://bitbucket.org/kidmose/sqlite-jdbc/commits/cb7a7ef62c034938e8ecb737b148e5c80877c083 通过这些变化,我设法建立在pi: https//bitbucket.org/kidmose/sqlite-jdbc/commits/cb7a7ef62c034938e8ecb737b148e5c80877c083

pi@raspberry ~/sqlite-jdbc $ export JAVA_HOME=/usr/lib/jvm/jdk-7-oracle-armhf/
pi@raspberry ~/sqlite-jdbc $ chmod 755 ./amalgamation_version.sh
pi@raspberry ~/sqlite-jdbc $ make

Replacing the old jar with the newly built one solved my problem. 用新建的旧罐子替换旧罐子解决了我的问题。

All credit goes to: https://pidome.wordpress.com/2013/10/01/java-goes-hard-on-the-raspberry-pi-so-does-pidome/ 所有信用都归于: https//pidome.wordpress.com/2013/10/01/java-goes-hard-on-the-raspberry-pi-so-does-pidome/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM