How do I check if an insert was successful with MySQLdb in Python?
Question:
I have this code:
cursor = conn.cursor()
cursor.execute(("insert into new_files (videos_id, filename, "
"is_processing) values (%s,%s,1)"), (id, filename))
logging.warn("%d", cursor.rowcount)
if (cursor.rowcount == 1):
logging.info("inserted values %d, %s", id, filename)
else:
logging.warn("failed to insert values %d, %s", id, filename)
cursor.close()
Fun as it is, cursor.rowcount
is always one, even though i updated my database to make the videos_id a unique key. That is, the insert fails because in my tests the same videos_id
is going to appear (and when I check the database, nothing was inserted).
But for whatever reason, the rowcount
is always 1 – even the logging.warn
I have spits out a rowcount
of 1.
So, the question:
Can I use rowcount
to work out if an insert went fine? If so, what am I (presumably) doing wrong?
otherwise, how do i check if an insert went fine?
Answers:
If the insert fails you’ll get a raised exception or someone will yell or you’ll fall off your chair.
How about using a try/catch block instead of looking at rowcount.
If it catches an exception then there is a problem; otherwise, no problem.
Your code does not commit after the modifications (your modifications are rolled back). That is, you should add the following line after cursor.execute
:
conn.commit()
Failed insert will throw MySQLdb.IntegrityError
, so you should be ready to catch it.
Thus, your code should look something like:
sql_insert = """insert into new_files (videos_id, filename, is_processing)
values (%s,%s,1)"""
cursor = conn.cursor()
try:
affected_count = cursor.execute(sql_insert, (id, filename))
conn.commit()
logging.warn("%d", affected_count)
logging.info("inserted values %d, %s", id, filename)
except MySQLdb.IntegrityError:
logging.warn("failed to insert values %d, %s", id, filename)
finally:
cursor.close()
I don’t have enough reputation to make a comment, but here’s an important note:
It is also possible for execute() to fail silently if you don’t commit after the call. Namely, MyISAM tables don’t require the commit, but InnoDB ones do.
Store the insert statement in the string and print it after. If an exception occurs the statement won’t print.
sql_insert = """insert into new_files (videos_id, filename, is_processing)
values (%s,%s,1)"""
cur.execute(sql_insert)
print('Executed:', sql_insert)
I have this code:
cursor = conn.cursor()
cursor.execute(("insert into new_files (videos_id, filename, "
"is_processing) values (%s,%s,1)"), (id, filename))
logging.warn("%d", cursor.rowcount)
if (cursor.rowcount == 1):
logging.info("inserted values %d, %s", id, filename)
else:
logging.warn("failed to insert values %d, %s", id, filename)
cursor.close()
Fun as it is, cursor.rowcount
is always one, even though i updated my database to make the videos_id a unique key. That is, the insert fails because in my tests the same videos_id
is going to appear (and when I check the database, nothing was inserted).
But for whatever reason, the rowcount
is always 1 – even the logging.warn
I have spits out a rowcount
of 1.
So, the question:
Can I use rowcount
to work out if an insert went fine? If so, what am I (presumably) doing wrong?
otherwise, how do i check if an insert went fine?
If the insert fails you’ll get a raised exception or someone will yell or you’ll fall off your chair.
How about using a try/catch block instead of looking at rowcount.
If it catches an exception then there is a problem; otherwise, no problem.
Your code does not commit after the modifications (your modifications are rolled back). That is, you should add the following line after cursor.execute
:
conn.commit()
Failed insert will throw MySQLdb.IntegrityError
, so you should be ready to catch it.
Thus, your code should look something like:
sql_insert = """insert into new_files (videos_id, filename, is_processing)
values (%s,%s,1)"""
cursor = conn.cursor()
try:
affected_count = cursor.execute(sql_insert, (id, filename))
conn.commit()
logging.warn("%d", affected_count)
logging.info("inserted values %d, %s", id, filename)
except MySQLdb.IntegrityError:
logging.warn("failed to insert values %d, %s", id, filename)
finally:
cursor.close()
I don’t have enough reputation to make a comment, but here’s an important note:
It is also possible for execute() to fail silently if you don’t commit after the call. Namely, MyISAM tables don’t require the commit, but InnoDB ones do.
Store the insert statement in the string and print it after. If an exception occurs the statement won’t print.
sql_insert = """insert into new_files (videos_id, filename, is_processing)
values (%s,%s,1)"""
cur.execute(sql_insert)
print('Executed:', sql_insert)