python mysqlコマンドを実行し、結果をテキストにエクスポート

1502 ワード

スクリプト:
# -*- coding: utf-8 -*-
import MySQLdb
import sys  #         
 
if __name__ == '__main__':
  #     
    conn = MySQLdb.connect(host="dz-233", user="abc", passwd="def", db="gh", charset="utf8")
    cursor = conn.cursor()

 #        0     
date = sys.argv[1]

topRateSql = 'select DISTINCT "rate" as tag,shop_id from tb_top_rate_secret_stat where date = %s and rate_top_num <= 100 and shop_id not in (select distinct(shop_id) from CAUser.tb_user_secret_relation where status >= 0)'

topSql = 'select distinct "top" as tag, shop_id from tb_top_secret_stat where date = %s and top_num <= 100 and shop_id not in (select distinct(shop_id) from CAUser.tb_user_secret_relation where status >= 0)'

shopRelationSql = 'select distinct(shop_id) from CAUser.tb_user_secret_relation where user_id="TSU" and status = 10 and last_modify_time >= "2017-04-01"'

cursor.execute(shopRelationSql)

rows1 = cursor.fetchall()
##     
with open("./result" + date, 'wb+') as f:
    for row in rows1:
        f.write('%s
' % row[0]) # cursor.close() conn.close()

コメント:
  • pythonスクリプトはshellより簡単に書くべきで、IDEAを使うことができて、同時にもっと強い多面性があって、選択と方向はとても重要です
  • もし間違いがあれば提出することができて、本人の実戦の経験はまたゆっくりと
  • を蓄積して総括する必要があります
  • サーバーの上でvimを使って熟練していない情況の下で、編集してテストしてとても面倒で、長い間IDEAをして遠隔でローカルpythonを発表して、そしてサーバーの上で運行する方式、いくつかの穴が失敗したため、後で時間があって引き続き
  • をします