Skip to content

SQL Result Type #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
StemonZhang opened this issue Aug 29, 2013 · 5 comments
Closed

SQL Result Type #277

StemonZhang opened this issue Aug 29, 2013 · 5 comments

Comments

@StemonZhang
Copy link

I am confused , what's the type of the SQL Result?

I used the Lua get the data stored in MySQL DB .

The SQL statement is db:query("select result from result_table order by data_time desc limit 1").

I want to get the result. But I don't know the type of it~

when I use like this:

                    if table.getn(res) ~= 0 then
                            ngx.say(res[1][1]) ;
                    end

The result is nil. But I directly used the same SQL Statement to the MySQL DB, it could get the result I wanted.

When I use like this:

                    if table.getn(res) ~= 0 then
                            ngx.say(res[1]) ;
                    end

It threw error: runtime error: [string "rewrite_by_lua"]:39: bad argument #1 to 'say' (non-array table found)

It's strange, I search the Internet ,but there is no answer about the type of the SQL Result.

THX~

@agentzh
Copy link
Member

agentzh commented Aug 30, 2013

Which MySQL client are you using? Are you using the lua-resty-mysql library? If yes, you should consult its documentation for details on the output results. Basically, it is documented in the read_query method's documentation:

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

That is, by default, you should access a single field in the form res[row_num][column_name], for instance,

res[1]["id"]

accesses the "id" field in the 1st row of the resultset.

But also be aware of the "compact_arrays" option in the "connect" method:

https://github.com/agentzh/lua-resty-mysql#connect

Regards,
-agentzh

@StemonZhang
Copy link
Author

It did work.res[row_num][column_name]is OK.
I thought it was res[row_num][column_num] before.
So it worked wrong.
I checked the info in https://github.com/agentzh/lua-resty-mysql#connect.
But It did not do this definitely.
Just use these words to describe the info like this:

It returns a Lua table (res) describing the MySQL OK packet or result set packet for the query result.

For queries corresponding to a result set, it returns an array holding all the rows. Each row holds key-value apirs for each data fields.

But now I think it already do the job, because it said : "Each row holdskey-valueapirs for each data fields".

Key-Value is the most info, but I ignored it , it's my fault.

Thank you very much, Big Zhang^_^

At 2013-08-30 23:46:37,"Yichun Zhang" [email protected] wrote:

Which MySQL client are you using? Are you using the lua-resty-mysql library? If yes, you should consult its documentation for details on the output results. Basically, it is documented in the read_query method's documentation:

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

That is, by default, you should access a single field in the form res[row_num][column_name], for instance,

res[1]["id"]

accesses the "id" field in the 1st row of the resultset.

But also be aware of the "compact_arrays" option in the "connect" method:

https://github.com/agentzh/lua-resty-mysql#connect

Regards,
-agentzh

¡ª
Reply to this email directly or view it on GitHub.

@agentzh
Copy link
Member

agentzh commented Sep 1, 2013

If I were you, I would just use libraries like lua-cjson to dump the data structures ;)

@StemonZhang
Copy link
Author

Yeah, it's a good idea.
But I just want to get a single data from the DB, so if get the data structure, maybe the cost is a little big.

ÔÚ 2013-09-02 06:25:57£¬"Yichun Zhang" [email protected] дµÀ£º

If I were you, I would just use libraries like lua-cjson to dump the data structures ;)

¡ª
Reply to this email directly or view it on GitHub.

@agentzh
Copy link
Member

agentzh commented Sep 8, 2013

@StemonZhang I'm suggesting using lua-cjson temporarily for your local debugging purposes, not for the formal output in the final product.

Anyway, I'm closing this.

@agentzh agentzh closed this as completed Sep 8, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants