06-python操作数据库
Python操作MySQL
安装pymysl
pip install pymysql
创建数据库表
import pymysql
db_config = {
'host':'127.0.0.1',
'user':'root',
'password':'qwe123',
'db':'test',
'charset':'utf8'
}
conn = pymysql.connect(**db_config)
try:
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
# 使用预处理语句创建表
sql = """CREATE TABLE EMPLOYEE (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""
cursor.execute(sql)
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
数据库查询
- fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
- fetchall(): 接收全部的返回结果行.
import pymysql
db_config = {
'host':'127.0.0.1',
'user':'root',
'password':'qwe123',
'db':'test',
'charset':'utf8'
}
# 打开数据库连接
conn = pymysql.connect(**db_config)
try:
# 创建一个游标对象
cursor = conn.cursor()
sql = 'select * from student'
# 使用execute()方法执行SQL查询
cursor.execute(sql)
# 使用fetchall()方法获取数据.
res = cursor.fetchall()
for data in res:
print(data)
except Exception as e:
print(e)
finally:
cursor.close()
# 关闭数据库连接
conn.close()
(901, '张老大', '男', 1985, '计算机系', '北京市海淀区')
(902, '张老二', '男', 1986, '中文系', '北京市昌平区')
(903, '张三', '女', 1990, '中文系', '湖南省永州市')
(904, '李四', '男', 1990, '英语系', '辽宁省阜新市')
(905, '王五', '女', 1991, '英语系', '福建省厦门市')
(906, '王六', '男', 1988, '计算机系', '湖南省衡阳市')
数据库插入操作
import pymysql
db_config = {
'host':'127.0.0.1',
'user':'root',
'password':'qwe123',
'db':'test',
'charset':'utf8'
}
conn = pymysql.connect(**db_config)
try:
cursor = conn.cursor()
sql = '''INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)
'''
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
conn.commit()
except:
# 如果发生错误则回滚
conn.rollback();
finally:
cursor.close()
conn.close()
使用变量向SQL语句中传递参数:
user_id = "test123"
password = "password"
sql = 'insert into Login values("%s", "%s")' % (user_id, password))
数据库更新操作
import pymysql
db_config = {
'host':'127.0.0.1',
'user':'root',
'password':'qwe123',
'db':'test',
'charset':'utf8'
}
conn = pymysql.connect(**db_config)
try:
cursor = conn.cursor()
sql = "update EMPLOYEE set age=age+1 where sex = '%c'" % ('M')
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
conn.commit()
except:
# 如果发生错误则回滚
conn.rollback();
finally:
cursor.close()
conn.close()
删除操作
import pymysql
db_config = {
'host':'127.0.0.1',
'user':'root',
'password':'qwe123',
'db':'test',
'charset':'utf8'
}
conn = pymysql.connect(**db_config)
try:
cursor = conn.cursor()
sql = "delete from EMPLOYEE where age>'%d'"%(20)
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
conn.commit()
except:
# 如果发生错误则回滚
conn.rollback();
finally:
cursor.close()
conn.close()
执行事务
事务机制可以确保数据一致性。
事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。
- 原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。
- 一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
- 隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
- 持久性(durability)。持续性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。
Python DB API 2.0 的事务提供了两个方法 commit 或 rollback。
sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)
try:
# 执行SQL语句
cursor.execute(sql)
# 向数据库提交
db.commit()
except:
# 发生错误时回滚
db.rollback()
对于支持事务的数据库, 在Python数据库编程中,当游标建立之时,就自动开始了一个隐形的数据库事务。
commit()方法游标的所有更新操作,rollback()方法回滚当前游标的所有操作。每一个方法都开始了一个新的事务。
python操作MongoDB
安装pymongo
pip install pymongo
创建一个数据库
import pymongo
client = pymongo.MongoClient('127.0.0.1',27017)
db = client["student"]
创建一个集合
import pymongo
client = pymongo.MongoClient('127.0.0.1',27017)
db = client["test"]
stu = db["students"]
插入集合
insert_one()
import pymongo
client = pymongo.MongoClient('127.0.0.1',27017)
mydb = client["test"]
mycol = mydb["students"]
mydict = {"name":"533","age":17}
x = mycol.insert_one(mydict)
print(x)
<pymongo.results.InsertOneResult object at 0xb6bb8e2c>
insert_many()
import pymongo
client = pymongo.MongoClient('127.0.0.1',27017)
mydb = client["test"]
mycol = mydb["students"]
mylist = [
{"name": "533", "age": 17},
{"name": "53", "age": 16},
{"name": "5", "age": 18},
]
x = mycol.insert_many(mylist)
# 输出插入的所有文档对应的_id值
print(x.inserted_ids)
[ObjectId('5b3200dec9fd2e06fac4056b'), ObjectId('5b3200dec9fd2e06fac4056c'), ObjectId('5b3200dec9fd2e06fac4056d')]
插入指定 _id 的多个文档
import pymongo
client = pymongo.MongoClient('127.0.0.1',27017)
my_db = client["test"]
my_col = my_db["students"]
my_list = [
{"_id": 10001, "name": "533", "age": 17},
{"_id": 10002, "name": "53", "age": 16},
{"_id": 10003, "name": "5", "age": 18},
]
x = my_col.insert_many(my_list)
print(x.inserted_ids)
[10001, 10002, 10003]
查询文档
查询一条数据find_one()
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
x = my_col.find_one()
print(x)
{'_id': 10001, 'name': '533', 'age': 17}
查询所有数据find()
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
for x in my_col.find():
print(x)
{'_id': 10001, 'name': '533', 'age': 17}
{'_id': 10002, 'name': '53', 'age': 16}
{'_id': 10003, 'name': '5', 'age': 18}
查询指定字段的数据
将要返回的字段对应值设置为 1。
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
for x in my_col.find({}, {"_id": 0, "name": 1, "age": 1}):
print(x)
{'name': '533', 'age': 17}
{'name': '53', 'age': 16}
{'name': '5', 'age': 18}
根据指定条件查询
查询name=5的数据
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_query = {"name":'5'}
my_doc = my_col.find(my_query)
for x in my_doc:
print(x)
{'_id': 10003, 'age': 18, 'name': '5'}
查询age>16的数据
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_query = {"age": {"$gt": 16}}
my_doc = my_col.find(my_query)
for x in my_doc:
print(x)
{'_id': 10001, 'age': 17, 'name': '533'}
{'_id': 10003, 'age': 18, 'name': '5'}
limit()
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_doc = my_col.find().limit(2)
for x in my_doc:
print(x)
{'_id': 10001, 'age': 17, 'name': '533'}
{'_id': 10002, 'age': 16, 'name': '53'}
修改文档
update_one()
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_query = {"age":17}
new_value = {"$set":{"age":20}}
my_col.update_one(my_query,new_value)
for x in my_col.find():
print(x)
{'age': 20, '_id': 10001, 'name': '533'}
{'age': 16, '_id': 10002, 'name': '53'}
{'age': 18, '_id': 10003, 'name': '5'}
update_many()
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_query = {"age":16}
new_value = {"$set":{"age":20}}
my_col.update_many(my_query,new_value)
for x in my_col.find():
print(x)
{'age': 20, '_id': 10001, 'name': '533'}
{'age': 20, '_id': 10002, 'name': '53'}
{'age': 18, '_id': 10003, 'name': '5'}
{'age': 20, '_id': 10004.0, 'name': '3'}
排序
sort() 方法第一个参数为要排序的字段,第二个字段指定排序规则,1 为升序,-1 为降序,默认为升序。
sort()
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_doc = my_col.find().sort("age")
for x in my_doc:
print(x)
{'_id': 10003, 'name': '5', 'age': 18}
{'_id': 10004, 'name': '3', 'age': 20}
{'_id': 10002, 'name': '53', 'age': 20}
{'_id': 10001, 'name': '533', 'age': 20}
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_doc = my_col.find().sort("age",-1)
for x in my_doc:
print(x)
{'age': 20, '_id': 10004, 'name': '3'}
{'age': 20, '_id': 10002, 'name': '53'}
{'age': 20, '_id': 10001, 'name': '533'}
{'age': 18, '_id': 10003, 'name': '5'}
删除
delete_one()
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_query = {"name":"53"}
my_col.delete_one(my_query)
for x in my_col.find():
print(x)
{'name': '533', 'age': 20, '_id': 10001}
{'name': '5', 'age': 18, '_id': 10003}
{'name': '3', 'age': 20, '_id': 10004}
delete_many()
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_query = {"age":20}
my_col.delete_many(my_query)
for x in my_col.find():
print(x)
{'_id': 10003, 'name': '5', 'age': 18}
删除集合中的所有文档
delete_many() 方法如果传入的是一个空的查询对象,则会删除集合中的所有文档:
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_col.delete_many({})
for x in my_col.find():
print(x)
删除集合
drop()
import pymongo
client = pymongo.MongoClient("127.0.0.1",27017)
my_db = client['test']
my_col = my_db['students']
my_col.drop()
python操作redis
安装redis
pip install redis
set get
import redis
pool = redis.ConnectionPool(host='127.0.0.1',port=6379,decode_responses=True)
r = redis.Redis(connection_pool=pool)
r.set("name","533")
print(r["name"])
print(r.get("name"))
print(type(r.get("name")))
533
533
<class 'str'>
mset mget
import redis
pool = redis.ConnectionPool(host='127.0.0.1',port=6379,decode_responses=True)
r = redis.Redis(connection_pool=pool)
r.mset(k1='v1',k2='v2')
print(r.mget("k1","k2"))
['v1', 'v2']
hset hget
import redis
pool = redis.ConnectionPool(host='127.0.0.1',port=6379,decode_responses=True)
r = redis.Redis(connection_pool=pool)
r.hset("hash1","k1","k2")
print(r.hget("hash1","k1"))
print(r.hkeys("hash1"))
k2
['k1']
hmset hmget
import redis
pool = redis.ConnectionPool(host='127.0.0.1',port=6379,decode_responses=True)
r = redis.Redis(connection_pool=pool)
r.hmset("hash1",{"k1":"v1","k2":"v2"})
print(r.hmget("hash1","k1","k2"))
print(r.hkeys("hash1"))
['v1', 'v2']
['k1', 'k2']
lpush lrange
import redis
pool = redis.ConnectionPool(host='127.0.0.1',port=6379,decode_responses=True)
r = redis.Redis(connection_pool=pool)
r.lpush("list1",12,24,30)
print(r.lrange("list1",0,-1))
['30', '24', '12', '3', '2', '1', '3', '2', '1']