-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdb.py
83 lines (70 loc) · 2.52 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: UTF-8 -*-
import pymysql
import traceback
# 打开数据库连接
db = pymysql.connect(host='localhost',user='root',passwd='ascent',db='today',port=3306,charset='utf8')
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 查询语句
sql = "SELECT * FROM users"
sql2 = "SELECT * FROM orders"
sql3 = "SELECT * FROM dishes"
#['user_id', 'gender', 'age', 'occupation', 'zip']#设列名称
#['user_id', 'movie_id', 'rating', 'timestamp']
#['movie_id', 'title', 'genres']
with open('data\\users.dat', 'w', encoding='utf-8') as f:
# 数据库读取的特点是每读一次会把前一次的盖掉
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
userid = row[0]
gender = row[5]
age = row[6]
occupation = row[8]
zip = row[9]
# 打印结果
f.write(str(userid) + "::" + str(gender) + "::" + str(age) + "::" + str(occupation) + "::" + str(zip)+"\n")
print("userid=%s,gender=%s,age=%s,occupation=%s,zip=%s" % \
(userid, gender, age, occupation, zip))
except:
print("Error: unable to fetch data")
msg = traceback.format_exc() # 方式1
print(msg)
with open('data\\ratings.dat', 'w', encoding='utf-8') as f2:
try:
# 执行SQL语句
cursor.execute(sql2)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
userid = row[1]
movieid = row[5]
rating = row[4]
timestamp = row[3]
# 打印结果
f2.write(str(userid)+"::"+str(movieid)+"::"+str(rating)+"::"+str(timestamp)+"\n")
print("userid=%s,movieid=%s,rating=%s,timestamp=%s" % \
(userid, movieid, rating, timestamp))
except:
print("Error: unable to fecth data")
with open('data\\movies.dat', 'w', encoding='utf-8') as f3:
try:
# 执行SQL语句
cursor.execute(sql3)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
movieid = row[0]
title = row[2]
genres = row[5]
# 打印结果
f3.write(str(movieid)+"::"+str(title)+"::"+str(genres)+"\n")
print("movieid=%s,title=%s,genres=%s" % \
(movieid, title, genres))
except:
print("Error: unable to fecth data")
# 关闭数据库连接
db.close()