【问题标题】:Attempt to concatenate global 'q2' (a table value) corona error尝试连接全局“q2”(表值)电晕错误
【发布时间】:2018-07-04 06:10:37
【问题描述】:

我正在做一个电晕项目,我一直在尝试将 2 个文本框值插入到我的 sqlite 数据库中。我无法做到这一点并不断收到错误“尝试连接全局'q2'(表值)”下面是错误的图片。

 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 行

【问题讨论】:

    标签: sqlite lua coronasdk


    【解决方案1】:

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

    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 injection attack 的攻击;你最好改用占位符。

    【讨论】:

    • q1 和 q2 是数字文本框。我只是想将值插入到 sqlite DB 中。你能帮我解决这个问题吗?
    • 你需要像别人说的那样使用q1.text,但是如果没有赋值,它将是nil,所以你需要检查:(q1 or "NULL")或(q1 or "") ;与第二季度相同。
    • 嗨,我已经完成了,但仍然有错误。我已经更新了上面的问题,你可以看看。谢谢
    • 我已经添加了示例;我没有看到您将建议纳入更新后的问题中,因此我在更新后的答案中发布了它的外观。
    • 嗨,现在没有错误,但在我的数据库中它是一个空白值(null)。它仅在我删除 q1.text = "" 和 q2.text = "" 时有效。但是当我删除代码时,当我点击提交按钮时,我的文本框不会刷新(清除以前的值)。你知道为什么会这样吗?或者更确切地说我怎样才能刷新所有的文本框
    【解决方案2】:

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

    【讨论】:

    • 它现在仍然显示(零值)错误。你知道怎么回事吗?
    • 我的所有代码都在问题中。你能帮我吗??
    • 零值错误指的是哪一行或哪条指令?
    • 嗨,你能看看我上面的问题吗?我已经更新了。谢谢
    • 嗨,你能看看stackoverflow.com/questions/48455857/…这是我的实际问题。谢谢:DD
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2020-08-02
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多