【问题标题】:SQLite : Insert text with single quote got from a SQL requestSQLite:插入带有从 SQL 请求获得的单引号的文本
【发布时间】:2015-01-31 01:27:32
【问题描述】:

我已经查看了有关此问题的所有论坛,并为我的第一个插入找到了解决方案,我使用“双引号”而不是单引号,如下所示:

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”并将其插入到另一个表中,如下所示:

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(什么是“等视图”)。但它不起作用,因为它需要“单引号”!

当文本内部有单引号时,如何告诉“格式化” 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")]])

【问题讨论】:

    标签: sqlite lua coronasdk


    【解决方案1】:

    永远不要将字符串值直接放入 SQL 字符串! 这不仅会给您带来格式问题(如您所见),而且还允许 SQL 注入攻击。

    改用参数,这样就不用转义引号了:

    tx.executeSql("INSERT INTO MyTable(ID, Name, Description) VALUES(?,?,?)",
                  [2, "photo02", "Modern City's skytrain"]);
    

    【讨论】:

    • 对不起,我无法理解我的问题和您的回答之间的关系 :( 如果您在谈论第二个“插入”,我的值来自 SQL 请求!
    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 2023-03-25
    • 2016-05-02
    • 2013-03-24
    • 2010-10-31
    相关资源
    最近更新 更多