我有一個包含字符串的sqlite數據庫。我正在從我的javafx接口讀取這些字符串。一切工作順利,但我的問題是當我試圖閱讀帶撇號的字符串。我讀的字符串代碼如下:Sqlite閱讀帶撇號的文本
String sql = "select * from Questions where Subject = ? and Grade = ? and Level = ? and questionId = ?";
PreparedStatement pst = gui.connectionQuestions.prepareStatement(sql);
pst.setString(1,gui.textSubjectQTest);
pst.setString(2,gui.showGradeLabel.getText());
pst.setString(3,gui.showCurrentLevelLabel.getText())
pst.setString(4,list.get(counter));
ResultSet rs = pst.executeQuery();
if(rs.next())
{
String temp = rs.getString("Question");
gui.question.setText(temp);
...
sql = "Update Questions set Used ='"+1+"' where Question = '"+gui.question.getText().replaceAll("'", "/'")+"'";
pst = gui.connectionQuestions.prepareStatement(sql);
pst.execute();
在上面的代碼中,我一個[執行查詢返回的問題字符串,並將其添加到標籤gui.question。然而,由於撇號我收到以下錯誤(我得到了錯誤,由於最後一行):
[SQLITE_ERROR] SQL錯誤或丟失的數據庫(靠近「(」:語法錯誤)
我試圖按照解決方案from here,但我的概率仍然是如何解決的東西與撇號
編輯:?我試着使用雙撇號逃脫的字符,該方法是工作,但它改變了我的字符串。雙撇號,這是沒有用的。
你從哪裏得到錯誤,這裏有什麼值? –
我編輯我的問題,我收到pst.execute()中的錯誤; – konstantin
啊 - 在你之前沒有發佈過的代碼中。是的,你也應該使用參數化的SQL。 *始終*使用參數化SQL,而不是直接在SQL字符串中包含未知值。目前還不清楚爲什麼你會使用參數化的SQL進行第一次查詢,但後來停止這樣做... –