#!/usr/bin/env python # -*- coding:gbk -*- #Author: nulige import time import MySQLdb import xlwt import os import xlrd from xlutils.copy import copy # 打开数据库连接 db = MySQLdb.connect("localhost", "test", "test", "c_test", charset='utf8' ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # SQL 查询语句 sql = "SELECT statisttime,sum(cnum) " \ "FROM c_statist_prov " \ "WHERE statisttime BETWEEN date_format( curdate( ) - 7, '%Y%m%d' ) " \ "AND date_format( curdate( ), '%Y%m%d' ) " \ "GROUP BY statisttime" #sql='select curdate()' try: # 使用execute方法执行SQL语句 cursor.execute(sql) # 使用 fetchone() 方法获取一条数据测试 #data = cursor.fetchone() #print "Database version : %s " % data # 使用 fetchone() 方法获取多条数据 data = cursor.fetchall() for row in data: riqi = row[0] statist = row[1] print "%s,%d" % \ (riqi, statist ) except: print "Error: unable to fecth data" # 关闭数据库连接 db.close()