1
我正在嘗試output my table as a .csv file。我正在使用sqlite-jdbc java庫來執行我的查詢。以下是聲明:什麼是在我的SQLite查詢中導致「near」。「:syntax error」?
.mode csv
.output test.csv
select * from geninfo;
.output stdout
,這是我的Java代碼:
try {
try {
// create a database connection
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ex) {
Logger.getLogger(SQLite.class.getName()).log(Level.SEVERE, null, ex);
}
connection = DriverManager.getConnection("jdbc:sqlite:702Data.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("create table if not exists geninfo (id TEXT, DeviceID TEXT)");
statement.executeUpdate(".mode csv");
statement.executeUpdate(".output test.csv");
statement.executeUpdate("select * from geninfo;");
statement.executeUpdate(".output stdout");
ResultSet rs = statement.executeQuery("select * from geninfo");
while (rs.next()) {
// read the result set
System.out.println("DeviceID = " + rs.getString("DeviceID"));
System.out.println("id = " + rs.getInt("id"));
}
} catch (SQLException e) {
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// connection close failed.
System.err.println(e);
}
}
}
我曾嘗試加入周圍的時期括號,並去除週期,都會導致同樣的語法錯誤。
的命令,比如'.mode csv'似乎並不適合javadoc的'執行給定的SQL語句,這可能是一個INSERT,UPDATE或DELETE語句,或者不返回任何內容的SQL語句,如SQL DDL statement.'用於'executeUpdate(String)'。你可能需要'execute(String)'。 –
後堆棧跟蹤 – Makky
@SotiriosDelimanolis好找,我嘗試過了,雖然收到了同樣的錯誤,'[SQLITE_ERROR] SQL錯誤或丟失的數據庫(近「」:語法錯誤)' – jeanqueq