1. 程式人生 > >流行數據庫接口

流行數據庫接口

fetch from comm 是否阻塞 error lar http sed 不知道

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

  

流行數據庫接口