Python中mysql相關
阿新 • • 發佈:2018-11-08
引言
記錄python和mysql結合使用過程中的一些問題
1 一個比較標準的mysql資料庫類宣告
注意建立連線時候的charset編碼和cursorclass等屬性,後面一個屬性是讓返回值以字典的形式返回。
注意點:
- 查詢結果返回的生成器(適用於資料比較大的情況
- 插入資料和更新資料需要注意commit()方法使用
from conf import configs as cf
import pymysql
class DB(object):
def __init__(self, table):
"""initialization and connect mysql"""
self.connect = pymysql.connect(host=cf['DB']['host'], port=cf['DB']['port'], user=cf['DB']['user'],password=cf['DB']['psw'], db=cf['DB']['db'], charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
self.cursor = self.connect.cursor()
self.table = table
print('mysql connects successfully' )
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_db):
self.cursor.close()
self.connect.close()
print('mysql is closed')
def get_data_by_filed(self, field):
"""
get value by fields, filed is like url or url,release_date.
:param field: such as url,release_date if exist more than one filed, otherwise url or release_date
:return: generator
"""
sql = "SELECT %(field)s FROM %(table)s " % {"field": field, "table": self.table}
# print(sql)
try:
self.cursor.execute(sql)
res = self.cursor.fetchall()
for r in res:
yield r
except Exception as e:
print("get_data_by_filed ERROR: ", e)
def insert_data(self, field, value):
"""
insert data
:param field: such as "(url, id)"
:param value: ('https://weibo.com/121212112/dafadfa', '1faf12e1vad)
:return:
"""
sql = "INSERT INTO %(table)s %(field)s VALUES %(value)s" % {'table': self.table, 'field': field, "value": value}
print(sql)
try:
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
self.connect.rollback()
print("insert_data ERROR:", e)
def update_values_by_ID(self, field, value, _id):
"""
update value by id
:param field: 'content'
:param value: '【擴散!#四川九寨溝地震#最全應急電話】九寨溝'
:return:
"""
sql = "UPDATE %(table)s SET %(field)s = '%(value)s' WHERE ID = '%(id)s'" % {'table': self.table, 'field': field, "value": value, "id":_id}
print(sql)
try:
self.cursor.execute(sql)
self.connect.commit()
except Exception as e:
self.connect.rollback()
print("update_value_by_ID ERROR:", value ,e)
配置檔案
configs = {
'DB': {
'user': 'root',
'psw': 'psw',
'port': 3306,
'host': '127.0.0.1',
'db': 'public_opinion'
}
}