我试图将游戏得分存储在 SQLite 数据库中。我在 DB Browser 中创建了一个表 SCORE 和字段 HIGHSCORE。在 Game 类中,我在游戏结束后调用以下方法
private void Add(int highScore) {
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:score.db");
c.setAutoCommit(false);
stmt = c.createStatement();
String sql = " INSERT INTO SCORE (HIGHSCORE) " +
"VALUES (highScore);"+highScore;
stmt.executeUpdate(sql);
stmt.close();
c.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Score registred");
}
这是错误:java.sql.SQLException:没有这样的列:highScore
最佳答案
您没有正确传递参数。
更改为:
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:score.db");
c.setAutoCommit(false);
String sql = "INSERT INTO SCORE (HIGHSCORE) VALUES (?);";
PreparedStatement stmt = c.prepareStatement(sql);
stmt.setInt(1, highScore);
stmt.executeUpdate(sql);
stmt.close();
c.close();
这样就可以将参数highScore
传递给sql语句。
关于java - 将游戏分数存储到sqlite数据库java中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56245879/