主要是通過python的DBUtils庫、MySQLdb庫來實現連接池操作數據庫
import MySQLdb
from DBUtils.PooledDB import PooledDB
class DBHelper(object):
__pool = None
def __init__(self):
self._conn = DBHelper.__getConnection()
self._cursor = self._conn.cursor()
@staticmethod
def __getConnection():
if DBHelper.__pool is None :
__pool = PooledDB(creator = MySQLdb,
mincached=1,
maxcached=20,
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd = '123456',
db = 'test')
return __pool.connection()
def execute(self, sql, parameter=None):
if parameter is None:
self._cursor.execute(sql)
else :
self._cursor.execute(sql, parameter)
def readOne(self, sql, parameter=None):
if parameter is None :
count = self._cursor.execute(sql)
else :
count = self._cursor.execute(sql, parameter)
if count > 0:
return self._cursor.fetchone()
else :
return None
def readList(self, sql, parameter=None):
if parameter is None :
count = self._cursor.execute(sql)
else :
count = self._cursor.execute(sql, parameter)
if count > 0:
return self._cursor.fetchall()
else :
return None
def commint(self):
self._conn.commit()
def close(self):
if self._cursor :
self._cursor.close()
if self._conn :
self._conn.close()
《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm
《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm
Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm
在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm
Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡