Python操作MySQL数据库以及IO操作

#!/usr/bin/python
#excoding=utf-8

import mysql.connector as conn;
import os;
import urllib.request as urllib;
print (conn.__version__);

config = {
    'host':'172.00.0.00',
    'user':'xxxxxx',
    'password':'xxxxxx',
    'port':3306,
    'database':'xxxxxxx',
    'charset':'utf8'
}

try:
    con = conn.connect(**config);
except conn.Error as e:
    print ("connect failed:{}".format(e));

os.chdir("/Users/xx/documents/xx");
cursor = con.cursor();
try:
    sql = "select a.order_id, `group`, `type`, url, a.`name` as name , b.name as dirname from xxx a left join ccc b on a.order_id = b.order_id where a.valid = 1 and b.is_delete = 0 and `group` in (4, 6) and `type` = 2;"; #入院记录
    cursor.execute(sql);
    for order_id, group, type, url, name, dirname in cursor:
        if (dirname == ""):
            dirname = str(order_id);
        if not (os.path.exists(dirname)):
            os.mkdir(dirname);
            print (os.getcwd(), "1");
        os.chdir(dirname);
        print (os.getcwd(), "2");
        if (group == 6):
            if not (os.path.exists("记录")):
                os.mkdir("记录");
            os.chdir("记录");
        elif (group == 4):
            if not (os.path.exists("报告")):
                os.mkdir("报告");
            os.chdir("报告");
        print (os.getcwd(), "3");
        print (order_id, group, type, url, name);
        file = urllib.urlopen(url);
        data = file.read();
        with open(name + ".jpg", "wb") as code:
             code.write(data);
        os.chdir("/Users/xx/documents/xx");
except conn.Error as e:
    print ("query error:{}".format(e));
finally:
    cursor.close();
    con.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值