7

Is there a way to do these two updates in a single instruction?

cur.execute("UPDATE table_name1 SET email = '[email protected]' WHERE id = 4")
cur.execute("UPDATE table_name1 SET phone = '0400-123-456' WHERE id = 4")

I've tried all sort of variations but can't get it to work.

Edit: I want to pass email, phone and I'd as parameters.

2
  • 1
    Have you taken a look at the sqlite3 documentation? Commented Apr 2, 2017 at 22:57
  • 2
    Yes peter. I looked at it but it doesn't help with the python implementation of of doing UPDATE on multiple values. Commented Apr 3, 2017 at 21:19

5 Answers 5

10

You're solution opens you up to SQL injections. If you read the first section of the documentation, it specifically says not to do it the way you are proposing:

Never do this -- insecure!

symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)

Do this instead

t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)

So you should change your code to something along the following lines:

conn = sqlite3.connect('connex.db')
cur = conn.cursor()
mobileval = '0400-123-456'
emailval = '[email protected]'
constrain = 4

q = "UPDATE licontacts310317 SET (?, ?) 
             WHERE (?)=(?)"

cur.execute(q, (liemailval, limobileval, id, constrain) )

conn.commit()
conn.close()

I haven't tested it, but hopefully you get the idea =)

Sign up to request clarification or add additional context in comments.

Comments

6

The following works: Its just standard SQL at this point.

cur.execute("""UPDATE table_name1
            SET email = '[email protected]', phone = '0400-123-456'
            WHERE id = 4""")

1 Comment

For me there was a syntax error. I had to remove the quote marks between table name & SET. cur.execute("UPDATE table_name1 SET email = '[email protected]', phone = '0400-123-456' " "WHERE id = 4")
4

I was facing a similar issue with my own code and was able to get my code working using the following:

cur.execute("UPDATE licontacts310317 SET liemail=?, limobile=? WHERE id=? ", (liemailval, limobileval, constrain))

Someone else already commented this, but it's better to use the ? placeholder and not the string formatting operations because those leave your db vulnerable to SQL injection attacks (basically, hackers).

2 Comments

Thank you for the answer. Note however that this is a new answer to a fairly old question that does not add anything that the other answers did not already cover. Consider adding answers only if you are introducing something that the other answers do not address adequately.
I was facing this same issue and the posted solution(s) didn't work for me (either just didn't work or were recommended against due to potential hacking issues) so I wanted to share what did work for me.
2

OK. I made a solution that works with parameters.

First thanks to David for his original answer. It had a small syntax error (corrected in the comments for that answer) but it was enough to help me work out how to get it working without parametising.

(Note:I think David posted his reply before I edited the question to add the need to working with parameters.)

Then this answer helped me parametise the solution.

Here is my solution to the question. I'm poting it in case someone else has the same problem because I did quite a bit of searching before posting the original question and couldn't find the answer.

conn = sqlite3.connect('connex.db')
cur = conn.cursor()
mobileval = '0400-123-456'
emailval = '[email protected]'
constrain = 4

cur.execute("UPDATE licontacts310317 SET liemail=%s, limobile=%s 
             WHERE %s=?" % (liemailval, limobileval, id), (constrain,))

conn.commit()
conn.close()

Comments

0

Use Dictionaries!

They seem to work well:

cur.execute(
   """UPDATE table_name1 
      SET email =:email, 
      phone =:phone 
      WHERE id = 4
   """, 
   {"email": "[email protected]", "phone": '0400-123-456'}
)

So you can just post a dictionary in like so, provided they contain the keys:

cur.execute(
   """UPDATE table_name1 
      SET email =:email, 
      phone =:phone 
      WHERE id = 4
   """, 
   the_dictionary
)

Where the_dictionary = {"email": "[email protected]", "phone": "0400-123-456"}. You can put in as many as you'd like. This seems more readable as well I feel.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.