主要是通过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数据库操作函数,直接将数据库操作封装为函数,可直接调用。
python数据库操作,适合嵌入式开发,方便简单。
Python连接Access数据库Python连接Access数据库Python连接Access数据库Python连接Access数据库Python连接Access数据库Python连接Access数据库Python连接Access数据库Python连接Access数据库Python连接Access数据库...
详细介绍Python操作数据库,包括数据库的简单介绍、常见的SQL语句、Python操作数据库的核心API等,并通过一个综合案例演示Python操作SQLite数据库的过程,非常适合高校教师教学和学生课后复习使用。
python 连接达梦数据库
第15章 使用Python操作数据库.pdf
python操作mysql数据库.pdf
Python 使用Pandas实现数据库的读、写操作 Python源码Python 使用Pandas实现数据库的读、写操作 Python源码Python 使用Pandas实现数据库的读、写操作 Python源码Python 使用Pandas实现数据库的读、写操作 Python源码...
Python数据库操作库研究
Python的数据库操作。 Python语言本身的易读,易用,使得许多技术开发都使用它。 文档中介绍了Python语言实现数据库的各种操作的方法。值得一看。
Python数据库操作库研究.pdf
完整版 零基础学Python课件11 第11章 使用Python操作数据库.ppt
基于python语言的使用Python操作数据库背记手册
Python 数据库编程 09操作MySQL数据库插入数据.mp4
Python 数据库编程 08操作MySQL数据库创建表.mp4
Python 数据库编程 10操作MySQL数据库查询数据.mp4
在Python中操作Mysql数据库的配置方法及最基本的数据库操作演示代码
Python数据库操作思维导图,便捷整理思路,库、库的导入、创建连接、创建游标、执行SQL语句、关闭连接
Python 数据库编程 02操作sqlite3数据库插入数据.mp4