Skip to content

Commit 1d2ff87

Browse files
committed
save to mysql
1 parent 22c64ac commit 1d2ff87

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎PyBeaner/0001/coupon.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ def generate_one_coupon(coupon_length=5):
2626

2727

2828
if __name__ == '__main__':
29-
coupons = generate_coupons(10, 5)
29+
coupons = generate_coupons(200, 5)
3030
print(coupons)

‎PyBeaner/0002/save_to_mysql.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
__author__ = 'PyBeaner'
2+
import pymysql.cursors
3+
4+
5+
def save_to_mysql(coupons):
6+
try:
7+
connection = pymysql.connect(host='localhost',
8+
user='user',
9+
passwd='passwd',
10+
db='db',
11+
cursorclass=pymysql.cursors.DictCursor)
12+
13+
with connection.cursor() as cursor:
14+
select_sql = "SELECT coupon FROM coupons where coupon='%s'"
15+
insert_sql = "INSERT INTO coupons VALUES (%s);"
16+
for coupon in coupons:
17+
cursor.excute(select_sql, (coupon,))
18+
result = cursor.fetchone()
19+
if result:
20+
continue
21+
22+
cursor.excute(insert_sql, (coupon,))
23+
24+
connection.commit()
25+
finally:
26+
connection.close()

‎PyBeaner/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__author__ = 'PyBeaner'

0 commit comments

Comments
 (0)