0
我試圖在我的SQLite3數據庫中的表中插入電子郵件ID。在我的情況下,它成功創建表,但在插入記錄時出現錯誤 - 「near」@gmail「:syntax error」。我該如何解決這個問題?這裏是代碼 -使用JDBC在SQLite數據庫中插入電子郵件
public void insertData(String emailId, double gtse, long receivedDate) throws ClassNotFoundException, SQLException{
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:testdb.sqlite");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
ResultSet result = statement.executeQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='T1'");
if(!result.next()){
statement.executeUpdate("create table T1 (email TEXT, gtse REAL, receiveddate DATE)");
statement.executeUpdate("insert into T1 values(" + emailId + ", "+ gtse +", "+ receivedDate +")");
}
else{
}
}
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);
}
}
}
我當前的查詢看起來像這樣,因爲我將變量插入數據庫。插入到T1值(「emailId,gtse,receivedDate」)。 emailId,gtse和receivedDate是傳遞到此方法的變量 – Dan 2013-04-07 14:50:11
使用預準備語句也會將值插入到數據庫中,但它更安全,不太容易遇到您遇到的錯誤。 – Perception 2013-04-07 14:52:15
非常感謝.. :) – Dan 2013-04-07 14:58:57