Python operates three major databases

Python operates three major databases

When we develop software in the Python language, we often use three major databases to store data: Mysql, MongoDB, and Redis;
as a review, rewrite it today to facilitate the review of the usage of Python to operate major databases in the future;

Python operation Mysql

DDL (Database Definition Statement)

    create table/database
    alter   table/database
    drop   table/database

DML (Data Management Statement)

insert 	新增
delete	删除
update	修改
select	查询

In python2, most of the libraries connected to Mysql use MySQLdb, but this library does not officially support Python3, so we can
directly pip install pymysql with PyMySQL

import pymysql

db = pymysql.connect(host='localhost',port=3306,user='root',password='12345')   # 连接mysql,声明一个连接对象db
cursor = db.cursor()    # 获取操作游标
cursor.execute('select version()')# 查询数据库版本,返回的是查询到的数量
# version = cursor.execute('select version()')    # 使用游标执行sql语句
data = cursor.fetchone()    # 获得第一条数据,也就得到了版本号
print(data)

----------------------------------结果
('5.7.17-log',)

Query the database:

cursor.execute('show databases')
databases = cursor.fetchall()	# fetchall()方法获取所有结果,元组的形式
print(databases)
----------------------------
(('information_schema',), ('dailyfresh',), ('hive',), ('ihome',), ('images360',), ('lagou',), ('movie',), ('mxonline',), ('mysql',), ('news',), ('performance_schema',), ('solar',), ('sougou_data_result',), ('spiders',), ('student',), ('sys',), ('testdjango',))

Create the database:

cursor.execute('create database test_Mysql default character set utf8')# 创建数据库,使用默认utf8编码

Create a table:
After creating the database test_Mysql, if we want to use the database, we only need to specify one more db parameter when linking

import pymysql

db = pymysql.connect(host='localhost',port=3306,user='root',password='12345',db='test_Mysql')
cursor = db.cursor()    # 获取操作游标
# 创建表
sql = '''create table if not exists students (id varchar(15) not null, name varchar(255) not null, age INT not null, primary key(id))'''
cursor.execute(sql)
db.close()

insert data

id = '20120203'
name = 'Li'
age = 18
sql = '''insert into students(id,name,age) values(%s,%s,%d)'''  # 格式化字符%s代替value值
try:
    cursor.execute(sql,(id,name,age))# 第一个参数传入sql语句,第二个参数传入value值,用元组的方式
    db.commit() # 提交事务
except:
    db.rollback() # 错误回滚
db.close()

Four properties of things:

Attributes explain
atomicity A transaction is an inseparable unit of work, and the injection operations included in the transaction are either done or not done at all
consistency Consistency means that a transaction must bring the database from one consistent state to another consistent state, that is, a transaction must be in a consistent state before and after execution. For example, assuming that the sum of the money of user A and user B is 1000, then no matter how A and B are transferred or transferred several times, the sum of the money of the two users after the transaction ends should still be 1000, this is the consistency of the transaction
isolation Isolation means that when multiple users access the database concurrently, such as operating the same table at the same time, the transaction opened by the database for each user cannot be interfered by the operations of other transactions, and multiple concurrent transactions must be isolated from each other. Transactional isolation The database provides multiple isolation levels
Endurance Durability means that once a transaction is committed, the changes to the data in the database are permanent, and the operation of committing the transaction will not be lost even if the database system encounters a failure.
--------- ------------------------------

Insert, update, delete operations are all operations to change the database, and the change operation must be a transaction, so the standard writing of these operations is:

try:
	cursor.execute(sql)
	db.commit()
except:
	db.rollback()

Inserting data can also implement dynamic insertion in the form of a dict
dictionary , which is very convenient for Python crawler data collection.

data = {
	'id': '123456',
	'name':‘ligang’
	'age':20
} 
table = 'students'
keys = ','.join(data.keys())
values = ','.join(['%s']*len(data))
sql = 'insert into {table}({keys}) values ({values})'.format(table=table, keys=keys,values=values)
try:
	if cursor.execute(sql, tuple(data.values())):
		print('Successful')
		db.commit()
except:
		print('Filed')
db.close()

update data

sql = ‘update students set age = %s where name = %s’
cursor.execute(sql, (20,'Bob'))

Inquire

sql = 'select * from students where age>=20'
print('row count:', cursor.execute(sql))
cursor.fetchone()	# 获取一条
cursor.fetchall()	# 获取剩余全部 ,二重元组的形式,如果数据量大,内存开销便会增大

Python operation MongoDB

  1. Connect to MongoDB
from pymongo import MongoClient
from datetime import datetime
# 方式1
# client = MongoClient()
# 方式2
# client2 = MongoClient('localhost',27017)# ip, 端口
# 方式三
# client3 = MongoClient('mongodb://localhost:27017')# 直接传入字符串mongodb开头
# print(client2.HOST)
# print(client.list_database_names())
# client.close() # 关闭依然可以用。自带线程池 当重新访问 数据库会自动连接
# print(client.list_database_names())
  1. Operation CRUD
**  ****** 将连接封装成为类,用面向对象的方式去调用方法 *****   **
class TetMongo(object):
    def __init__(self):
        self.client = MongoClient()  # 建立连接 默认本地
      #  self.db = self.client['test'] # 没有自动创建
        self.db = self.client.test   # 链接数据库 和上边一样
    def add_one(self):
        """新增数据"""
        student = {
            'id': '135136',
            'name': '老李',
            'age': '22',
            'gender': '男',
            'time': datetime.now() # 自动添加字段,这个时间可以用原生的python时间不需要加处理
        }
        return self.db.students.insert_one(student)
        # 除了插入一条之外 insert插入多条采用【{stu1},{stu2}】列表嵌套字典的方式
        # save() 也可以保存多条,但是要遍历列表 速度慢
    def get_one_from_id(self,stu_id):
        '''查询指定id的数据'''
        return self.db.students.find_one({'id':str(stu_id)}) # 查找一条 find_one
    def get_more_from_gender(self,gender):
        '''查询多个性别为男的数据'''
        return self.db.students.find({'gender':str(gender)})  # 查找多条 find

    def find_all(self):
        return self.db.students.find()  # 直接调用不传参数 默认查找该集合全部数据

    #    (>)  大于 - $gt
    #    (<)  小于 - $lt
    #    (>=)  大于等于 - $gte
    #    (<= )  小于等于 - $lte
    # 例:查询集合中age大于25的所有记录
    # for i in my_set.find({"age": {"$gt": 25}}):
    #     print(i)
    def update_from_id(self):
        rest = self.db.students.update_one({'name':'老王'},{'$set':{'gender':'女'}})
       # rest = self.db.students.update_many({},{'$set':{'gender':'女'}})
        print(rest)
    def remove(self):
        # return self.db.students.delete_one({'id':str(id)}) # 删除一条
        return self.db.students.remove() # 删除所有记录
        # return self.db.students.remove({'name':'老王'})
def main():
    obj = TetMongo()
    # result = obj.add_one()
    # print(result.inserted_id)

    # result = obj.get_one_from_id(123415)
    # print(result)
    #
    result_list = obj.get_more_from_gender('男')
    for res in result_list:
        print(res)

    # all_stu = obj.find_all()
    # for res in all_stu:
    #     print(res)
    # # obj.update_from_id()
    #
    res1=obj.remove()
    print(res1)
if __name__ == '__main__':
    main()

Python operation Redis

  1. simple mode
from redis import StrictRedis, ConnectionPool
redis_url="redis://:[email protected]:6379/15"
pool = ConnectionPool.from_url(redis_url, decode_responses=True)
r= StrictRedis(connection_pool=pool)
  1. Simple packaging
REDIS_URL = 'redis://:{password}@{host}:{port}/{db}'.format(
    **DEFAULT_REDIS_CONF)
    
def operator_status(func):
    """
    get operatoration status
    """

    def gen_status(*args, **kwargs):
        error, result = None, None
        try:
            result = func(*args, **kwargs)
        except Exception as e:
            error = str(e)
        return {
    
    'result': result, 'error': error}

    return gen_status


class Es2Redis:
    def __init__(self):
        if not hasattr(Es2Redis, 'pool'):
            Es2Redis.create_pool()
        self._connection = redis.Redis(connection_pool=Es2Redis.pool)

    @staticmethod
    def create_pool():
        Es2Redis.pool = redis.ConnectionPool.from_url(REDIS_URL, decode_components=True)

    @operator_status
    def set_data(self, key, value):
        """
        set data with (key, value)
        """
        return self._connection.set(key, value)

    @operator_status
    def get_data(self, key):
        """
        get data by key
        """
        return self._connection.get(key)

    @operator_status
    def del_data(self, key):
        """
        delete cache by key
        """
        return self._connection.delete(key)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324513113&siteId=291194637