Sqlite + Java:表不更新 [英] Sqlite + Java: table not updating

查看:18
本文介绍了Sqlite + Java:表不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 SQLite 使用一个名为 SQLiteJDBC 的 Java 包装器 - 这可能对任何答案...

I'm using a Java wrapper for SQLite called SQLiteJDBC - this may have some bearing on any answers...

我有一个显示在 GUI 中的表格,在该 UI 中,我有一个按钮,用于显示该表格的单个行的字段.当我保存更改时,我会这样做...

I have a table which I've displayed in a GUI, within that UI I have a button for the fields for an individual row of that table. When I save my changes I do this...

Statement stmt = connection.createStatement();
stmt.execute("update 'tableName' set 'fieldName'=1 where userid=1");

int updateCount = stmt.getUpdateCount();

我的连接是有效的,我没有抛出异常并且 getUpdateCount() 返回1",表明一行已被更新.但是我的表没有更新.我花了最后几个小时试图弄清楚发生了什么,但我没有任何运气.帮助!!

My connection is valid, I get no exceptions thrown and getUpdateCount() returns '1' indicating that one row has been updated. However my table is not updated. I've spent the last few hours trying to work out what's going on but I'm not having any luck. Help!!

更新

问题似乎在于它没有提交我所做的更改.当应用程序运行时,我的更改是可见的,但是一旦我关闭应用程序并重新打开它或直接检查数据库,我的更改就会丢失,我们又回到了原点.这是怎么回事??!

The problem seems to be that it's not committing the changes I make. While the application is running my changes are visible but as soon as I close the application and reopen it or check the database directly my changes are lost and we're back to square one. What's going on??!

自动提交设置为 true,如果我将其设置为 false 并尝试提交,则会抛出异常.

Auto-commit is set to true and if I set it to false and try to commit I get an exception thrown.

推荐答案

我的问题是我依赖垃圾收集器来清理我用过的 ResultSets,我没有明确地关闭它们.

My problem was that I was relying on the garbage collector to clear up my used ResultSets, I was not explicitly closing them.

在 SQLite 中,访问表会锁定整个数据库,因此这些使用过的仍打开"的 ResultSet 阻止了我在读取表内容后获得对表的写访问权限.

In SQLite accessing a table locks the entire database so these used ResultSets that were still 'open' were preventing my obtaining write access to a table after I'd read its contents.

如果您遇到此问题并且正在使用 SQLite,只需在完成后在每个 ResultSet 上调用 close() 即可.(您可能还想尝试对任何 PreparedStatements 执行相同的操作,我假设它们也会关闭它们用于创建的任何 ResultSet,但我还没有尝试验证这一点.)

If you have this problem and are using SQLite just call close() on each ResultSet when you're done with it. (You may also want to try doing the same to any PreparedStatements, I'd assume that these would also close any ResultSets they were used to create but I haven't experimented to verify this.)

这篇关于Sqlite + Java:表不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆