? ? ? ? ? ? return results
? ? ? ? except MySQLdb.Error, e:
? ? ? ? ? ? print "Mysql Error %d: %s" % (e.args[0], e.args[1])
? ? ? ? ? ?
? ? # select all records
? ? def selectAll(self, name):
? ? ? ? try:
? ? ? ? ? ? self.cur.execute('select * from ' + name + ';')
? ? ? ? ? ? self.cur.scroll(0, mode='absolute') # reset cursor location (mode = absolute | relative)
? ? ? ? ? ? results = self.cur.fetchall()
? ? ? ? ? ? return results
? ? ? ? except MySQLdb.Error, e:
? ? ? ? ? ? print "Mysql Error %d: %s" % (e.args[0], e.args[1])
? ? ? ? ? ?
? ? # delete a record
? ? def deleteByID(self, name, id):
? ? ? ? try:
? ? ? ? ? ? self.cur.execute('delete from ' + name + ' where id=%s;', id)
? ? ? ? except MySQLdb.Error, e:
? ? ? ? ? ? print "Mysql Error %d: %s" % (e.args[0], e.args[1])
? ?
? ? # delete some record
? ? def deleteSome(self, name):
? ? ? ? pass
? ?
? ? # drop the table
? ? def dropTable(self, name):
? ? ? ? try:
? ? ? ? ? ? self.cur.execute('drop table ' + name + ';')
? ? ? ? except MySQLdb.Error, e:
? ? ? ? ? ? print "Mysql Error %d: %s" % (e.args[0], e.args[1])
? ? ? ? ? ?
? ? # drop the database
? ? def dropDB(self, name):
? ? ? ? try:
? ? ? ? ? ? self.cur.execute('drop database ' + name + ';')
? ? ? ? except MySQLdb.Error, e:
? ? ? ? ? ? print "Mysql Error %d: %s" % (e.args[0], e.args[1])
? ? ? ? ? ?
? ? def __del__(self):
? ? ? ? if self.cur != None:
? ? ? ? ? ? self.cur.close()
? ? ? ? if self.conn != None:
? ? ? ? ? ? self.conn.close()
使用范例:
testHeroDB.py
#!/usr/bin/env python
import MySQLdb
from heroDB import HeroDB
def main():
? ? conn = MySQLdb.connect(host='localhost', user='root', passwd='260606', db='hero', port=3306, charset='utf8')
? ? cur = conn.cursor()
? ?
? ? # ------------------------------------------- create -----------------------------------------------------
? ? hero = HeroDB('hero', conn, cur)
? ? hero.createTable('heros')
? ?
? ? # ------------------------------------------- insert -----------------------------------------------------
? ? hero.insert('heros', [3, 'Prophet', 0, 2000, 'The hero who in fairy tale.'])
? ? # ------------------------------------------- select -----------------------------------------------------
? ? print '-' * 60
? ? print 'first record'
? ? result = hero.selectFirst('heros')
? ? print result
? ?
? ? print '-' * 60
? ? print 'last record'
? ? result = hero.selectLast('heros')
? ? print result
? ?
? ? print '-' * 60
? ? print 'more record'
? ? results = hero.selectNRecord('heros', 3)
? ? for item in results:
? ? ? ? print item
? ?
? ? print '-' * 60
? ? print 'all record'
? ? results = hero.selectAll('heros')
? ? for item in results:
? ? ? ? print item
? ? ? ?
? ? # ------------------------------------------- update -----------------------------------------------------
? ? hero.updateSingle('heros', ['Zeus', 1, 22000, 'The god.', 2])
? ?
? ? values = []
? ? values.append(['SunWukong', 1, 1300, 'The hero who in fairy tale.', 1])
? ? values.append(['Zeus', 1, 50000, 'The king who in The Quartet myth.', 2])
? ? values.append(['Prophet', 1, 20000, 'The hero who in fairy tale.3', 3])
? ? hero.update('heros', values)
? ?
? ? # ------------------------------------------- delete -----------------------------------------------------
? ? hero.deleteByID('heros', 1)
? ?
? ? hero.dropTable('heros')
? ?
? ? hero.dropDB('hero')
? ?
if __name__ == '__main__':
? ? main()
注:请不要不假思索地使用他们。如果你想实现某一个功能点,请最好将其他的功能点注释掉,这样才符合单元测试的规范。
源码下载:
------------------------------------------分割线------------------------------------------
具体下载目录在 /2015年资料/4月/21日/Python访问MySQL数据库并实现其增删改查功能/
------------------------------------------分割线-