How do I check if an insert was successful with MySQLdb in Python? -



How do I check if an insert was successful with MySQLdb in Python? -

i have code:

cursor = conn.cursor() cursor.execute(("insert 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 insert values %d, %s", id, filename) cursor.close()

fun is, cursor.rowcount always one, though updated database create videos_id unique key. is, insert fails because in tests same videos_id going appear (and when check database, nil inserted). whatever reason, rowcount 1 - logging.warn have spits out rowcount of 1.

so, question: can utilize rowcount work out if insert went fine? if so, (presumably) doing wrong? otherwise, how check if insert went fine?

your code not commit after modifications (your modifications rolled back). should add together next line after cursor.execute:

conn.commit()

failed insert throw mysqldb.integrityerror, should ready grab it.

thus, code should like:

sql_insert = """insert 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 insert values %d, %s", id, filename) finally: cursor.close()

python mysql-python

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -