码迷,mamicode.com
首页 > 数据库 > 详细

python查询数据库小例子

时间:2014-08-27 20:19:18      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:http   os   io   for   ar   数据   2014   cti   html   

Windows下面到下载MySQLdb:

http://www.codegood.com/downloads

 

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

 

from datetime import *

import MySQLdb

import sys

 

FILE_C = "c_%d.html"

 

CIDS = [409070983655456980, 409070984218760117, 409070984223655957, 409070984228852701]

 

SQL_CATE = "select * from tb_access_log_20140827 where id = %d"

 

reload(sys)

sys.setdefaultencoding(‘utf-8‘)

 

db_hlx = MySQLdb.connect(host="127.0.0.1",port=3601,user="zcw",passwd="zcw",db="db_access_log");

db_hlx.set_character_set(‘utf8‘)

 

cur_category = db_hlx.cursor();

cur_category.execute(‘SET NAMES utf8;‘)

cur_category.execute(‘SET CHARACTER SET utf8;‘)

cur_category.execute(‘SET character_set_connection=utf8;‘)

 

def fetch_cate(cid):

 

        SQL_CATE_X = SQL_CATE % (cid)

 

        cur_category.execute(SQL_CATE_X)

 

        row = cur_category.fetchone()

 

        cid = row[0]

 

        f = open(FILE_C % cid, ‘w‘)

        f.write(row[3])

        f.close()

 

def printCateList():

        for cid in CIDS:

                fetch_cate(cid)

 

if __name__ == "__main__":

        printCateList()

python查询数据库小例子

标签:http   os   io   for   ar   数据   2014   cti   html   

原文地址:http://www.cnblogs.com/super-d2/p/3940063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!