尝试连接全局 'q2'(table 值)电晕错误

Attempt to concatenate global 'q2' (a table value) corona error

我正在做一个 corona 项目,我一直在尝试将 2 个文本框值插入到我的 sqlite 数据库中。我无法执行此操作并不断收到错误 "Attempt to concatenate global 'q2' (a table value) " 下面是错误的图片。

 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
    -- User begins editing "numericField"
end
end

 -- Create text field
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
    -- User begins editing "numericField"
end
end

-- Create text field
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 行

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

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

我不确定你到底想做什么,但这行不通,因为 table 值不能以这种方式连接。

假设 q1.text 给出了您正在寻找的值,这样的事情应该有效:

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

请注意,连接值以形成 SQl 查询使其容易受到 SQL injection attack 的攻击;你最好改用占位符。

要获取 TextField 对象的值,请使用 textField.text。它 returns 表示本机文本输入字段内容的字符串。 q1.textq2.text 说明应该适合您。