python查询数据库小例子

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()

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。