简体   繁体   English

如何在Java中使用jdbc准备好的语句读取sqlite文本列?

[英]How to read sqlite text column using jdbc prepared statement in java?

Here is my sqlite table 这是我的小桌子

CREATE TABLE hello ( 
    id INTEGER PRIMARY KEY AUTOINCREMENT,  
    import_data text,  
    export_data text
);

Here is my java code 这是我的java代码

Connection sqliteConnection = DriverManager.getConnection(db);
PreparedStatement preparedStatement = sqliteConnection.prepareStatement("select * from hello where export_data = ?;");
preparedStatement.setString(1, "ABD19E3C63"); // hex string
  ResultSet resultSet = preparedStatement.executeQuery();
  resultSet.next();
  importData  = resultSet.getString("import_data");
  System.out.println(importData); // prints nothing although I checked in the sqlite database that it exists.

so importData prints nothing although I can see in the sqlite database that it exists. 因此,尽管我可以在sqlite数据库中看到它的存在,所以importData不会打印任何内容。 not sure why? 不知道为什么?

sqlite query sqlite查询

select hex(import_data) from hello where hex(export_data)="ABD19E3C63"; // This works

The above query works in sqlite although I declared both columns as text 尽管我将两列都声明为文本,但以上查询在sqlite中有效

你应该试试这个吗?

PreparedStatement preparedStatement = sqliteConnection.prepareStatement("select * from hello where hex(export_data) = ?;");

You should use looping statement like the following. 您应该使用如下循环语句。

while (resultSet.next()) {
    String import_data= resultSet.getString("import_data");
     System.out.println("Import_data"+import_data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM