-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdb.py
150 lines (122 loc) · 3.85 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# encoding:utf-8 -*-
import hashlib
import MySQLdb
import sys
import datetime
reload(sys)
sys.setdefaultencoding("utf-8")
host = "127.0.0.1"
user = "root"
password = "root"
database = "Blog"
charset = "utf8"
def open():
conn = MySQLdb.connect(host, user, password, database, charset=charset)
cursor = conn.cursor()
return conn, cursor
def close(conn, cursor):
conn.close()
cursor.close()
def allArticle():
conn, cursor = open()
cursor.execute("SELECT content,id from craft where type = '1' order by id DESC ;")
result = cursor.fetchall()
close(conn, cursor)
return result
def getArticle(id):
conn, cursor = open()
cursor.execute("SELECT content,pubTime,type,name from craft,user where craft.id = %s and userid = user.id;" % id)
result = cursor.fetchall()
close(conn, cursor)
return result
def APIlogin(result):
conn, cursor = open()
result[0] = MySQLdb.escape_string(result[0])
result[1] = MySQLdb.escape_string(result[1])
cursor.execute("select name,password from user where name = '%s'" % result[0])
result = cursor.fetchall()
close(conn, cursor)
return result
def addArticle(data, userId):
conn, cursor = open()
data["title"] = MySQLdb.escape_string(data["title"].decode("utf-8"))
data["content"] = MySQLdb.escape_string(data["content"].decode("utf-8"))
cursor = conn.cursor()
today = datetime.date.today()
cursor.execute(
"insert into craft(userid,title,content,pubTime,type) values ('%s','%s','%s','%s','%s')" % (
userId, data["title"], data["content"], today.strftime("%Y-%m-%d"), "0"))
conn.commit()
close(conn, cursor)
return
def articleManage():
conn, cursor = open()
cursor.execute("SELECT id,title,type,pubTime from craft where type != '4' ORDER BY pubTime desc;")
result = cursor.fetchall()
close(conn, cursor)
return result
def change(result):
conn, cursor = open()
result[0] = MySQLdb.escape_string(str(result[0]))
result[1] = MySQLdb.escape_string(result[1])
cursor = conn.cursor()
cursor.execute("UPDATE craft set type = '%s' where id = '%s';" % (result[1], result[0]))
conn.commit()
close(conn, cursor)
return
def verifyToken():
conn, cursor = open()
cursor.execute("select password,id from user ;")
result = cursor.fetchall()
close(conn, cursor)
return result
def createUser():
conn, cursor = open()
cursor.execute("DROP table if EXISTS user")
cursor.execute('''create table user (
id INT(11) primary key not null unique auto_increment,
name VARCHAR(45),
isAdmin VARCHAR(45),
regTime DATE,
password VARCHAR(45)
)''')
close(conn, cursor)
return
def createCraft():
conn, cursor = open()
cursor.execute("DROP table if EXISTS craft")
cursor.execute('''create table craft (
id INT(11) primary key not null unique auto_increment,
userid INT(11),
title LONGTEXT,
content LONGTEXT,
pubTime date,
type INT(11)
)
''')
close(conn, cursor)
return
def insertUser(username, password):
conn, cursor = open()
today = datetime.date.today()
Md5 = hashlib.md5()
Md5.update(password)
Md5hex = Md5.hexdigest()
Md52 = hashlib.md5()
Md52.update(Md5hex)
password_twice = Md52.hexdigest()
cursor.execute("insert into user values('1','%s','1','%s','%s')" % (
username, today.strftime("%Y-%m-%d"), password_twice))
conn.commit()
close(conn, cursor)
return
def insertCraft():
conn, cursor = open()
today = datetime.date.today()
cursor.execute(
"insert into craft values('1','1','你好世界!','# 你好世界!\n\n* Shakespeare是一个轻量型的,基于ES6编写的个人博客; \n* 这是博客的第一篇文章;\n* 赶紧进入后台发表文章吧!','%s','1');" % (
today.strftime(
"%Y-%m-%d")))
conn.commit()
close(conn, cursor)
return