数据库编程一般过程:
示例:
"""
python操作数据库流程大概如下:
1.创建数据库连接对象
2.创建游标对象
3.利用游标对象,执行sql语句命令
4.若是select操作,获取返回结果,根据需要进行处理
5.若是DML的insert,delete,update操作,需要提交到数据库
6.关闭游标对象
7.关闭数据库连接
"""
import sqlite3
def sqlite3lab():
# python操作数据库流程大概如下:
# 1.创建数据库连接对象
connect=sqlite3.connect("books.db")
# 2.创建游标对象
cursor=connect.cursor()
# 3.利用游标对象,执行sql语句命令
# 4.若是select操作,获取返回结果,根据需要进行处理
# 5.若是DML的insert,delete,update操作,需要提交到数据库
search_sql="select * from books"
select=cursor.execute(search_sql)
#获取查询结果
result=select.fetchall()
# 6.关闭游标对象
cursor.close()
# 7.关闭数据库连接
connect.close()
#返回结果
return result
if __name__ == '__main__':
resultes=sqlite3lab() # 获取结果,遍历结果集输出
for item in resultes:
print(item)
优化版本:
"""
python操作数据库流程大概如下:
1.创建数据库连接对象
2.创建游标对象
3.利用游标对象,执行sql语句命令
4.若是select操作,获取返回结果,根据需要进行处理
5.若是DML的insert,delete,update操作,需要提交到数据库
6.关闭游标对象
7.关闭数据库连接
"""
import sqlite3
##################################################################
##################################################################
#数据查询操作
# 调用sqlite3lab函数
def callsqlite3lab():
resultes = sqlite3lab() # 获取结果,遍历结果集输出
for item in resultes:
print(item)
def sqlite3lab():
# python操作数据库流程大概如下:
# 1.创建数据库连接对象
connect=sqlite3.connect("books.db")
# 2.创建游标对象
cursor=connect.cursor()
# 3.利用游标对象,执行sql语句命令
# 4.若是select操作,获取返回结果,根据需要进行处理
# 5.若是DML的insert,delete,update操作,需要提交到数据库
search_sql="select * from books"
select=cursor.execute(search_sql)
#获取查询结果
result=select.fetchall()
# 6.关闭游标对象
cursor.close()
# 7.关闭数据库连接
connect.close()
#返回结果
return result
#有条件的查询代码实现
def ifselect():
# 1.创建数据库连接对象
connection = sqlite3.connect("books.db")
try:
# 2. 创建游标对象
cursor=connection.cursor()
# 3. 执行SQL操作
sql = 'select * from books where id > ?'
# cursor.execute(sql, [80]) # cursor.execute(sql, 0)
cursor.execute(sql,(2,)) # cursor.execute(sql, 0)
# 4. 提取结果集
result_set = cursor.fetchall()
for row in result_set:
print('id:{0} - title:{1}'.format(row[0], row[1]))
# with代码块结束 5. 关闭游标
finally:
#游标关闭
cursor.close()
# 6. 关闭数据连接
connection.close()
# 查询单个条件的数据
def selects():
# 1.创建数据库连接对象
connection = sqlite3.connect("books.db")
try:
# 2. 创建游标对象
cursor=connection.cursor()
# 3. 执行SQL操作
sql = 'select max(price) from books'
cursor.execute(sql)
# 4. 提取结果集
row = cursor.fetchone()
if row is not None:
print('最大用户Id :{0}'.format(row[0]))
# with代码块结束 5. 关闭游标
finally:
#游标关闭
cursor.close()
# 6. 关闭数据连接
connection.close()
###################################################################
##################################################################
# 数据修改操作
def updatedata():
connection = sqlite3.connect("books.db")
try:
# 2. 创建游标对象
cursor=connection.cursor()
# 3. 执行SQL操作
sql = 'insert into books (title, author,price) values (?,?,?)'
cursor.execute(sql, ('计算机编程历史','人教版本',33.33))
# 4. 提交数据库事务
connection.commit()
# with代码块结束 5. 关闭游标
except Exception as e:
print("数据写入失败",e)
# 4. 回滚数据库事务
connection.rollback()
finally:
cursor.close()
# 6. 关闭数据连接
connection.close()
if __name__ == '__main__':
selects()
updatedata() # 更新数据
ifselect()
# resultes=sqlite3lab() # 获取结果,遍历结果集输出
# for item in resultes:
# print(item)