2012-12-06 125 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

相关问题