lua-resty-mysql 怎么调用输入输出的类型的PROCEDURE?

128 views
Skip to first unread message

sunjun

unread,
Oct 25, 2014, 4:27:52 AM10/25/14
to openresty
第一步:db:send_query()
     local sql = "call PROC_V1_ADD_user('aa',@randpass);select @randpass; "
第二步:db:read_result() 调用2次
 
谁能提供封装好的相关的类库,和更好的解决思路,拜谢
 
 

Yichun Zhang (agentzh)

unread,
Oct 27, 2014, 3:56:02 PM10/27/14
to openresty
Hello!

2014-10-25 1:27 GMT-07:00 sunjun:
> 第一步:db:send_query()
> local sql = "call PROC_V1_ADD_user('aa',@randpass);select @randpass; "
> 第二步:db:read_result() 调用2次
>
> 谁能提供封装好的相关的类库,和更好的解决思路,拜谢
>

https://github.com/openresty/lua-resty-mysql#read_result

"If more results are following the current result, a second err return
value will be given the stringagain. One should always check this
(second) return value and if it is again, then she should call this
method again to retrieve more results. This usually happens when the
original query contains multiple statements (separated by semicolon in
the same query string) or calling a MySQL procedure."

Regards,
-agentzh

yuting

unread,
Nov 11, 2014, 2:11:17 AM11/11/14
to openresty
大家好,
    不好意思我又来了。
    我将db.的初始化代码放入了,content_by_lua_file 中
    local function mysql_connect(options)
    local mysql = require "resty.mysql"
    local db, err = mysql:new()
    if not db then error("failed to instantiate mysql: " .. err) end
    db:set_timeout("1000")
    local db_options = {
        host = options.host,
        port = options.port,
        database = options.database,
        user = options.user,
        password = options.password,
        max_packet_size = max_packet_size
    }
    local ok, err, errno, sqlstate = db:connect(db_options)
    if not ok then error("failed to connect to mysql: " .. err .. ": " .. errno .. " " .. sqlstate) end
    return db
end
 
local options = {
  host = "10.10.10.240",
  port = 3306,
  database = "todo",
  user = "root",
  password = "root",
  max_packet_size = 1024 * 1024 }
 
local db_conn = mysql_connect(options)
 
query = function(sql,...)
  local res = db_conn:query(format(sql,...))
  return res
end
local ok, err = db_conn:set_keepalive(10000, 100)
 
我在使用测试代码的时候报了如下错误。
 
2014/11/11 14:52:36 [error] 26586#0: *1 lua entry thread aborted: runtime error: attempt to yield across C-call boundary
stack traceback:
coroutine 0:
        [C]: in function 'connect'
        /usr/local/nginx/lualib/resty/mysql.lua:486: in function 'connect'
        lua/db.lua:21: in function 'mysql_connect'
        lua/db.lua:35: in main chunk
        [C]: in function 'require'
        ./lua/service.lua:1: in function <./lua/service.lua:1>, client: 127.0.0.1, server: 10.10.10.240, request: "GET /joint/service/test5 HTTP/1.1", host: "localhost"
我想问下 我这问题出在哪里了谢谢。
 

yuting

unread,
Nov 11, 2014, 2:46:35 AM11/11/14
to openresty
大家好 我已经解决这个C越界的问题,谢谢大家
 
location / {
   content_by_lua '
     local util = require("util")
     local db = require("db")
     local db_conn = db.init()
     ngx.say(util.encode(db_conn:query("select * from test")))
   ';
 }
 
感谢大家
 
 
 
2014-11-11

yuting

发件人:"yuting" <yut...@sh-tpa.com>
发送时间:2014-11-11 15:11
主题:[openresty] lua-resty-mysql connect报错
收件人:"openresty"<open...@googlegroups.com>
抄送:
 
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
Reply all
Reply to author
Forward
0 new messages