以MYSQL為例蔫劣。
import pymysql
import redis
def conn(u, p, h, o, d):
# 連接數(shù)據(jù)庫(MYSQL)
# u/用戶名: str 'edaijia'
# p/登錄密碼: str '12345678'
# h/數(shù)據(jù)庫服務(wù)器IP: str '121.40.43.10'
# o/數(shù)據(jù)庫服務(wù)器端口: num 3306
# d/數(shù)據(jù)庫名: str 'db_report'
try:
global thisDb
global connStr
connStr = pymysql.connect(
user=u,
passwd=p,
host=h,
port=o,
db=d)
connStr.autocommit(True)
thisDb = connStr.cursor()
except Exception as err:
print('數(shù)據(jù)庫無法連接: %s' % err)
def query(sqlStr):
# 返回數(shù)據(jù)庫查詢結(jié)果
# sqlStr/被執(zhí)行SQL: str 'select * from user order by created desc limit 1'
try:
thisDb.execute(sqlStr)
# connStr.commit()
results = thisDb.fetchall()
return results
except Exception as err:
print('SQL執(zhí)行錯誤: %s' % err)
def execute(sqlStr):
# 返回執(zhí)行SQL后影響行數(shù)
# sqlStr/被執(zhí)行SQL: str 'select * from user order by created desc limit 1'
try:
results = thisDb.execute(sqlStr)
connStr.commit()
# results = thisDb.fetchall()
return results
except Exception as err:
print('SQL執(zhí)行錯誤: %s' % err)
def close():
try:
thisDb.close()
except Exception as err:
print('無連接可關(guān)閉: %s' % err)
def refreshRedis(cusPhone):
# 根據(jù)顧客電話清楚REDIS緩存
# cusPhone/客戶電話: str '13701070000'
try:
re = redis.Redis(host='121.40.43.10', port=22125, db=0, password='k74FkBwb7252FsbNk2M7')
cusPhone = 'GatherPhone_' + cusPhone
re.delete(cusPhone)
# print('清理REDIS = true')
except Exception as err:
print('清理REDIS報錯: %s' % err)