002.操作sqlite3插入、查询、修改、删除数据

002.操作sqlite3插入、查询、修改、删除数据

1.像数据库中插入一条数据

"""
1. 导入模块
2. 创建连接 sqlite3.connect()
3. 创建游标对象
4. 编写创建表的sql语句
5. 执行sql
6. 提交事务
7. 关闭游标对象和连接对象
"""
# 导入模块
import sqlite3
# 创建连接
con = sqlite3.connect('E://Postgraduate//004.Python//001.Code//DBMS//sqlite3_demo//demo.db')
# 创建游标对象
cur = con.cursor()
# 编写sql语句
sql = 'insert into t_person(pname, age) values(?, ?) '
# 执行sql语句
try:
    cur.execute(sql, ('张三', 24))
    # 提交事务
    con.commit()
    print('插入数据成功')
except Exception as e:
    print(e)
    # 回滚
    con.rollback()
    print('插入数据失败')
finally:
    cur.close()
    con.close()

2.向数据库中插入多条数据

"""
1. 导入模块
2. 创建连接 sqlite3.connect()
3. 创建游标对象
4. 编写创建表的sql语句
5. 执行sql
6. 提交事务
7. 关闭游标对象和连接对象
"""
# 导入模块
import sqlite3
# 创建连接
con = sqlite3.connect('E://Postgraduate//004.Python//001.Code//DBMS//sqlite3_demo//demo.db')
# 创建游标对象
cur = con.cursor()
# 编写sql语句
sql = 'insert into t_person(pname, age) values(?, ?) '
# 执行sql语句
try:
    # 插入多条数据,利用列表
    cur.executemany(sql, [('小李', 24), ('小花', 34), ('小明', 28)])
    # 提交事务
    con.commit()
    print('插入多条数据成功')
except Exception as e:
    print(e)
    # 回滚
    con.rollback()
    print('插入多条数据失败')
finally:
    cur.close()
    con.close()

3.查询所有数据

  • fecthall
# 导入模块
import sqlite3
# 创建连接
con = sqlite3.connect('E://Postgraduate//004.Python//001.Code//DBMS//sqlite3_demo//demo.db')
# 创建游标
cur = con.cursor()
# 编写sql
sql = 'select * from t_person'
try:
    cur.execute(sql)
    # 获取结果集
    person_all = cur.fetchall()
    # 利用循环进行遍历
    print(person_all)	# 输出的是列表
    for person in person_all:
        print(person)
except Exception as e:
    print(e)
    print('查询所有数据失败')
finally:
    # 关闭游标
    cur.close()
    # 关闭连接
    con.close()

002.操作sqlite3插入、查询、修改、删除数据_第1张图片

4. 查询一条数据

  • fetchone
# 导入模块
import sqlite3
# 创建连接
con = sqlite3.connect('E://Postgraduate//004.Python//001.Code//DBMS//sqlite3_demo//demo.db')
# 创建游标
cur = con.cursor()
# 编写sql
sql = 'select * from t_person'
try:
    cur.execute(sql)
    # 获取结果集
    person = cur.fetchone()
    # 利用循环进行遍历
    print(person)
except Exception as e:
    print(e)
    print('查询所有数据失败')
finally:
    # 关闭游标
    cur.close()
    # 关闭连接
    con.close()

002.操作sqlite3插入、查询、修改、删除数据_第2张图片

4.修改一条数据

  • 不要忘记回滚和提交
# 导入模块
import sqlite3
# 创建连接
con = sqlite3.connect('E://Postgraduate//004.Python//001.Code//DBMS//sqlite3_demo//demo.db')
# 创建游标
cur = con.cursor()
# 编写sql
sql = 'update t_person set pname=? where pno=?'
try:
    cur.execute(sql, ('小张', 1))
    # 提交事务
    con.commit()
    print('修改成功')
except Exception as e:
    print(e)
    print('修改数据失败')
    con.rollback()
finally:
    # 关闭游标
    cur.close()
    # 关闭连接
    con.close()

5.删除一条数据

  • 不要忘记回滚和提交
# 导入模块
import sqlite3
# 创建连接
con = sqlite3.connect('E://Postgraduate//004.Python//001.Code//DBMS//sqlite3_demo//demo.db')
# 创建游标
cur = con.cursor()
# 编写sql
sql = 'delete from t_person where pno=?'
try:
    cur.execute(sql, (1,))   # 第二个参数是元组类型,元组类型中只有一个元素时,后面必须添加逗号
    # 提交事务
    con.commit()
    print('删除成功')
except Exception as e:
    print(e)
    print('删除数据失败')
    con.rollback()
finally:
    # 关闭游标
    cur.close()
    # 关闭连接
    con.close()

你可能感兴趣的:(#,数据库访问,sqlite3,数据库,python,增删查改)