SQLite:在SQL请求中获取包含单引号的文本并插入。

我已经检查了所有关于这个问题的论坛,并且我找到了解决方法来进行第一次插入,我使用了 “双引号” 代替单引号,如下所示:

insertGiftShop(2,"photo02","Modern City by night photo", "item-grumdy1l", "A view of Modern City''s skytrain",1,100, "","no","items",0)

上一个函数将行插入到游戏的开头。当我检查数据库时,存储的值是这样的:“A view of Modern City's skytrain”。

它非常好用!现在我正在尝试获取存储的信息“ A view of Modern City's skytrain ”并将其插入到另一个表中,如下所示:

function insertInventory(id, code, name, src, desc, sale, qtyoninventory, price, usetxt, type)
   local sql = "insert into inventory (id, code, name, src, desc, sale, qtyoninventory, price, usetxt, type) values (" .. id.. ",'" .. code .. "','" .. name .. "', '" .. src .. "', '" .. desc .. "', '" .. sale .. "',"..qtyoninventory..","..price..",'"..usetxt.."','"..type.."')"
   db:exec(sql)
end

insertInventory(maxid+1,row_2.code, row_2.name, row_2.src, row_2.desc, "no",1,row_2.price,row_2.usetxt, row_2.type)

在这种情况下,我直接从存储的文件获取row_2.desc(即“A view of etc ...”)。但是它不起作用,因为它需要 “单引号”!

如何告诉“格式”,在文本中添加所需的“双引号”时,row_2.desc内包含单引号?


编辑:

根据您的评论,我改变了在我的表中“插入”数据的方式。

所以,我尝试过这个:

tx.executeSql("INSERT INTO inventory(id, code, name) VALUES(?,?,?)",[2, "photo02", "Modern City's skytrain"])

我有一个错误在“\ [”旁边。语法正确吗?

我使用了这个,它是否正确并且可以防止SQL注入?

db:exec([[INSERT INTO items(id, code, name, src, desc, sale, qty, price, usetxt, type, onscene, area) VALUES(1,"ls01","LETP", "item-lss", "LVP","no",0,100, "It this burger?", "items", "yes", "table02")]])
点赞
用户11654
用户11654

永远不要直接将字符串值放入 SQL 字符串中!

这不仅会给您带来格式化问题(正如您所看到的),还会容易受到 SQL 注入攻击。

相反,请使用参数,那么您就不需要转义引号:

tx.executeSql("INSERT INTO MyTable(ID, Name, Description) VALUES(?,?,?)",
              [2, "photo02", "Modern City's skytrain"]);
2014-12-02 17:39:21