python - How to tell whether an update statement is successful in pysqlite 2.6.3 -
i'm using pysqlite
talk sqlite db, , wonder right way check whether update
sql statement has update in table.
is there variable can check after execution in pysqlite
?
check cursor.rowcount
attribute; it'll indicate number of affected rows.
if update
not successful rowcount
0:
>>> conn = sqlite3.connect(':memory:') >>> conn.execute('create table foo (bar, baz)') <sqlite3.cursor object @ 0x1042ab6c0> >>> conn.execute('insert foo values (1, 2)') <sqlite3.cursor object @ 0x1042ab730> >>> cursor = conn.cursor() >>> cursor.execute('update foo set baz=3 bar=2') <sqlite3.cursor object @ 0x1042ab6c0> >>> cursor.rowcount 0 >>> cursor.execute('update foo set baz=3 bar=1') <sqlite3.cursor object @ 0x1042ab6c0> >>> cursor.rowcount 1
of course, if seek update table or column doesn't exist, exception thrown instead:
>>> cursor.execute('update nonesuch set baz=3 bar=2') traceback (most recent phone call last): file "<stdin>", line 1, in <module> sqlite3.operationalerror: no such table: nonesuch >>> cursor.execute('update foo set nonesuchcolumn=3 bar=2') traceback (most recent phone call last): file "<stdin>", line 1, in <module> sqlite3.operationalerror: no such column: nonesuchcolumn
i used sqlite3
library included python demo this; pysqlite2
added python under name in python 2.5. difference simply import:
try: import sqlite3 # included library except importerror: pysqlite2 import dbapi2 sqlite3 # utilize pysqlite2 instead
python sqlite pysqlite
No comments:
Post a Comment