How to format an RDSDataService batchExecuteStatement for an update query with parameters? - aws-sdk

I'm using the RDSDataService.batchExecuteStatement to update records in a postgres database. I'm struggling with understanding how the query parameter should be formatted for an update query.
The insert query is very straight forward and the documentation shows this formatted as
insert into mytable values (:id, :val)
With a full request looking like
aws rds-data batch-execute-statement --resource-arn "arn:aws:rds:us-east-1:123456789012:cluster:mydbcluster" \
--database "mydb" --secret-arn "arn:aws:secretsmanager:us-east-1:123456789012:secret:mysecret" \
--sql "insert into mytable values (:id, :val)" \
--parameter-sets "[[{\"name\": \"id\", \"value\": {\"longValue\": 1}},{\"name\": \"val\", \"value\": {\"stringValue\": \"ValueOne\"}}],
[{\"name\": \"id\", \"value\": {\"longValue\": 2}},{\"name\": \"val\", \"value\": {\"stringValue\": \"ValueTwo\"}}],
[{\"name\": \"id\", \"value\": {\"longValue\": 3}},{\"name\": \"val\", \"value\": {\"stringValue\": \"ValueThree\"}}]]"
How would an update query look like? Would it just be the following?
update mytable set :id, set :val

Related

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)

How to set Django to Ignore mysql errors and force mysql to insert a record?

So I am a naive Django developer and want a similar functionality as INSERT IGNORE of mysql in Django. Is there a way to do that? Right now whenever I try to save a record in my database, It throws (1364, Field doesn't have a default value). Should there be any change in Django settings or mysql settings?
You can execute mySQL command in Django.
create_fields = ['f1', 'f2', 'f3']
values = [
(1, 2, 3),
(4, 5, 6),
(5, 3, 8)
]
db_table = self.model._meta.db_table
values_sql = []
values_sql.append( "(%s)" % (','.join([ " %s " for i in range(len(create_fields))]),) ) # correct format
base_sql = "INSERT IGNORE INTO %s (%s) VALUES " % (db_table, ",".join(create_fields))
sql = """%s %s""" % (base_sql, ", ".join(values_sql))
cursor.executemany(sql, values)

Filtering pg_dump with only INSERT lines and changing table name using awk

I want to create a bash script that does a pg_dump of only INSERT lines and changes the table title in the INSERT line.
I have the following bash script:
#!bin/bash
#Create temp files to store the PSQL dump
DUMPFILE='poops.dump.sql' || (echo "make sql dump file failed" 1>&2; exit 1)
TMPFILE=`mktemp` || (echo "mktemp failed" 1>&2; exit 1)
#Tables to dump: api_order, poops_order_dates, poops_price
#Dump as INSERT queries statements
pg_dump --username="poops" --host="localhost" \
--table="api_order" --table="poops_order_dates" --table="poops_price" \
--no-password --column-inserts \
--data-only "poops" | \
awk '/^INSERT/ {i=1} {if(i) print}' \
> "$TMPFILE" \
|| (echo "pg_dump failed" 1>&2; exit 1)
(echo "start transaction; truncate table api_order; "; \
echo "truncate table poops_order_dates; "; \
echo "truncate table poops_price; "; \
cat "$TMPFILE"; echo 'commit;' ) \
> "$DUMPFILE" \
|| (echo "parsing dump file failed" 1>&2; exit 1)
rm "$TMPFILE"
It removes all the junk before the first INSERT INTO line, however there is still the following stuff after all the INSERT INTO queries:
--
-- Name: api_order_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('api_order_id_seq', 33146, true);
--
-- Data for Name: poops_order_dates; Type: TABLE DATA; Schema: public; Owner: poops
--
--
-- Name: poops_order_dates_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('poops_order_dates_id_seq', 1, false);
--
-- Data for Name: poops_price; Type: TABLE DATA; Schema: public; Owner: poops
--
--
-- Name: poops_price_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('poops_price_id_seq', 1, false);
--
-- PostgreSQL database dump complete
--
How do I write the following awk line:
awk '/^INSERT/ {i=1} {if(i) print}' \
To make it so that it only outputs the INSERT INTO queries, and changes api_order to api_order_test.
I did try:
awk '/^INSERT/ {gsub("INSERT INTO api_order", "INSERT INTO api_order_test", $0); print $0}' \
But because of print $0, it cuts off part of the query where it starts on a new line. I need it to filter for, output and change only
"INSERT INTO api_order .... );\r"
I believe at the end it is a carriage return (\r) or does pg_dump output each INSERT INTO query with a \n at the end?
Raw SQL Dump:
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET lock_timeout = 0;
--
-- Data for Name: api_order; Type: TABLE DATA; Schema: public; Owner: poops
--
INSERT INTO api_order (id, order type, …'', 0, NULL);
INSERT INTO api_order (id, order type, …'', 0, NULL);
INSERT INTO api_order (id, order type, …'', 0, NULL);
INSERT INTO api_order (id, order type, …'', 0, NULL);
INSERT INTO api_order (id, order type, …'', 0, NULL);
--
-- Name: api_order_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('api_order_id_seq', 33294, true);
--
-- Data for Name: hoops_price; Type: TABLE DATA; Schema: public; Owner: poops
--
--
-- Name: hoops_price_id_seq; Type: SEQUENCE SET; Schema: public; Owner: poops
--
SELECT pg_catalog.setval('hoops_price_id_seq', 1, false);
--
-- PostgreSQL database dump complete
--
If everything you don't want to print comes after everything you do want to print, you can "turn off" printing in the same way you turn it on. Something like /^INSERT/ {i=1} /^SELECT/ {i=0} i. (Note that the trailing i is equivalent to your { if (i) print }) If you're input is more complicated or there are other considerations, please post a representative sample of the input to awk and your expected output.
– jas

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!
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.

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");