-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.py
87 lines (66 loc) · 2.61 KB
/
sql.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
84
85
86
87
"""
一般 Python 用于连接 MySQL 的工具:pymysql
"""
import pymysql.cursors
connection = pymysql.connect(host='localhost',
user='root',
password='1234',
db='test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
# 保存评论
def insert_comments(music_id, comments, detail, connection0):
with connection0.cursor() as cursor:
sql = "INSERT INTO `comments` (`MUSIC_ID`, `COMMENTS`, `DETAILS`) VALUES (%s, %s, %s)"
cursor.execute(sql, (music_id, comments, detail))
connection0.commit()
# 保存音乐
def insert_music(music_id, music_name, album_id):
with connection.cursor() as cursor:
sql = "INSERT INTO `musics` (`MUSIC_ID`, `MUSIC_NAME`, `ALBUM_ID`) VALUES (%s, %s, %s)"
cursor.execute(sql, (music_id, music_name, album_id))
connection.commit()
# 保存专辑
def insert_album(album_id, artist_id):
with connection.cursor() as cursor:
sql = "INSERT INTO `albums` (`ALBUM_ID`, `ARTIST_ID`) VALUES (%s, %s)"
cursor.execute(sql, (album_id, artist_id))
connection.commit()
# 保存歌手
def insert_artist(artist_id, artist_name):
with connection.cursor() as cursor:
sql = "INSERT INTO `artists` (`ARTIST_ID`, `ARTIST_NAME`) VALUES (%s, %s)"
cursor.execute(sql, (artist_id, artist_name))
connection.commit()
# 获取所有歌手的 ID
def get_all_artist():
with connection.cursor() as cursor:
sql = "SELECT `ARTIST_ID` FROM `artists` ORDER BY ARTIST_ID"
cursor.execute(sql, ())
return cursor.fetchall()
# 获取所有专辑的 ID
def get_all_album():
with connection.cursor() as cursor:
sql = "SELECT `ALBUM_ID` FROM `albums` ORDER BY ALBUM_ID"
cursor.execute(sql, ())
return cursor.fetchall()
# 获取所有音乐的 ID
def get_all_music():
with connection.cursor() as cursor:
sql = "SELECT `MUSIC_ID` FROM `musics` ORDER BY MUSIC_ID"
cursor.execute(sql, ())
return cursor.fetchall()
# 获取前一半音乐的 ID
def get_before_music():
with connection.cursor() as cursor:
sql = "SELECT `MUSIC_ID` FROM `musics` ORDER BY MUSIC_ID LIMIT 0, 800000"
cursor.execute(sql, ())
return cursor.fetchall()
# 获取后一半音乐的 ID
def get_after_music():
with connection.cursor() as cursor:
sql = "SELECT `MUSIC_ID` FROM `musics` ORDER BY MUSIC_ID LIMIT 800000, 1197429"
cursor.execute(sql, ())
return cursor.fetchall()
def dis_connect():
connection.close()