操作SQLite3数据库
python3标准库中内置了SQLite3模块,可以支持SQLite3数据库的访问和相关数据库操作。
python操作SQLite3数据库的基本流程
1.导入相关库或模块。
2.使用connect()连接数据库并获取数据库连接对象
connect提供的方法:
方法 | 说明 |
---|
.cursor() | 创建一个游标对象 |
.commit() | 处理事务提交 |
.rollback() | 处理事务回滚 |
.close() | 关闭一个数据库连接 |
3.使用con.cursor()获取游标对象。
4.使用游标对象的方法(execute()、executemany()、fetchall()等)来操作数据库,实现插入、修改和删除操作,并查询获取显示相关的记录。
在python程序中,连接函数sqlite3.connect()有两个常用参数:
- database:表示要访问的数据库名。
- timeout:表示访问数据的超时设定。
5.使用close()关闭游标对象和数据库连接,数据库操作完成之后,必须关闭数据库连接,这样可以减轻数据库服务器压力。
使用sqlite3创建表
使用sqlite3模块的connect方法创建或打开数据库,需要指定数据库路径,不存在则会创建一个新的数据库。
1
| con = sqlite3.connect('e:/sqlitedb/first.db')
|
例:使用sqlite3创建表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import sqlite3
con = sqlite3.connect("e:/sqlite3Demo/demo.db")
cur = con.cursor()
sql = """create table t_person( pno INTEGER PRIMARY KEY autoincrement, panme VARCHAR NOT NULL, age INTEGER )"""
try: cur.execute(sql) print("创建表成功") except Exception as e: print(e) print("创建失败") finally: cur.close() con.close()
|
使用sqlite3插入数据
调用游标对象的execute()方法执行插入的sql,使用executemany()执行多条sql语句,使用executemany()比循环使用execute()执行多条sql语句效率高。
例1:使用sqlite3插入一条数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import sqlite3
con = sqlite3.connect("e:/sqlite3Demo/demo.db")
cur = con.cursor()
sql = "insert into t_person(pname, age) values(?, ?)"
try: cur.execute(sql, ("张三", 22)) con.commit() print("插入数据成功")
except Exception as e: print(e) print("插入数据失败") con.rollback()
finally: cur.close() con.close()
|
例2:使用sqlite3插入多条数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import sqlite3
con = sqlite3.connect("e:/sqlite3Demo/demo.db")
cur = con.cursor()
sql = "insert into t_person(pname, age) values(?, ?)"
try: cur.executemany(sql, [("李四", 21), ("小明", 24), ("无名", 25)]) con.commit() print("插入多条数据成功")
except Exception as e: print(e) print("插入数据失败") con.rollback()
finally: cur.close() con.close()
|
使用sqlite3查询数据
游标对象提供了fetchall()和fetchone()方法查询数据库数据,fetchall()方法获得所有数据,返回一个列表,fetchone()方法获取其中一个结果,返回一个元组。
例1:fetchall()查询所有数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import sqlite3
con = sqlite3.connect("e:/sqlite3Demo/demo.db")
cur = con.cursor() sql = "select * from t_person" try: cur.execute(sql) person_all = cur.fetchall() for p in person_all: print(p) print("查询数据成功")
except Exception as e: print(e) print("查询数据失败") con.rollback()
finally: cur.close() con.close()
|
例2:fetchone()查询一条数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import sqlite3
con = sqlite3.connect("e:/sqlite3Demo/demo.db")
cur = con.cursor() sql = "select * from t_person" try: cur.execute(sql) person_one = cur.fetchone() print(person_one) print("查询数据成功")
except Exception as e: print(e) print("查询数据失败") con.rollback()
finally: cur.close() con.close()
|
使用sqlite3修改或删除数据
例1:修改一条数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import sqlite3
con = sqlite3.connect("e:/sqlite3Demo/demo.db")
cur = con.cursor()
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()
|
例2:删除数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import sqlite3
con = sqlite3.connect("e:/sqlite3Demo/demo.db")
cur = con.cursor()
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()
|
Python调用Pymysql库使用Mysql
python安装pymysql
操作Pymysql数据库创建表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import pymysql
con = pymysql.connect(host="localhost", user="root", password="QQ524900", database="python_db", port=3306)
cur = con.cursor()
sql = """ create table t_student( son int primary key auto_increment, sname varchar(30) not null, age int(2), score float(3, 1) ) """ try: cur.execute(sql) print("创建表成功") except Exception as e: print(e) print("创建失败") finally: cur.close() con.close()
|
操作Pymysql数据库插入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import pymysql
con = pymysql.connect(host="localhost", user="root", password="QQ524900", database="python_db", port=3306)
cur = con.cursor()
sql = """ insert into t_student(sname, age, score) values(%s, %s, %s) """ try: cur.execute(sql, ("小明", 23, 98.2)) con.commit() print("插入数据成功") except Exception as e: print(e) con.rollback() print("插入数据失败") finally: cur.close() con.close()
|
操作Pymysql数据库插入多条数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import pymysql
con = pymysql.connect(host="localhost", user="root", password="QQ524900", database="python_db", port=3306, charset = 'utf8')
cur = con.cursor()
sql = """ insert into t_student(sname, age, score) values(%s, %s, %s) """ args = [("张三", 23, 98.2), ("李四", 25, 99.4)] try: cur.executemany(sql, args) con.commit() print("插入数据成功") except Exception as e: print(e) con.rollback() print("插入数据失败") finally: cur.close() con.close()
|
操作Pymysql数据库查询所有数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import pymysql
con = pymysql.connect(host="localhost", user="root", password="QQ524900", database="python_db", port=3306, charset = 'utf8')
cur = con.cursor()
sql = "select * from t_student where age>=22" try: cur.execute(sql) results = cur.fetchall() for r in results: sno = r[0] sname = r[1] age = r[2] score = r[3] print("sno:", sno, "sname:", sname, "age:", age, "score:", score) print("查询数据成功") except Exception as e: print(e) print("查询数据失败") finally: cur.close() con.close()
|
操作Pymysql数据库查询一条数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import pymysql
con = pymysql.connect(host="localhost", user="root", password="QQ524900", database="python_db", port=3306, charset = 'utf8')
cur = con.cursor()
sql = "select * from t_student where age>=22" try: cur.execute(sql) results = cur.fetchone() print("sno:", results[0], "sname:", results[1], "age:", results[2], "score:", results[3]) print("查询数据成功") except Exception as e: print(e) print("查询数据失败") finally: cur.close() con.close()
|
操作Pymysql数据库修改数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import pymysql
con = pymysql.connect(host="localhost", user="root", password="QQ524900", database="python_db", port=3306, charset = 'utf8')
cur = con.cursor()
sql = "update t_student set sname=%s where sno=%s" try: cur.execute(sql, ("五方", 2)) con.commit() print("修改数据成功") except Exception as e: print(e) con.rollback() print("修改数据失败") finally: cur.close() con.close()
|
操作Pymysql数据库删除数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import pymysql
con = pymysql.connect(host="localhost", user="root", password="QQ524900", database="python_db", port=3306, charset = 'utf8')
cur = con.cursor()
sql = "delete from t_student where sname=%s" try: cur.execute(sql, ("五方")) con.commit() print("删除数据成功") except Exception as e: print(e) con.rollback() print("删除数据失败") finally: cur.close() con.close()
|