Dysanix Official
Dysanix Official

Reputation: 862

How do I update a value in a SQLite DB?

I am trying to update a specific row in an SQLite DB, but it doesn't seem to work. This is what the database looks like.

-----------------------
_id | user | coins
-----------------------
1   | me   | 20
-----------------------

And this is what my code looks like:

public static void updateUserWallet() throws SQLException {
        try {
            int coins;
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:Database.db");

            String query = "Select * from userSettings WHERE user = " + "'" + lastUser + "'";
            Statement statement = connection.createStatement();
            ResultSet result = statement.executeQuery(query);

            while (result.next()) {
                coins = result.getInt("coins");
                result.updateInt("coins", 10);
                result.updateRow();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Selecting the table and stuff works, but the second I execute this code, it crashes:

result.updateInt("coins", 10);
result.updateRow();

With this error:

java.sql.SQLException: not implemented by SQLite JDBC driver

    at org.sqlite.jdbc4.JDBC4ResultSet.unused(JDBC4ResultSet.java:321)
    at org.sqlite.jdbc4.JDBC4ResultSet.updateInt(JDBC4ResultSet.java:452)
    at dysanix.advanced.kikbot.main.updateUserWallet(main.java:367)
    at dysanix.advanced.kikbot.main$1.cmdGameRoll(main.java:266)
    at dysanix.advanced.kikbot.main$1.run(main.java:142)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

-----------------------
_id | user | coins
-----------------------
1   | me   | 20
-----------------------

All I want to do, is update the "20" to "10" (or another number). How would I go around doing this properly?

Upvotes: 2

Views: 5025

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123549

The error message is telling you that the SQLite JDBC driver does not support updatable ResultSets. (The feature is "not implemented by SQLite JDBC driver".) So, you'll need to use an UPDATE statement, like so:

String query = "UPDATE userSettings SET coins=? WHERE user=?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setInt(1, 10);
statement.setString(2, lastUser);
statement.executeUpdate();

Upvotes: 6

Related Questions