Tag: sqlite

Java:缺少数据库错误

我收到以下错误: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: apartments) 实际上,该表确实存在。 以下是我的代码: try { // load the sqlite-JDBC driver using the current class loader Class.forName(“org.sqlite.JDBC”); Connection connection = null; // create a database connection connection = DriverManager.getConnection(“jdbc:sqlite:/Path/To/apartments.db”); System.out.println(connection.getMetaData()); Statement statement = connection.createStatement(); statement.setQueryTimeout(30); ResultSet rs = statement.executeQuery(“select * from apartments”); while(rs.next()) { // […]

如何通过java在sqlite中插入日期

我想创建一个将在其中保存日期的数据库(SQLite)。 现在首先要问的是声明日期列的正确语法是什么。 第二个我想知道的是如何在此之后插入日期。 我想知道的第三件事是如何在两者之间选择日期,例如选择包含01/05/2010和05/06/2010之间日期的所有行。 谢谢

使用Java中的sqlite获取最后插入的id的最佳方法是什么?

使用Java中的sqlite获取最后插入的id的最佳方法是什么? 谷歌给了我不同的答案 – 有人说选择last-insert-rowid; 其他人说call statement.getGeneratedKeys()。 什么是最好的路线? (我只想返回id,不要将它用于其他插入或任何东西。)

multithreadingJava应用程序中的SQLite

我编写了一个java应用程序,偶尔将事件从多个线程记录到SQLite数据库。 我注意到我可以通过同时生成少量事件来相对容易地触发SQLite的“数据库锁定”错误。 这促使我编写了一个模拟最坏情况行为的测试程序,我很惊讶SQLite在这个用例中表现得有多糟糕。 下面发布的代码只是将五条记录添加到数据库中,首先按顺序获取“控制”值。 然后同时添加相同的五个记录。 import java.sql.*; public class Main { public static void main(String[] args) throws Exception { Class.forName(“org.sqlite.JDBC”); Connection conn = DriverManager.getConnection(“jdbc:sqlite:test.db”); Statement stat = conn.createStatement(); stat.executeUpdate(“drop table if exists people”); stat.executeUpdate(“create table people (name, occupation)”); conn.close(); SqlTask tasks[] = { new SqlTask(“Gandhi”, “politics”), new SqlTask(“Turing”, “computers”), new SqlTask(“Picaso”, “artist”), new SqlTask(“shakespeare”, “writer”), […]

java.lang.ClassNotFoundException:来自xerial的Sample.java程序中的org.sqlite.JDBC错误

我试图让Xerial的Sample类在Eclipse中使用sqlite,但我不断收到错误“ClassNotFoundException:org.sqlite.JDBC” 我从https://bitbucket.org/xerial/sqlite-jdbc/downloads下载了sqlite-jdbc-3.7.2.jar文件。 将它复制到eclipse中我的项目“database_test”下的lib文件夹中。 然后右键单击Project-> Properties-> Java Build Path-> Libraries选项卡 – > Add JARs->选择jar文件。 我试图从这里找到的Xerial执行此代码: https ://bitbucket.org/xerial/sqlite-jdbc#markdown-header-usage // load the sqlite-JDBC driver using the current class loader Class.forName(“org.sqlite.JDBC”); Connection connection = null; try { // create a database connection connection = DriverManager.getConnection(“jdbc:sqlite:sample.db”); Statement statement = connection.createStatement(); statement.setQueryTimeout(30); // set timeout to 30 sec. statement.executeUpdate(“drop table if […]

什么是’Class.forName(“org.sqlite.JDBC”);’ 做?

我正在尝试使用SQLite数据库创建一个简单的应用程序。 我选择使用SQLiteJDBC驱动程序 。 以下代码取自上述网站。 我的问题是关于public static void main之后的行… 它的内容如下: Class.forName(“org.sqlite.JDBC”); 我的问题是,这条线是什么意思? 它做了什么? 它似乎没有连接到其余的代码。 Class.forName()应该返回一个类,但该行似乎独立于体内。 无论它返回的是代码的另一部分都没有使用,我可以看到。 请帮助澄清一下。 提前致谢。 public class Test { public static void main(String[] args) throws Exception { Class.forName(“org.sqlite.JDBC”); Connection conn = DriverManager.getConnection(“jdbc:sqlite:test.db”); Statement stat = conn.createStatement(); stat.executeUpdate(“drop table if exists people;”); stat.executeUpdate(“create table people (name, occupation);”); PreparedStatement prep = conn.prepareStatement( “insert into people […]

每个实体hibernate两个表

我有一个实体 – User 。 它由User.class描述。 Hibernate为每个实体创建一个表,所以当我调用session.save(user) ,我的数据总是保存到该表中。 现在我需要另一个表用于相同User类型的数据,我只需要将我的实体保存到该表。 数据结构 (类似这样): table users_1_table{ string id; string username; } table users_2_table{ string id; string username; } 使用这个 : session.save(user1,”users_1_table”) session.save(user2,”users_2_table”) 结果我应该在users_1_table有user1在users_1_table user2 。 由于系统限制,我不能将这两个对象放在一个表中。 (即使创建额外的字段也是个坏主意)。 我可以不进行子类化吗? 使用programmaticaly hibernate配置?

无法将图像保存为blob到sqlite

我正在使用SQLite,我无法将图像保存到数据库。 这是我的代码: File file = new File(url); try { fis = new FileInputStream(file); } catch (FileNotFoundException e) {} fileLenght = (int) file.length(); stat.executeUpdate(“create table “+tableName+” (id int,name String ,image Blob, features String);”); prep = conn.prepareStatement(“insert into “+tableName+” values (?, ?, ?, ?);”); prep.setBinaryStream(3, fis, fileLenght); 这是我得到的错误: java.sql.SQLException: not implemented by SQLite JDBC driver at org.sqlite.Unused.unused(Unused.java:29) […]

如何通过Java在SQLite中强制执行外键约束?

看来SQLite默认不强制执行外键。 我正在使用sqlitejdbc-v056.jar并且我已经阅读了使用PRAGMA foreign_keys = ON; 将打开外键约束,并且需要在每个连接的基础上打开它。 我的问题是:我需要执行哪些Java语句来打开此命令? 我试过了: connection.createStatement().execute(“PRAGMA foreign_keys = ON”); 和 Properties properties = new Properties(); properties.setProperty(“PRAGMA foreign_keys”, “ON”); connection = DriverManager.getConnection(“jdbc:sqlite:test.db”, properties); 和 connection = DriverManager.getConnection(“jdbc:sqlite:test.db;foreign keys=true;”); 但这些都不起作用。 这里有什么我想念的吗? 我已经看到了这个答案 ,我想做同样的事情,只使用JDBC。

使用查询字符串在Android SQLiteDatabase中执行插入/更新/删除的正确方法是什么?

我一直在查看官方文档( http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html )以及与StackOverflowpost和实际观察到的行为的交叉引用,文档似乎是在几个方面误导并可能是错误的。 我正在尝试执行参数化查询以插入/更新/删除,就像使用rawQuery(String sql, String[] arguments). 我需要通过查询而不是在SQLiteDatabase类上使用insert / update / delete方法来执行此操作,因为在许多情况下,查询跨越多个表,并且在任何情况下我们都编写了一组我们用于此app的查询在我们所有的平台上(也为SQLite编写),最好按原样使用它。 这是我发现令人困惑/误导的内容: doc对rawQuery说,语句不能用分号终止,但它实际上似乎没有任何区别。 这对我很重要,因为我们有一个巨大的XML文档,里面填充了我在我的应用程序中跨多个平台使用的查询,如果可能的话,我更愿意保持相同。 rawQuery似乎不适用于插入(是的,我尝试过使用和不使用分号)。 该文件没有说明这一点。 我确实看到它返回一个Cursor,我想这可能是一个倾斜提示,它只适用于select,但不一定 – 它可以简单地返回零长度或null Cursor,当它们没有结果集时。 execSQL(String sql, Object[] bindArgs)明确表示它不适用于插入,但事实上确实如此! 此外,尽管execSQL(String, Object[])明确告诉您不要尝试CRUD操作,但它的无参数版本不包含此类警告,并且也可以正常工作(缺点是不允许SQL参数)。 所有这一切的结果是我能够找到成功执行带参数化参数的插入的唯一方法是使用文档明确指示您不要用于此目的的一种工作方法。 此外,对于插入/更新/删除而言,rawQuery不起作用真是一个让人失望,因为我们有一个通用的数据层,如果我们可以使用单个统一的API调用来运行我们所有的CRUD查询,它会更优雅。数据库。 那么这里发生了什么? 这份文件是否已经过时了? 是否可以使用execSql进行插入,更新等? 有没有人成功使用rawQuery进行插入? 附录: 针对OS运行:Android 4.3。 示例查询(什么工作,什么不工作) // Works db.execSql(“INSERT into MyTable (Col1, Col2) VALUES (?, ?)”, new String[] { “Foo”, “Bar” }); db.execSql(“INSERT into […]