execSQL:bindargs 更好吗?
execSQL: is bindargs better?
我想知道 execSQL 方法可以带 1 个或 2 个参数。
为什么要使用第二种方法,如果我可以使用对象直接在 SQLite 数据库上进行操作?
例如:
db.execSQL("INSERT INTO "+ TableName +" VALUES (null, ?)",
new Object[] { type.getName() })
这比使用这个好吗
db.execSQL("INSERT INTO "+ TableName +" VALUES (null,"+ type.getName() +")")
第一个例子更安全吗?
执行时更快?
更容易阅读...
还是一样?
方法1更好
例如,它可以避免 SQL 注射。
因为它会为您处理数据类型。
这意味着它会在需要时通过添加字符串定界符并转换撇号来转换字符串。
即:
要正确工作,您应该将方法 2 写成
db.execSQL("INSERT INTO " + TableName + " VALUES (null, '" + type.getName().replace("'", "''") + "')");
所以...
is the 1st example more secure?
是的。
faster when executing?
不确定是不是。
easier to read
是的,一旦你习惯了(主要是基于意见)。
... or is it the same?
没有,上面讨论过的。
我想知道 execSQL 方法可以带 1 个或 2 个参数。
为什么要使用第二种方法,如果我可以使用对象直接在 SQLite 数据库上进行操作?
例如:
db.execSQL("INSERT INTO "+ TableName +" VALUES (null, ?)",
new Object[] { type.getName() })
这比使用这个好吗
db.execSQL("INSERT INTO "+ TableName +" VALUES (null,"+ type.getName() +")")
第一个例子更安全吗?
执行时更快?
更容易阅读...
还是一样?
方法1更好
例如,它可以避免 SQL 注射。
因为它会为您处理数据类型。
这意味着它会在需要时通过添加字符串定界符并转换撇号来转换字符串。
即:
要正确工作,您应该将方法 2 写成
db.execSQL("INSERT INTO " + TableName + " VALUES (null, '" + type.getName().replace("'", "''") + "')");
所以...
is the 1st example more secure?
是的。
faster when executing?
不确定是不是。
easier to read
是的,一旦你习惯了(主要是基于意见)。
... or is it the same?
没有,上面讨论过的。