我使用这些库:
sqljdbc4.jar
INTEGER
在SQLite中,列类型为long tsMills = 123;
preparedStatement.setLong(parameterIndex, tsMills);
。
我的Java代码:
long
这会正确地将数据插入数据库。
但是,如果我使用long tsMills = 1522746908000l;
值,如
{{1}}
此值不已插入数据库。
我如何解决这个问题?
这里是SQLite中所有可能的数据类型:
https://github.com/nltk/nltk/blob/develop/nltk/classify/naivebayes.py#L201
答案 0 :(得分:0)
SQLite does not care much about types。使用Java插入长值非常有可能。
使用此库
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.21.0.1</version>
</dependency>
和一个包含一个表的数据库test.db
create table warehouses(name string, capacity integer);
此Java代码按预期工作:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertApp {
private Connection connect() {
String url = "jdbc:sqlite:/tmp/test.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
private void insert(String name, long capacity) {
String sql = "INSERT INTO warehouses(name,capacity) VALUES(?,?)";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, name);
pstmt.setLong(2, capacity);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
InsertApp app = new InsertApp();
app.insert("Raw Materials", 3000000000L);
app.insert("Semifinished Goods", 4000000000L);
app.insert("Finished Goods", 5000000000L);
}
}
执行此代码后,该表有三行,其中包含预期值:
$ sqlite3 test.db .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE warehouses(name string, capacity integer);
INSERT INTO "warehouses" VALUES('Raw Materials',3000000000);
INSERT INTO "warehouses" VALUES('Semifinished Goods',4000000000);
INSERT INTO "warehouses" VALUES('Finished Goods',5000000000);
COMMIT;