python指令碼相關 連線資料庫 重定向

2021-08-30 23:46:54 字數 691 閱讀 6132

今天寫了乙個 指令碼 作用是將乙個資料庫中的乙個查詢結果 跑出來 放到乙個檔案中 查詢結果有70k多條。

涉及到了 2個知識點吧 記錄一下

1 連mysql的問題 主要是這個mysqldb模組兒    主要可以參考這個url

2 結果重定向的問題

就是平時在控制台打出的結果 打到乙個檔案中去 可以這樣做  

def print_result(cursor, list):

'''列印出查詢結果'''

f = open('test.txt','w')

total = 0

print >> f, user_id,'|',amount,'|',order_no, '|',deal_time

或者 這樣

import sys

def print_result(cursor, list):

'''列印出查詢結果'''

sys.stdout=open('test.txt','w')

total = 0

print user_id,'|',amount,'|',order_no, '|',deal_time

再或者 就直接寫print然後執行指令碼的時候這樣執行

python test.py > test.txt

和shell指令碼類似

python 連線資料庫

原文 原文1 安裝mysql python pip install mysql python dome1 def db mange db bank conn none try 開啟資料庫連線 conn mysqldb.connect localhost root 123456 db bank 獲取操...

Python連線資料庫

usr bin env python coding utf 8 import sys reload sys sys.setdefaultencoding utf 8 import pymysql import pymysql.cursors usr bin env python coding utf...

python連線資料庫

1 安裝mysql ubantu下安裝不撰述 2 安裝python版本的mysql開發包 sudo apt get install python mysqldb3 編寫python usr bin python coding utf 8 import mysqldb 引入mysqldb包 開啟資料庫...