您的位置:首页 > 博客中心 > 数据库 >

python查询数据库小例子

时间:2022-03-13 23:03

Windows下面到下载MySQLdb:

 

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

热门排行

今日推荐

热门手游