在android中更新sqlite db的首选方法

时间:2011-03-01 12:24:28

标签: java android performance sqlite sql-injection

在android中使用db.update的哪种方式更快更好?即:构造整个where子句字符串以及where子句变量值或者通过将where子句变量值作为字符串数组传递来使用第4个参数进行更新?

将where子句变量值作为新字符串数组传递是否可以防止sql注入攻击?

  public boolean UpdateChannelSortKey(Channel c)
  {
        ContentValues cv = new ContentValues();
        cv.put("SortKey", c.SortKey);
        return this.db.update("Channels", cv, "ChannelID = ?", new String[]{String.valueOf(c.ChannelID)}) > 0;
  }

OR

public boolean UpdateChannelSortKey(Channel c)
  {
        ContentValues cv = new ContentValues();
        cv.put("SortKey", c.SortKey);
        return this.db.update("Channels", cv, "ChannelID = " + c.ChannelID, null) > 0;
  }

1 个答案:

答案 0 :(得分:8)

第一种方式更可取,因为:

1)是的,它可以防止sql注入攻击。

2)最好总是使用准备好的语句 - 不仅仅是在android中,所以你会养成一个好习惯。

3)恕我直言,它具有更高的可读性。