last day (14 days later) » 

12:42 PM
2
A: Importing tuple list into Postgres via Python

salomonderossiYou could use the multirow VALUES syntax described in the examples part of the postgresql documentation. Here ist a python snipped which creates the insert statement from the tuple_list from your question. tuple_list = [(u'17',u'1', u'2', u'2'), (u'17',u'2', u'1', u'4')] a = ["("+", ".join(a)+")...

 
I'm new to postgres so that comment is very helpful. As my list of tuples may have 5 tuples or even 50 tuples, how can I write a query that adapts to the number of tuples I am passing to it?
 
@LemusThelroy I updated my answer
 
This is a fantastic answer. Thank you. If this query sits in my Python code, do you think it would be safe from SQL injection? As the tuple_list comes from user input from the web application.
 
@LemusThelroy You should ensure that all inserted data is properly escaped. The best way is to create stored procedures. This example ist NOT save!
 
I have a stored procedure that I use which is as follows: select spupserttopredictions(%s, %s, %s, %s)" % (user_identity, Web_Fixture_No, Web_Home_Side_Score, Web_Away_Side_Score)) Can I still import the tuple list into this?
 
12:42 PM
Just let me think a moment...
 
I have moved this to a private chat
thank you so much for your help so far
 
no problem
 
I am thinking that I might need to pass one string to postgres and split up the string in postgres
e.g. 2|2|3|4||2|7|6||
but I may be over complicating this
 
imho the simplest way would be to create the query in python as mentioned and then send it to postgresql. String processing is also possible in pgsql but a lot more complicated
regexp_split_to_array('a|b|c|d', '|') should work in posgresql
 
the function in my db looks like this...
CREATE OR REPLACE FUNCTION spupserttopredictions(user_identity integer, "Web_Fixture_No" integer, "Web_Home_Side_Score" integer, "Web_Away_Side_Score" integer)
RETURNS void AS
$BODY$
BEGIN
LOOP
-- first try to update the key
-- note that "a" must be unique
UPDATE "Predictions" SET "userid" = "user_identity", "Fixture_No" = "Web_Fixture_No", "Home_Side_Score" = "Web_Home_Side_Score", "Away_Side_Score" = "Web_Away_Side_Score" WHERE "Fixture_No" = "Web_Fixture_No" AND "userid" = "user_identity";
 
12:48 PM
But I'm not sure if this will help you...
 
Do i not just need to create this as a string in python
and insert my arg as the values
I am not sure how to add mutliple values into my update query
 
You are right. That should work, too. But I'm not that experienced in plpgsql...
 
UPDATE "Predictions" FROM VALUES+args_str SET ...
I'm not too sure that's right though
 
79
A: Update multiple rows in same query using PostgreSQL

Roman PekarYou can also use update ... from syntax and use a mapping table. If you want to update more than one column, it's much more generalizable: update test as t set column_a = c.column_a from (values ('123', 1), ('345', 2) ) as c(column_b, column_a) where c.column_b = t.column_b; You...

 
That looks the trick
I'll give that a go
 
12:59 PM
There is a nice example of updating multiple rows
I can not help you any further. I will be in a meeting soon
 
I thought I could get away with making a python web application and blag my way throught postgres...but it's a little harder than that!
Thank you for your help, it's been great
 
No problem. You are welcome :)
 

last day (14 days later) »