好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用python操作mysql数据库详解

基础环境:Python 3.5.1

getusercoin()

f.close()

f1.close()

#!/usr/bin/env python

#-*-coding:utf-8-*-

#明细

import MySQLdb

import os, sys, re,string

import time, tarfile,getopt

optmap = {

'dbuser' : 'haoren',

'dbpass' : 'FqDxhG4d',

'dbhost' : '192.168.1.10',

'dbport' : 3306,

'dbname' : 'JSDB'

}

def get_files(dir, pattern):

res_file_list =[]

if os.path.exists(dir):

cur_file_list = os.listdir(dir)

for file_name in cur_file_list:

if re.search(pattern, file_name):

res_file_list.append(file_name)

return res_file_list

else:

return 'no'

def main():

cur_day = time.strftime("%Y%m%d", time.localtime(time.time()-86400))

opts, args = getopt.getopt(sys.argv[1:], 'd:')

for op, value in opts:

if op == '-d':

m = re.search('[0-9]{8}', value)

if m:

cur_day = value

else:

print "请输入8位日期(比如:20130215)"

return 'no'

log_day = time.strftime('%y%m%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

fmt_day = time.strftime('%Y-%m-%d', time.localtime(time.mktime(time.strptime(cur_day, '%Y%m%d'))))

print '结算统计日期:',fmt_day

#log_day = time.strftime("%y%m%d", time.localtime(time.time()-86400))

dirname="/home/haoren/logdir/%s_67"%log_day

print dirname

db_conn = MySQLdb.connect(user=optmap['dbuser'], passwd=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], db=optmap['dbname'])

db_cursor=db_conn.cursor()

db_conn.query("use %s"%optmap['dbname'])

tabletime = time.strftime("%y%m%d", time.localtime(time.mktime(time.strptime(cur_day, "%Y%m%d"))))

sql="CREATE TABLE IF NOT EXISTS `JIESUANTONGJI_%s` like JIESUANTONGJISAMPLE"%tabletime

db_conn.query(sql)

db_conn.query("delete from JIESUANTONGJI_%s"%tabletime)

if os.path.exists("/tmp/JieSuanTongJi2016.txt"):

os.system("rm -f /tmp/JieSuanTongJi2016.txt")

file_list2=get_files(dirname,'billserver')

for file2 in file_list2:

command = "cat %s/%s | grep -h -w 结算统计 |grep -v 人民币消费结算统计 >> /tmp/JieSuanTongJi2016.txt"%(dirname,file2)

os.system(command)

#结算统计记录放在txt文档里面

filename='/tmp/JieSuanTongJi2016.txt'

record = {}

a_file = open(filename, 'r')

#160125-11:00:14 Bill[40268] INFO: [结算统计]时间(1453690814)类别(1)名称(购物卡收入)平台(3977962)等级(2)用户(65147500)赠送(1)个购物卡(39)给客户(65147500),客户等级(28),签约(1), 消耗人民币(100), 客户获得人民币(8000), 平台获得人民币(2000),客户当前人民币(1320960)平台当前人民币(335560)

for a_line in a_file.readlines():

m = re.search("^(\S+) Bill\[\d+\] INFO: \[结算统计\]时间\((\d+)\)类别\((\d+)\)名称\((\S+)\)平台\((\d+)\)等级\((\d+)\)用户\((\d+)\)赠送\((\d+)\)个购物卡\((\d+)\)给客户\((\d+)\),客户等级\((\d+)\),签约\((\d+)\), 消耗人民币\((\d+)\), 客户获得人民币\((\d+)\), 平台获得人民币\((\d+)\),客户当前人民币\((\d+)\)平台当前人民币\((\d+)\)", a_line)

if m:

#print "第一项:"+m.group(1)

#print "第二项:"+m.group(2)

#print "第三项:"+m.group(3)

#print "第四项:"+m.group(4)

#print "第五项:"+m.group(5)

#print "第六项:"+m.group(6)

#print "第七项:"+m.group(7)

#print "第八项:"+m.group(8)

#print "第九项:"+m.group(9)

#print "第十项:"+m.group(10)

#print "第十一项:"+m.group(11)

#print "第十二项:"+m.group(12)

#print "第十三项:"+m.group(13)

#print "第十四项:"+m.group(14)

#print "第十五项:"+m.group(15)

#print "第十六项:"+m.group(16)

#print "第十七项:"+m.group(17)

#print "第十八项:"+m.group(18)

if int(m.group(14)) >0 or int(m.group(15)) >0 :

db_conn.query("insert into JIESUANTONGJI_%s(OPTIME,TYPE,ITEMNAME,CHANNELID,CHANNELLEVEL,PRESENTERID,ITEMNUM,ITEMID,SINGERID,SINGERLEVEL,SIGN,CONSUMECOIN,SINGERRECVGOLD,CHANNELRECVGOLD,CURRENTSINGERGOLD,CURRENTCHANNELGOLD) values(%d,%d,'%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)"%(tabletime,int(m.group(2)),int(m.group(3)),str(m.group(4)),int(m.group(5)),int(m.group(6)),int(m.group(7)),int(m.group(8)),int(m.group(9)),int(m.group(10)),int(m.group(11)),int(m.group(12)),int(m.group(13)),int(m.group(14)),int(m.group(15)),int(m.group(16)),int(m.group(17))))

a_file.close()

db_conn.commit()

db_cursor.close()

db_conn.close()

main()

#if __name__ == "__main__":

# main()

以上就是 使用python操作mysql数据库详解的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于使用python操作mysql数据库详解的详细内容...

  阅读:41次