0

I am trying to use luasql.mysql and execute multiple INSERT statements from a file to a mysql database. I can not seem to figure out how to use a string variable instead of using a string. I want to execute from a for loop, looping though a file of strings. Any help would be appreciated.

mysql = require "luasql.mysql"
local env  = mysql.mysql()
local conn = env:connect('mydb','myusername','mypassword','myip')
print(env,conn)
file = io.open("sqldumps.sql")
lines = file:lines()
print("Contents of file:");
for line in lines do  
  status,errorString = conn:execute( '"line"' )
  print(status,errorString )
end
Raphael Heard
  • 79
  • 1
  • 8

1 Answers1

0

It's actually really simple:

for line in file:lines() do
  status, errorString = conn:execute(line)
  print(status, errorString)
end

Lua knows that line is a string, so you don't need any quotes around it. In fact, if you add them, Lua will treat it as the actual text "line", not the content of the line variable.


A hint on for loops: file:lines() just so happens to return a single function that you can use in your for loop, but this isn't always the case.

DarkWiiPlayer
  • 6,871
  • 3
  • 23
  • 38