尝试连接全局变量'q2'(一个表值) corona错误

我正在做一个corona项目,我一直在尝试将2个文本框值插入到我的sqlite数据库中,但是我无法做到这一点,不断出现一个错误“尝试连接全局变量'q2'(一个表值)”,下面是错误截图。 从corona获得的错误消息

 local widget = require "widget"
 local sqlite3 = require( "sqlite3" )
 local path = system.pathForFile( "data.db", system.DocumentsDirectory )
 local db = sqlite3.open( path )
 local tablesetup = [[CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, q1, q2);]]
print( tablesetup )
db:exec( tablesetup )

_G.numericField = q1
local function textListener( event )

if ( event.phase == "began" ) then
    -- 用户开始编辑“numericField”
end
end

 -- 创建文本框
q1 = native.newTextField( 290, 150, 50, 30 )
q1.inputType = "number"
q1:addEventListener( "userInput", textListener )

 _G.numericField1 = q2
local function textListener( event )

if ( event.phase == "began" ) then
    -- 用户开始编辑“numericField”
end
end

-- 创建文本框
q2 = native.newTextField( 290, 50, 50, 30 )
q2.inputType = "number"
q2:addEventListener( "userInput", textListener )

saveData = function ( event )
   --textString = q1.text
   --textString = q2.text
  -- LINE 39 IS BELOW
local tablefill = [[INSERT INTO test VALUES (NULL, ']]..q1..[[',']]..q2..[['); ]]

    db:exec( tablefill )
    end

  savebutton = widget.newButton {
    left = 60,
    top = 250,
    default = "buttonGreen.png",
    over = "buttonGreenOver.png",
    label = "Update",
    embose = true,
    onRelease = saveData
    }

有人能帮忙吗?先感谢您!

更新(第36-41行)

saveData = function ( event )
q1.text = ""
q2.text = ""
local tablefill = [[INSERT INTO test VALUES (NULL, ']]..q1..[[',']]..q2..[['); ]]
db:exec( tablefill )
end

现在我仍然得到相同的错误,但是这次是在第40行。

点赞
用户1442917
用户1442917

你正在创建q1q2对象(它们表示为表格),但随后尝试将它们的值作为字符串连接:

q1 = native.newTextField( 290, 150, 50, 30 )
...
q2 = native.newTextField( 290, 50, 50, 30 )
...
local tablefill = [[INSERT INTO test VALUES (NULL, ']]..q1..[[',']]..q2..[['); ]]

我不确定你想要做什么,但这是行不通的,因为表格值不能这样连接。

假设q1.text给出了你想要的值,这样做应该可以:

local tablefill = [[INSERT INTO test VALUES (NULL, ']]..(q1.text or "")
  ..[[',']]..(q2.text or "")..[['); ]]

请注意,将值连接成SQL查询的方式使其容易受到SQL注入攻击的影响;你最好使用占位符而不是这种方式。

2018-01-25 06:11:57
用户7026995
用户7026995

要获取 TextField 对象的值,请使用 textField.text。它会返回表示原生文本输入字段内容的字符串。 指令 q1.textq2.text 适用于你。

2018-01-25 09:03:32