python查詢資料庫小例子

來源:互聯網
上載者:User

標籤: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查詢資料庫小例子

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.