Need help using os.execute to perform a mysql insert - mysql

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!

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.

Related

PHP exec | mysql command returns sh: -c: line 0: syntax error near unexpected token `newline'

I want to make multiple insertions into DB, but mysql command returns me:
sh: -c: line 0: syntax error near unexpected token `newline'.
Does anybody know, what this error means?
Here is my command:
mysql -h 127.0.0.1 -D db_name -u username --password=pass --debug-info true <shared/local_sql/3.sql>
Example of SQL file:
DELETE FROM table_name WHERE num = '999999';
START TRANSACTION;
INSERT INTO table_name SET key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = '', key1 = '', key1 = 'val1', num = '999999';
INSERT INTO table_name SET key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = '', key1 = '', key1 = 'val2', num = '999999';
COMMIT;
UPDATE table_name SET num = '999998' WHERE num = '2';
UPDATE table_name SET num = '2' WHERE num = '999999';
DELETE FROM table_name WHERE num = '999998' OR num = '999999';
Use
mysql -h 127.0.0.1 -D db_name -u username --password=pass --debug-info true shared/local_sql/3.sql
You probably copied the characters < and > from some example where these characters are used to indicate a placeholder for the file name like mysql ... <sql_file>.
The whole placeholder <sql_file> must be replaced with the actual file name which is shared/local_sql/3.sql in your example.
The characters < and > are interpreted by the shell as redirection of standard input or standard output.
<shared/local_sql/3.sql instructs the shell to redirect the standard input of the mysql command from the specified file. This may actually work.
> needs a file name after it to specify where the output should be redirected to. Since there is no file name but the end of the line you get the error message.
> is the output redirection operator, and you don't set any output file. A shorter test case:
$ >
sh: syntax error: unexpected newline
If you really want to send output to a file you need to add the file name, but I have the impression that you were thinking of <> as a kind of quotes, which they aren't. Get rid of >:
mysql -h 127.0.0.1 -D db_name -u username --password=pass --debug-info true < shared/local_sql/3.sql

Inserting a variable into MySQL with Go

I have these 2 variables here
name := request.FormValue("username")
pass := request.FormValue("password")
I want to insert those 2 variables into my database
db.Query("INSERT INTO `godb` (`Username`, `Password`) VALUES ( )")
I tried (name,pass) ('name','pass') ($name, $pass) , none of them work.
Hope the question is not stupid, but I've been looking for solutions online but I did't understand them. Thanks !
From Using Prepared Statements
Parameter Placeholder Syntax
The syntax for placeholder parameters in prepared statements is
database-specific. For example, comparing MySQL, PostgreSQL, and
Oracle:
MySQL PostgreSQL Oracle
===== ========== ======
WHERE col = ? WHERE col = $1 WHERE col = :col
VALUES(?, ?, ?) VALUES($1, $2, $3) VALUES(:val1, :val2, :val3)
You tried PostgreSQL syntax but you use MySQL.
query should be in this format
db.Query("INSERT INTO table ($1, $2) VALUES (column1, column2)", value1, value2)
in your case something like that
db.Query("INSERT INTO godb ($1, $2) VALUES (username, password)", name, pass)

invalid byte sequence for encoding "UTF8": 0xed 0xa0 0xbd

I have been importing some data from MySQL to Postgres, the plan should have been simple- manually re-create the tables with their equivalent data types, divise a way to output as CSV, transfer over the data, copy it into Postgres. Done.
mysql -u whatever -p whatever -d the_database
SELECT * INTO OUTFILE '/tmp/the_table.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' FROM the_table;
send and import to postgres
psql -etcetc -d other_database
COPY the_table FROM '/csv/file/location/the_table.csv' WITH( FORMAT CSV, DELIMITER ',', QUOTE '"', ESCAPE '\', NULL '\N' );
It had been too long, I had forgotten that '0000-00-00' was a thing...
so first of all I had to come up with some way of addressing weird data types, preferably at the MySQL end and so wrote this script for the 20 or so tables I planned to import to address any imcompatabilities and list out the columns accordingly
with a as (
select
'the_table'::text as tblname,
'public'::text as schname
), b as (
select array_to_string( array_agg( x.column_name ), ',' ) as the_cols from (
select
case
when udt_name = 'timestamp'
then 'NULLIF('|| column_name::text || ',''0000-00-00 00:00:00'')'
when udt_name = 'date'
then 'NULLIF('|| column_name::text || ',''0000-00-00'')'
else column_name::text
end as column_name
from information_schema.columns, a
where table_schema = a.schname
and table_name = a.tblname
order by ordinal_position
) x
)
select 'SELECT '|| b.the_cols ||' INTO OUTFILE ''/tmp/'|| a.tblname ||'.csv'' FIELDS TERMINATED BY '','' OPTIONALLY ENCLOSED BY ''"'' ESCAPED BY ''\\'' FROM '|| a.tblname ||';' from a,b;
Generate CSV, ok. Transfer across, ok - Once over...
BEGIN;
ALTER TABLE the_table SET( autovacuum_enabled = false, toast.autovacuum_enabled = false );
COPY the_table FROM '/csv/file/location/the_table.csv' WITH( FORMAT CSV, DELIMITER ',', QUOTE '"', ESCAPE '\', NULL '\N' ); -- '
ALTER TABLE the_table SET( autovacuum_enabled = true, toast.autovacuum_enabled = true );
COMMIT;
and it was all going well, until I came across this message:
ERROR: invalid byte sequence for encoding "UTF8": 0xed 0xa0 0xbd
CONTEXT: COPY new_table, line 12345678
a second table also encountered the same error however every other one imported successfully.
Now all columns and tables in the MySQL db were set to utf8, the first offending table containing messages was along the lines of
CREATE TABLE whatever(
col1 int(11) NOT NULL AUTO_INCREMENT,
col2 date,
col3 int(11),
col4 int(11),
col5 int(11),
col6 int(11),
col7 varchar(64),
PRIMARY KEY(col1)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
So presumably the data should be utf... right? to make sure there were no major errors I edited the my.cnf to ensure everything I could think of to include the encoding
[character sets]
default-character-set=utf8
default-character-set=utf8
character-set-server = utf8
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
I altered my initial "query generating query" case statement to convert columns for the sake of converting
case
when udt_name = 'timestamp'
then 'NULLIF('|| column_name::text || ',''0000-00-00 00:00:00'')'
when udt_name = 'date'
then 'NULLIF('|| column_name::text || ',''0000-00-00'')'
when udt_name = 'text'
then 'CONVERT('|| column_name::text || ' USING utf8)'
else column_name::text
end as column_name
and still no luck. After googling "0xed 0xa0 0xbd" I am still none the wiser, character sets are not really my thing.
I even opened the 3 gig csv file to the line it mentioned and there didn't appear to be anything out of place, looking with a hex editor I could not see those byte values (edit: maybe I didn't look hard enough) so I am starting to run out of ideas. Am I missing something really simple, and worryingly, is it possible that some of the other tables may have been more "silently" corrupted too?
The MySQL version is 5.5.44 on a ubuntu 14.04 operating system and the Postgres is 9.4
Without any further things to try I went for the simplest solution, just alter the files
iconv -f utf-8 -t utf-8 -c the_file.csv > the_file_iconv.csv
there were about 100 bytes between the new files and the originals, so there must've been invalid bytes in there somewhere that I could not see, they imported "properly" so I suppose that is good, however it would be nice to know if there were some way to enforce proper encoding when creating the files before discovering about it on import.

Inserting data into the mysql database from perl

I am trying to insert data into a MySQL database:
$response = $client->fql->query(
query => '
SELECT name, email, birthday, username, first_name, last_name, pic
FROM user
WHERE uid = me()
',
);
print join "\n Name:", sort map { $_->{name} } #$response;
$dbh->do("
INSERT INTO Users(SNo,Name,Email,Birthday,UserName,FirstName,LastName)
VALUES(1,
sort map { $_->{name} } #$response,
'imm\#gmail.com',
'1987/12/10',
'imm',
'imm',
'Dee')
");
$dbh->disconnect();
used the mysql query in one line.
This above print statement is printing the name correctly but why the above sql insert statement is not working?
I connect the db and after that i am receiving the value and printing in the browser is working.
Why does the mysql statement not accept the value?
When inserting the database is not working?
You should have a look at the official doc
and specially this :
# INSERT some data into 'foo'. We are using $dbh->quote() for
# quoting the name.
$dbh->do("INSERT INTO foo VALUES (1, " . $dbh->quote("Tim") . ")");
# Same thing, but using placeholders
$dbh->do("INSERT INTO foo VALUES (?, ?)", undef, 2, "Jochen");

INSERT INTO not working on newest xampp

$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf) VALUES ($plan_name, $plan_nr, $anz)";
echo "<br />".$query."<br />";
if (!mysql_query($query) && !$error) {
die (mysql_error());
}
mysql-error tells me:
INSERT INTO 'p_stats' ('name', 'name_ID', 'anz_aufruf') VALUES ('Laptop 1', '1', '95')
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''p_stats' ('name', 'name_ID', 'anz_aufruf') VALUES ('Laptop 1', '1', '95')' at line 1
Where is the code wrong here?
the error is the wrapping of single quotes around table name.
Table names as well as column names are identifiers. They should be wrap if a name is a reserved keyword with backtick (optional if not). Single quotes are for string literal.
MySQL - when to use single quotes, double quotes, and backticks?
Use this.
$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf)
VALUES ('".$plan_name."', '".$plan_nr."', '".$anz."')";
$query = "INSERT INTO $stats_table_name (name, name_ID, anz_aufruf)
VALUES ('".$plan_name."', '".$plan_nr."', '".$anz."')";