python操作数据库学习,自封装一个类来处理
2011-09-29 18:17
716 查看
自封装一个简单的类来测试一下,挺不错。。。
import MySQLdb
class WebSiteDB:
def __init__(self, dbname = 'website', hostname = 'localhost', username = 'root', password = 'test'):
self.dbname = dbname
self.hostname = hostname
self.username = username
self.password = password
self.conn = None
self.cursor = None
self.__connectMySql()
self.__createDB()
self.__selectDB()
def __connectMySql(self):
try:
self.conn = MySQLdb.connect(host = self.hostname, user = self.username, passwd = self.password)
self.cursor = self.conn.cursor()
except:
print 'fail to connect to MySQLdb '
def __createDB(self):
try:
sqlcmd = 'create database if not exists ' + self.dbname
self.cursor.execute(sqlcmd)
except:
print 'fail to create database ' + self.dbname
return None
def __selectDB(self):
try:
self.conn.select_db(self.dbname)
except:
print 'fail to select_db ', self.dbname
def createTable(self, tablename):
try:
if tablename is None:
return None
sqlcmd = 'create table if not exists ' + tablename + '(website varchar(50), url varchar(300))'
self.cursor.execute(sqlcmd)
except:
print 'fail to create table ' + tablename
return None
def fetchOne(self, tablename):
try:
self.cursor.execute('select * from ' + tablename)
result = self.cursor.fetchone()
return result
except:
print 'fail to fetchOne'
return None
def fetchMany(self, tablename, num):
try:
self.cursor.execute('select * from ' + tablename)
results = self.cursor.fetchmany(num)
return results
except:
print 'fail to fetchMany ', num
return None
def fetchAll(self, tablename):
try:
self.cursor.execute('select * from ' + tablename)
results = self.cursor.fetchall()
return results
except:
print 'fail to fetchAll'
return None
def insertOneRd(self, tablename, value):
try:
sqlcmd = 'insert into ' + tablename + ' values(%s, %s)'
self.cursor.execute(sqlcmd, value);
except:
print 'fail to insertOneRd'
return None
def insertManyRd(self, tablename, values):
try:
sqlcmd = 'insert into ' + tablename + ' values(%s, %s)'
self.cursor.executemany(sqlcmd, values)
except:
print 'fail to insetManyRd'
return None
def droptable(self, tablename):
while True:
self.cursor.execute('show tables like "%s%%"' % tablename)
tables = self.cursor.fetchall()
if len(tables) == 0:
break
for x in tables:
try:
self.cursor.execute('drop table %s' % x)
self.conn.commit()
except:
continue
import MySQLdb
class WebSiteDB:
def __init__(self, dbname = 'website', hostname = 'localhost', username = 'root', password = 'test'):
self.dbname = dbname
self.hostname = hostname
self.username = username
self.password = password
self.conn = None
self.cursor = None
self.__connectMySql()
self.__createDB()
self.__selectDB()
def __connectMySql(self):
try:
self.conn = MySQLdb.connect(host = self.hostname, user = self.username, passwd = self.password)
self.cursor = self.conn.cursor()
except:
print 'fail to connect to MySQLdb '
def __createDB(self):
try:
sqlcmd = 'create database if not exists ' + self.dbname
self.cursor.execute(sqlcmd)
except:
print 'fail to create database ' + self.dbname
return None
def __selectDB(self):
try:
self.conn.select_db(self.dbname)
except:
print 'fail to select_db ', self.dbname
def createTable(self, tablename):
try:
if tablename is None:
return None
sqlcmd = 'create table if not exists ' + tablename + '(website varchar(50), url varchar(300))'
self.cursor.execute(sqlcmd)
except:
print 'fail to create table ' + tablename
return None
def fetchOne(self, tablename):
try:
self.cursor.execute('select * from ' + tablename)
result = self.cursor.fetchone()
return result
except:
print 'fail to fetchOne'
return None
def fetchMany(self, tablename, num):
try:
self.cursor.execute('select * from ' + tablename)
results = self.cursor.fetchmany(num)
return results
except:
print 'fail to fetchMany ', num
return None
def fetchAll(self, tablename):
try:
self.cursor.execute('select * from ' + tablename)
results = self.cursor.fetchall()
return results
except:
print 'fail to fetchAll'
return None
def insertOneRd(self, tablename, value):
try:
sqlcmd = 'insert into ' + tablename + ' values(%s, %s)'
self.cursor.execute(sqlcmd, value);
except:
print 'fail to insertOneRd'
return None
def insertManyRd(self, tablename, values):
try:
sqlcmd = 'insert into ' + tablename + ' values(%s, %s)'
self.cursor.executemany(sqlcmd, values)
except:
print 'fail to insetManyRd'
return None
def droptable(self, tablename):
while True:
self.cursor.execute('show tables like "%s%%"' % tablename)
tables = self.cursor.fetchall()
if len(tables) == 0:
break
for x in tables:
try:
self.cursor.execute('drop table %s' % x)
self.conn.commit()
except:
continue
相关文章推荐
- Python学习笔记(三)Python数据库操作。
- 学习python 第三季:编写简单简单连接数据库并执行查询操作
- Linux学习笔记--Python操作mysql数据库(封装基本的增删改查)
- Python学习笔记十(pymssql数据库操作)
- 学习python之编写简单简单连接数据库并执行查询操作
- Python学习之数据库操作
- python3的异常处理、操作excel和数据库
- Connection DBUtils (封装了原始JDBC对数据库的操作的一个开源类库)
- 封装JAVA数据库操作-学习
- Python 学习笔记4 - 数据库操作
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- Django从零开始搭建一个相册网站--5.Python操作数据库
- Python学习之使用Python操作Redis数据库
- 一个数据库操作类,封装所有数据库操作
- 一个简单的数据库操作类、封装了一些简单的操作
- Python 脚本学习笔记(五)集中式病毒扫描,端口扫描以及分段数据库操作
- Python入门学习教程:数据库操作,连接MySql数据库
- Python 学习笔记三 操作PostgreSQL数据库
- Spring学习心得(22)-- spring声明式事务处理(使用jdbc操作数据库)
- C#中一个操作数据库的类的封装,以前写的,被同学改过,很不完善