2013-09-27 69 views
0

如何插入一个字符串是这样的:使用corona在sqlite中插入查询?

local Namestring="my mother's gift" 

local insertQuery1 =[[INSERT INTO planne_tbl VALUES (']]..Namestring..[[');]] 

db:exec(insertQuery1) 

如何插入'符号sqlite的。

回答

1

通过连接字符串构造SQL命令不仅会导致格式问题,还会允许SQL injection attacks

在SQL中使用字符串值的推荐方法是使用参数。 在Lua中,它的工作原理是这样的:

local Namestring="my mother's gift" 
local insertQuery1 = "INSERT INTO planne_tbl VALUES (?)" 
local stmt = db:prepare(insertQuery1) 
stmt:bind(1, Namestring) 
stmt:step() 
stmt:finalize() 

(这是不必要的复杂性,你可能要为此编写的辅助功能。)

+0

我们需要做同样的选择具有可变例如:local displayitems =“SELECT * FROM items WHERE code ='”.. itemcode ..“'and area ='”.. vararea ..“'” –

+1

这些字符串也可以包含引号。 –