3

I have this so far

mk= {}
mk = conn:query({ live=true, sql="select * from OrderReports where OrderId= '"..T.OrderId.."'"})
 for a=1, # mk do

 end

I want strings at different indexes in mk to be concatenated and stored in a variable let's say lk, what should be in the body ?

1
  • Be wary of concatenating strings to get data into SQL queries; it opens the door to SQL injection attacks. While this example is probably not too insecure, it's better to use prepared statements and binding if the the engine supports it (and all competent ones should). Commented Mar 24, 2014 at 14:04

1 Answer 1

5

The direct way is to concatenate the strings in the for body like this:

local lk = ""
for a = 1, # mk do
    lk = lk .. mk[a]
end

But this has a poor performance when the table is large because strings are immutable in Lua. A new string is created and the contents are copied in each loop.

Instead, you should use the built-in function table.concat() for this job:

local lk = table.concat(mk)
Sign up to request clarification or add additional context in comments.

4 Comments

The first one gives error that says nodes with children can not be used in concatenaton.
second one gives error saying" bad argument #1 to '?' (table expected, got userdata"
@Simrankaur You said mk is an array, in Lua, that means it's a table with index from 1 to some integral value only. But from your error message, it's not, its type is userdata.
Thank you Yu hao, it worked :), that was error because of something else, My mistake .thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.