流行数据库接口

pymysql

 1 # -*- coding: utf-8 -*-
 2 
 3 """
 4 @Datetime: 2018/12/26
 5 @Author: Zhang Yafei
 6 """
 7 import pymysql
 8 from DBUtils.PooledDB import PooledDB
 9 
10 POOL = PooledDB(
11     creator=pymysql,  # 使用链接数据库的模块
12     maxconnections=6,  # 连接池允许的最大连接数,0和None表示不限制连接数
13     mincached=2,  # 初始化时,链接池中至少创建的空闲的链接,0表示不创建
14     maxcached=5,  # 链接池中最多闲置的链接,0和None不限制
15     maxshared=3,
16     # 链接池中最多共享的链接数量,0和None表示全部共享。PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。
17     blocking=True,  # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
18     maxusage=None,  # 一个链接最多被重复使用的次数,None表示无限制
19     setsession=[],  # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."]
20     ping=0,
21     # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
22     host='127.0.0.1',
23     port=3306,
24     user='root',
25     password='0000',
26     database='flask_code',
27     charset='utf8'
28 )
29 
30 
31 def connect(type=None):
32     conn = POOL.connection()
33     cursor = conn.cursor(cursor=type)
34     return conn, cursor
35 
36 
37 def connect_close(conn, cursor):
38     cursor.close()
39     conn.close()
40 
41 
42 def fetchone(sql, arg=list()):
43     conn, cursor = connect(type)
44     cursor.execute(sql, arg)
45     data = cursor.fetchone()
46     connect_close(conn, cursor)
47     return data
48 
49 
50 def fetchall(sql, arg=list(), type=pymysql.cursors.DictCursor):
51     conn, cursor = connect(type)
52     cursor.execute(sql, arg)
53     data = cursor.fetchall()
54     connect_close(conn, cursor)
55     return data
56 
57 
58 def insert(sql, arg=list()):
59     conn, cursor = connect()
60     row = cursor.execute(sql, arg)
61     conn.commit()
62     connect_close(conn, cursor)
63     return row
mysql_helper

sqlite

# -*- coding: utf-8 -*-

"""
@Datetime: 2019/1/31
@Author: Zhang Yafei
"""
import sqlite3
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DB_DIR = os.path.join(BASE_DIR, 'data.db')


class SqliteDB(object):
    def __init__(self):
        self.conn = sqlite3.connect(DB_DIR)  # db不存在时将自动创建db
        self.cursor = self.conn.cursor()

    def close(self):
        self.cursor.close()
        self.conn.close()

    def execute(self, sql, params=tuple()):
        self.cursor.execute(sql, params)
        self.close()

    def fetchone(self, sql, params=tuple()):
        result = self.cursor.execute(sql, params)
        data = result.fetchone()
        self.close()
        return data

    def fetchall(self, sql, params=tuple()):
        results = self.cursor.execute(sql, params)
        data = results.fetchall()
        self.close()
        return data


if __name__ == '__main__':
    sqlite = SqliteDB()
    # 1. 建表
    sql = '''create table happy(
             username text,
             password text,
             id int)'''
    sqlite.execute(sql)

    # 2. 插入数据
    sqlite.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
          VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )")

    # 3. 更改数据
    sqlite.execute("UPDATE COMPANY SET  ID=99  WHERE ID=2")

    # 4. 删除表里面的数据
    c.execute("DELETE FROM COMPANY WHERE ID=4")
    c.execute("DELETE FROM COMPANY WHERE ID=3")

    # 5. 查询
    data = sqlite.fetchall('select * from label limit 1')
    print(data)
    # 输出
    '''
    [('盘龙云海(排毒养颜胶囊)', 509881, '广东深圳龙岗区/女', '昨天吃的,今天就拉肚子了。感觉肚子有点涨痛!不知道效果怎么样~~~~~',
      '昨天/吃/的/,/今天/就/拉肚子/SB了/。/感觉/肚子/PB有点/涨痛/SB!/不/知道/效果/怎么样/~/~/~/~/~', '2011-09-30 15:26:00',
      'http://ypk.39.net/509881/comment/k0_p...', '昨天/吃/的/,/今天/就/拉肚子/SB了/。/感觉/肚子/PB有点/涨痛/SB!/不/知道/效果/怎么样/~/~/~/~/~',
      '昨天/吃/的/,/今天/就/拉肚子/SB了/。/感觉/肚子/PB有点/涨痛/SB!/不/知道/效果/怎么样/~/~/~/~/~')]
    '''
sqlite_helper

mongodb

  1 # -*- coding: utf-8 -*-
  2 
  3 """
  4 @Datetime: 2019/1/31
  5 @Author: Zhang Yafei
  6 """
  7 import json
  8 import pymongo
  9 import pandas as pd
 10 
 11 
 12 class MongoPipeline(object):
 13     """
 14     mongodb:
 15         save(self, data, collection):                    将数据保存到数据库
 16         read(self, data):                                读取数据库中指定表格
 17         insert(self, table, dict_data):                 插入数据
 18         delete(self, table, condition):                 删除指定数据
 19         update(self, table, condition, new_dict_data):  更新指定数据
 20         dbFind(self, table, condition=None):             按条件查找
 21         findAll(self, table):                           查找全部
 22         close(self):                                    关闭连接
 23     """
 24 
 25     def __init__(self, mongo_db, mongo_uri='localhost'):
 26         self.mongo_uri = mongo_uri
 27         self.mongo_db = mongo_db
 28         self.client = pymongo.MongoClient(self.mongo_uri)
 29         self.db = self.client[self.mongo_db]
 30 
 31     def close(self):
 32         """
 33         关闭连接
 34         :return:
 35         """
 36         self.client.close()
 37 
 38     def save(self, data, collection):
 39         """
 40         将数据保存到数据库表
 41         :param data:
 42         :param collection:
 43         :return: None
 44         """
 45         self.collection = self.db[collection]
 46         try:
 47             if self.collection.insert(json.loads(data.T.to_json()).values()):
 48                 print('mongodb insert {} sucess.'.format(collection))
 49                 return
 50         except Exception as e:
 51             print('insert error:', e)
 52             import traceback
 53             traceback.print_exc(e)
 54 
 55     def read(self, table):
 56         """
 57         读取数据库中的数据
 58         :param table:
 59         :return: dataframe
 60         """
 61         try:
 62             # 连接数据库
 63             table = self.db[table]
 64             # 读取数据
 65             data = pd.DataFrame(list(table.find()))
 66             return data
 67         except Exception as e:
 68             import traceback
 69             traceback.print_exc(e)
 70 
 71     def insert(self, table, dict_data):
 72         """
 73         插入
 74         :param table:
 75         :param dict_data:
 76         :return: None
 77         """
 78         try:
 79             self.db[table].insert(dict_data)
 80             print("插入成功")
 81         except Exception as e:
 82             print(e)
 83 
 84     def update(self,table, condition, new_dict_data):
 85         """
 86         更新
 87         :param table:
 88         :param dict_data:
 89         :param new_dict_data:
 90         :return: None
 91         """
 92         try:
 93             self.db[table].update(condition, new_dict_data)
 94             print("更新成功")
 95         except Exception as e:
 96             print(e)
 97 
 98     def delete(self, table, condition, one=False):
 99         """
100         按条件删除
101         :param table:
102         :param dict_data:
103         :return: None
104         """
105         try:
106             if one:
107                 self.db[table].delete_one(condition)
108                 print('删除成功')
109             else:
110                 result = self.db[table].delete_many(condition)
111                 print('删除成功')
112                 return result
113         except Exception as e:
114             print(e)
115 
116     def drop(self, table):
117         """
118         删除整个集合
119         :param table:
120         :return:
121         """
122         try:
123             self.db[table].drop()
124             print('删除{}成功'.format(table))
125         except Exception as e:
126             print(e)
127 
128     def dbFind(self, table, condition=None):
129         """
130         按条件查找
131         :param table:
132         :param dict_data:
133         :return: generator dict
134         """
135         data = self.db[table].find(condition)
136         for item in data:
137             yield item
138 
139     def findAll(self, table):
140         """
141         查找全部
142         :param table:
143         :return: generator dict
144         """
145         for item in self.db[table].find():
146             yield item
147 
148 
149 if __name__ == '__main__':
150     mongo = MongoPipeline('flask')
151     # data = mongo.read('label')
152     # print(data.head())
153     condition = {"药品ID": 509881}
154     data = mongo.dbFind('label', condition)
155     print(data)
156     for i in data:
157         print(i)
158     # mongo.findAll()
mongo_helper

  

猜你喜欢

转载自www.cnblogs.com/zhangyafei/p/10343535.html