python 批量写更新语句脚本

来源:互联网 发布:秋季淘宝网什么好卖 编辑:程序博客网 时间:2024/06/05 14:20
<pre name="code" class="python">#encoding=utf-8import sysreload(sys)sys.setdefaultencoding('utf-8')#上面这三行为了防止python2.7 出现编码问题import xlrdbook = xlrd.open_workbook(u"test.xlsx")  #文件名,把文件与py文件放在同一目录下sheet = book.sheet_by_name("test")#execl里面的表明f = open("sql.txt","a")for r in range(1, sheet.nrows): #第一行是我的标题名,对应表中的字段名所以应该从第二行开始,计算机以0开始计数,所以值是1      tel = int(sheet.cell(r,0).value)      uname = sheet.cell(r,1).value      branch = sheet.cell(r,2).value      str1 = "update tp_bind_emp SET  " + "tel =" + "'" + str(tel) + "' "+ "where uname= " +"'" + uname + "' " + "and branch =" +"'" +branch + "' " + ";" +"\n";      str22 = str(str1)      f.write(str22)print ""print "Done! "print ""columns = str(sheet.ncols)rows = str(sheet.nrows)print '一共插入了 %s'% columns + '列' +'一共插入了 %s' % rows + '行'
                                             
0 0