1+ # encoding: utf-8
2+
3+ """
4+ File: test.py
5+ Author: Rock Johnson
6+ """
7+ import os , requests , MySQLdb , json
8+
9+ # 输入git命令
10+ # file = os.path.dirname(os.path.abspath(""))
11+ #
12+ # os.chdir(file)
13+ # r = os.system("git pull")
14+ # if r != 0:
15+ # print("...")
16+
17+ # 下载文件
18+ # r = requests.get("https://github.com/RockJohnson503/MyDemo/archive/master.zip")
19+ # with open("master.zip", "wb") as code:
20+ # code.write(r.content)
21+
22+ # 导入数据库
23+ def dump_json (arg ):
24+ db = MySQLdb .connect (host = 'localhost' , user = 'root' , passwd = 'k836867547' , db = 'factory' )
25+ cur = db .cursor ()
26+ url = input ("输入文件路径: " )
27+ query = ["insert into turnoverZNQ_product (factory_name, product_type, product_name, product_default, "
28+ "product_now, product_in, product_out, is_delete, user_id) values "
29+ "(%s, %s, %s, %s, %s, %s, %s, %s, %s);" ,
30+ "insert into turnoverZNQ_detail (bill_id, operate, operate_num, time, product_id) VALUES "
31+ "(%s, %s, %s, %s, %s);" ,
32+ "insert into turnoverZNQ_productlave (amount, detail_id) values (%s, %s)" ]
33+
34+ with open (url ) as file :
35+ date = json .loads (file .read ())
36+
37+ for i in date ['data' ]:
38+ if arg == 1 :
39+ param = (i ['factory' ], i ['id' ], i ['name' ], i ['first' ], i ['now' ], i ['in' ], i ['out' ], 0 , 2 )
40+ elif arg == 2 :
41+ cur .execute ("select id from turnoverZNQ_product where factory_name=%s and product_type=%s and product_name=%s and user_id=2" ,
42+ (i ['factory' ], i ['id' ], i ['name' ]))
43+ product_id = cur .fetchone ()
44+ param = (i ['key' ], i ['operat' ], i ['num' ], i ['date' ], product_id [0 ])
45+ else :
46+ cur .execute (
47+ "select id from turnoverZNQ_product where factory_name=%s and product_type=%s and product_name=%s and user_id=2" ,
48+ (i ['factory' ], i ['id' ], i ['name' ]))
49+ product_id = cur .fetchone ()
50+ cur .execute ("select id from turnoverZNQ_detail where bill_id=%s and product_id=%s" , (i ['key' ], product_id ))
51+ detail_id = cur .fetchone ()
52+
53+ param = (i ['num' ], detail_id [0 ])
54+ cur .execute (query [arg - 1 ], param )
55+
56+ db .commit ()
57+
58+ db .close ()
59+
60+ if __name__ == '__main__' :
61+ arg = int (input ("选择导入类型(1.product 2.detail 3.productlave):" ))
62+ dump_json (arg )
0 commit comments