Skip to content

Instantly share code, notes, and snippets.

@thewhitetulip
Last active August 29, 2015 14:10
Show Gist options
  • Save thewhitetulip/f76ac98a98b3a0da7395 to your computer and use it in GitHub Desktop.
Save thewhitetulip/f76ac98a98b3a0da7395 to your computer and use it in GitHub Desktop.
The curious case of %d in cursor.execute
#!/bin/python
def insert_post(post):
try:
cursor.execute("select max(post_id) from post_master")
fetched = cursor.fetchone()
if fetched is None:
post_id=0
else:
post_id = fetched+1
cursor.execute('insert into post_master\
(post_id,post_title,post_content,posted_on,post_author,post_url) \
values (%d,%s,%s,%s,%s,%s)',[ post_id, post['title'], post['text'], \
post['date'], post['username'], post['url']])
cursor.commit()
except:
app.logger.error("Couldn't execute insert_posts")
#instead of %d you need to use %s irrespective of the data type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment