2012-12-06 126 views
1

我有一個html字符串我想存儲在我的SQLite數據庫「原樣」。在HTML字符串中的特殊字符阻止我INSERT的語句從存放:Android的 - 如何在SQLite數據庫中存儲html字符串

INSERT INTO myTable VALUES ('" + htmlString + "') 

在iOS上我用參數化查詢來實現這一點,它工作得很好。我如何才能在Android上完成此操作?我有谷歌參數化Android的查詢,但結果是多種多樣和不清楚。

Android中

回答

2

你有參數化查詢太...少的方式來實現這一目標:

ContentValues vals = new ContentValues(); 
vals.putString("ColumnName", htmlString); 
db.insert("myTable", null, vals); 

final SQLiteStatement insert = db.compileStatement("INSERT INTO myTable VALUES (?)"); 
insert.bindString(1, htmlString); 
//edit: hehe forgot about most important thing 
insert.executeInsert(); 

db.rawQuery("INSERT INTO myTable VALUES (?)", new String[] {htmlString}); 

編輯:(插入多行)

如果你瓦納插入多1行,然後做在交易(應該是更快) 和喜歡第二個解決方案:

db.beginTransaction(); 
try { 
    final SQLiteStatement insert = db.compileStatement("INSERT INTO myTable VALUES (?)"); 
    for(...){ 
    insert.clearBindings(); 
    insert.bindString(1, htmlString[N]); 
    //edit: hehe forgot about most important thing 
    insert.executeInsert(); 
    } 
    db.setTransactionSuccessful(); 
} finally { 
    db.endTransaction(); 
} 
+0

謝謝! db.rawQuery與db.execSQL一樣只是參數?另外,在你提供的3個例子中,你是否贊成其中的任何一個?如果是這樣,爲什麼? – PaulG

+0

execSQL不應該用於SELECT/INSERT/UPDATE/DELETE ...如果你只需要1行然後選擇其中的3個(真的無所謂),如果你插入多行然後看我的編輯 – Selvin

相關問題