#!/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();
Python操作MySQL数据库以及IO操作
最新推荐文章于 2023-06-11 16:02:59 发布