forked from huaisha1224/jd-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
65 lines (58 loc) · 2.08 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sqlite3
from time import strftime
filename = strftime("save\\jd-assistant_%Y_%m.db")
def save_db(order_id, order_vercode, amount, username, order_time):
"""保存数据到sqlite
"""
# filename = "weread.db"
# 判断文件是否存在
if os.path.exists(filename):
# 处理数据并写入
conn = sqlite3.connect(filename)
c = conn.cursor()
c.execute("INSERT OR IGNORE INTO JDORDER(order_id, order_vercode, amount, username, order_time) VALUES(?,?,?,?,?)",
(order_id, order_vercode, amount, username, order_time))
conn.commit()
conn.close()
else:
# 创建数据库 处理数据并写入
path = os.path.dirname(os.getcwd() +'\\save\\') # 判断db目录是否存在
if not os.path.exists(path):
os.makedirs(path)
conn = sqlite3.connect(filename)
c = conn.cursor()
c.execute("CREATE TABLE JDORDER \
(order_id TEXT NOT NULL UNIQUE,\
order_vercode TEXT NOT NULL,\
amount TEXT NOT NULL,\
username TEXT NOT NULL,\
order_time TEXT NOT NULL)")
c.execute("INSERT OR IGNORE INTO JDORDER(order_id, order_vercode, amount, username, order_time) VALUES(?,?,?,?,?)",
(order_id, order_vercode, amount, username, order_time))
conn.commit()
conn.close()
def query_db(order_id):
"""查询数据是否已经存在
"""
if os.path.exists(filename):
conn = sqlite3.connect(filename)
c = conn.cursor()
c.execute("SELECT * FROM JDORDER WHERE order_id=?",[order_id])
result = c.fetchone()
conn.commit()
conn.close()
return result
if __name__== "__main__":
order_time = '2022/5/6'
order_id = '2443156891680'
order_vercode = '232068573372'
amount = '255'
username = 'huaisha1224'
# res = query_db(order_id)
# if res:
# print(res)
# else:
# save_db(order_id, order_vercode, amount, username, order_time)