Python2.7编程中SQLite3基本操作方法示例( 二 )


conn.commit()
print("Total number of rows updated :", conn.total_changes)
cursor = conn.execute("SELECT id, name, address, salary from COMPANY")
for row in cursor:
print("ID = ", row[0])
print("NAME = ", row[1])
print("ADDRESS = ", row[2])
print("SALARY = ", row[3], "\n")
print("Operation done successfully")
在这个示例中,我们将ID为1的数据的SALARY字段更新为25000.00 。
七、删除数据
在SQLite3中,删除数据的方法也非常简单 。可以使用DELETE语句来删除表格中的数据 。
下面是一个删除数据的示例代码:
import sqlite3
conn = sqlite3.connect('test.db')
print("Opened database successfully")
conn.execute("DELETE from COMPANY where ID = 2;")
conn.commit()
print("Total number of rows deleted :", conn.total_changes)
cursor = conn.execute("SELECT id, name, address, salary from COMPANY")
for row in cursor:
print("ID = ", row[0])
print("NAME = ", row[1])
print("ADDRESS = ", row[2])
print("SALARY = ", row[3], "\n")
print("Operation done successfully")
在这个示例中,我们删除了ID为2的数据 。

推荐阅读