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()
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特性。
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()方法回滚当前游标的所有操作。每一个方法都开始了一个新的事务。
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"]
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)
object at 0xb6bb8e2c>
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')]
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]
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}
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'}
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'}
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 为降序,默认为升序。
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'}
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}
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()
pip install redis
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'>
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']
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']
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']
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']