dermur
dermur

Reputation: 193

Need help using os.execute to perform a mysql insert

I'm trying to get a lua script to perform a MySQL update.

However, the double quotes used with the -e "" part of the command are confusing lua (and me!). I've already tried numerous combinations of quotes, such as the ones below:

os.execute(""mysql --host=1.1.1.1 --port=3308 --user=username --password=password db -e "insert into table (RecordDate, RecordMilliseconds, data1, data2, data3) values ('111', '222', '333', '444', '555')""")

stdin:1: ')' expected near 'mysql'

os.execute("mysql --host=1.1.1.1 --port=3308 --user=username --password=password db -e "insert into table (RecordDate, RecordMilliseconds, data1, data2, data3) values ('111', '222', '333', '444', '555')" ")

stdin:1: ')' expected near 'insert'

Any ideas what syntax I can use to allow os.execute work with this mysql command?

Update - OK, I can use Egor's os.execute[[...]] syntax but I now need to be able to substitute those example values '111', '222', etc. with variable names. Of course, when I do this they're getting interpreted literally because of the quotes presumably. For example:

os.execute[[mysql --host=1.1.1.1 --port=3308 --user=username --password=password db -e "insert into table (RecordDate, RecordMilliseconds, data1, data2, data3) values (value1, value2, value3, value4, value5)"]]

which results in an error along the lines of: ERROR 1054 (42S22) at line 1: Unknown column 'my_date' in 'field list'

Hopefully there's some way to reference variable names from within this os.execute[[mysql..."variable"]] structure? Maybe using |variable| syntax?

Obviously in a regular linux shell, this is relatively easy to achieve...

mysql --host=1.1.1.1 --port=3308 --user=username --password=password db -e "insert into table (RecordDate, data1) values ($my_timestamp, $value1)"

Thanks in advance for any much needed help!

Upvotes: 2

Views: 291

Answers (1)

dermur
dermur

Reputation: 193

Ok - finally got around this issue by using string concatenation:

my_command = 'mysql --host=1.1.1.1 --port=3308 --user=username --password=password db -e "insert into table (RecordDate, RecordMilliseconds, data1, data2, data3) values ('..variable1..','..variable2..',')"  etc.

Then I just use

os.execute(my_command)

Does the job just fine although there may be a more elegant way of doing this.

Upvotes: 2

Related Questions