0

I have a scenario in which i have to store python list in database via python script. i have seen lot of solution like converting it into tuple or removing quotes around a string. but it want to store it into database in python list format.

['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']

and my query is

sql_test = "UPDATE log_messages SET unique_arguments =  %s , sent_to =  %s where log_id = %s " %  (unique_arguments_list,email_send_to,str(log_id))

and getting this error

syntax error at or near "["
LINE 1: UPDATE log_messages SET unique_arguments =  ['[email protected]...
                                                ^
5
  • Obviously string representation of list results in non-valid sql syntax, you have to either fix it (wrapping list in " quotation marks may be sufficient) of find other efficient way of serializing your list. Commented Mar 22, 2015 at 20:19
  • Obviously, I know this, that is why i ask here for any alternative Commented Mar 22, 2015 at 20:25
  • It's not clear what database / python interface you are using. Commented Mar 23, 2015 at 1:31
  • @MikeT python script with postgresql database Commented Mar 23, 2015 at 6:19
  • It's still not clear what python interface you are using, e.g. psycopg2, pygresql, pyodbc, etc. Commented Mar 23, 2015 at 6:30

1 Answer 1

1

This should work. You are not enclosing your list string within quotes.

sql_test = "UPDATE log_messages SET unique_arguments =  \"%s\" , sent_to =  %s where log_id = %s " %  (unique_arguments_list,email_send_to,str(log_id))
Sign up to request clarification or add additional context in comments.

5 Comments

this way, String breaks from \"
What do you mean by "String breaks"?
See the parameter now, double quotes the query as it is in string from
Well, should it not be? In which format should it be stored? There's no such thing as list format in SQL as far as I know. Such a thing would be pointless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.