【问题标题】:Assign an only select value, to a only one variable将唯一选择值分配给唯一一个变量
【发布时间】:2019-10-08 17:53:52
【问题描述】:

我用 LUA 和 SQL 语句来说明。我已经尝试过了,我可以显示一个 SELECT 或进行一个 UPDATE、INSERT ...但我不知道如何将一个唯一返回的选择值分配给一个变量

mysql = require "luasql.mysql"

env  = mysql.mysql()
conn = env:connect('table','user','pass','server')
--print(env,conn)

cursor,errorString = conn:execute([[select id from agent where extension = '9072']])
--print(cursor,errorString)

row = cursor:fetch ({}, "a")

while row do
   print(string.format("%s", row.id))
   row = cursor:fetch (row, "a")
end

-- close everything
cursor:close()
conn:close()
env:close()

我希望是这样的: 值=33

【问题讨论】:

标签: mysql lua


【解决方案1】:

我愿意

sql = "select id from agent where extension = '9072' limit 1"
cursor,errorString = conn:execute(sql)
value = nil
if cursor then
  row = cursor:fetch ({}, "a")    -- we'll have 0 or 1 rows
  if row then value = row.id end
end

【讨论】:

    猜你喜欢
    • 2017-01-31
    • 1970-01-01
    • 2018-03-08
    • 2019-01-20
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    相关资源
    最近更新 更多