使用pyexcelerator-0.6.3a读取excel并写入mysql数据库

编码解码(网上搜的)
  1. # encoding.py
  2. '''
  3. module to help encode and/or decode string.
  4. no rights reserved.
  5. '''
  6. import sys
  7. defaultencoding = sys.stdout.encoding
  8. def str2uni(string, enc = defaultencoding):
  9.     '''convert str to unicode
  10.     you can specify encoding. default to local encoding.
  11.     string can be a int.
  12.     '''
  13.     if isinstance(string, str):
  14.         return unicode(string, enc)
  15.     elif isinstance(stringint):
  16.         return unicode(str(string), enc)
  17.     else:
  18.         return string
  19. def uni2str(string, enc = defaultencoding):
  20.     '''convert unicode to str
  21.     you can specify encoding. default to local encoding.
  22.     '''
  23.     if isinstance(string, unicode):
  24.         return string.encode(enc)
  25.     else:
  26.         return string
  27. def utf82uni(string):
  28.     '''convert utf8 str to unicode'''
  29.     return str2uni(string"utf-8")
  30. def uni2utf8(string):
  31.     '''convert unicode to utf8 str'''
  32.     return uni2str(string"utf-8")
  33. def local2utf8(string):
  34.     '''convert local str to utf8 str'''
  35.     return uni2utf8(str2uni(string))
  36. def utf82local(string):
  37.     '''convert utf8 str to local str'''
  38.     return uni2str(utf82uni(string))
  1. #-*- encoding:utf-8 -*-
  2. #使用pyexcelerator-0.6.3a读取excel并写入mysql数据库
  3. from pyExcelerator import *
  4. import MySQLdb,encoding
  5. #----------------------------------------------------------------------
  6. SERVER = 'localhost'
  7. USERNAME = 'root'
  8. PASSWD = ''
  9. DB = 'db'
  10. conn = MySQLdb.connect(host=SERVER,db=DB,user=USERNAME,passwd=PASSWD,charset='utf8')
  11. curs = conn.cursor()
  12. sheets = parse_xls(u'd:/test.xls')
  13. read_xls = lambda x, y, z: sheets[x][1][(y,z)] 
  14. lenth = len(sheets[0][1])
  15. sqlstr = ''
  16. read_xls(0,182,2)
  17. for r in range(24,181):
  18.     for c in range(0,8,2):
  19.         print c
  20.         print c+1
  21.         f1 = str(encoding.local2utf8(read_xls(0,r,c)))#如果是数字,转换成字符串
  22.             
  23.             
  24.         f2 = encoding.local2utf8(read_xls(0,r,c+1))
  25.         sqlstr="""
  26.            INSERT INTO custom_cde (cde,name)
  27.            VALUES
  28.              (""" + "'" + f1 + "', " + "'" + f2 + "')" + """
  29.          """
  30.         print sqlstr
  31.         curs.execute(sqlstr)
  32.     
  33.     
  34. #提交事务    
  35. conn.commit()
  36. curs.close()
  37. conn.close()
  1. #-*- encoding:utf-8 -*-
  2. #查询mysql数据库,显示到wxPython的grid
  3. import wx
  4. import wx.grid
  5. import MySQLdb,encoding
  6. import DBConnection
  7. class SimpleGrid(wx.grid.Grid):
  8.     def __init__(self, parent):
  9.         wx.grid.Grid.__init__(self, parent, -1)
  10.         self.CreateGrid(73)
  11.         
  12.         conn = DBConnection.DBConnection('localhost','codelist','root','')
  13.         cursor = conn.getCursor()
  14.         cursor.execute ("SELECT * FROM cntr_dmg_cde")
  15.         rows = cursor.fetchall()
  16.         i = 0
  17.         self.SetColLabelValue(0, u"中文方位")
  18.         self.SetColLabelValue(1, u"英文方位")
  19.         self.SetColLabelValue(2, u"代码")
  20.         for row in rows:
  21.             tempLen = len(row)
  22.             for col in range(1,tempLen):
  23.                 tempStr = encoding.utf82local(row[col])
  24.                 
  25.                 self.SetCellValue(i, col-1, tempStr)
  26.             i = i + 1
  27.         cursor.close()
  28.         conn.close()
  29. class TestFrame(wx.Frame):
  30.     def __init__(self, parent):
  31.         wx.Frame.__init__(self, parent, -1, u"标题",
  32.                 size=(275275))
  33.         grid = SimpleGrid(self)
  34.         
  35. if __name__ == '__main__':
  36.     app = wx.PySimpleApp()
  37.     frame = TestFrame(None)
  38.     frame.Show(True)
  39.     app.MainLoop()
  1. #-*- encoding:utf-8 -*-
  2. # 操作数据库的封装
  3. # 因为自己擅长java,所以也用oop的思想,把操作数据库封装了一下
  4. # 这个是指初步,还有待完善
  5. import MySQLdb
  6. ########################################################################
  7. class DBConnection:
  8.     """"""
  9.     __conn = None
  10.     #----------------------------------------------------------------------
  11.     def __init__(self, host, db, user, password, charset='utf8'):
  12.         """Constructor"""
  13.         
  14.         #if charset == '':
  15.             #charset = 'utf8'
  16.         self.__conn = MySQLdb.connect(host=host,db=db,user=user, passwd=password,charset=charset)
  17.         print ''
  18.     #----------------------------------------------------------------------
  19.     def getConn(self):
  20.         """"""
  21.         return self.__conn
  22.     #----------------------------------------------------------------------
  23.     def getCursor(self):
  24.         """获取游标(奇怪,在java中没听说过这个概念)"""
  25.         return self.getConn().cursor()
  26.     #----------------------------------------------------------------------
  27.     def getResultSet(self):
  28.         """获取结果集"""
  29.         return self.getConn().cursor().fetchAll()
  30.     #----------------------------------------------------------------------
  31.     def getRow(self):
  32.         """get a row"""
  33.         rs = self.getResultSet()
  34.     #----------------------------------------------------------------------
  35.     def close(self):
  36.         """Close Database connecton"""
  37.         self.__conn.close()    
  38.     
  39. #----------------------------------------------------------------------
  40. def test():
  41.     """测试方法"""
  42.     dbOjbect = DBConnection('localhost','codelist','root','')
  43.     cursor = dbOjbect.getCursor()
  44.     cursor.execute ("SELECT * FROM cntr_dmg_cde")
  45.     rs = cursor.fetchall()
  46.     for r in rs:
  47.         for i in range(len(r)):
  48.             print r[i]
  49.         print
  50.        
  51. if __name__ == '__main__':
  52.     test()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值