Repository navigation
Expand file tree
/
Copy pathmysqlUtils.py
More file actions
103 lines (91 loc) · 4.31 KB
/
Copy pathmysqlUtils.py
File metadata and controls
103 lines (91 loc) · 4.31 KB
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
#Import mysql database
import pymysql
class MysqlUtils():
#initialization
def __init__(self,host,user,password,db,charset):
self.host=host
self.user=user
self.password=password
self.db=db
self.charset=charset
self.conn = pymysql.connect(host=host, user=user, password=password, db=db, charset=charset)
self.cur = self.conn.cursor()
# #add books
def add_book(self,number,name,author,publicationdate,location ,remark):
# Add multiple pieces of data
# sqlstr="insert into book (number,name,author,publicationdate,location ,remark) values(%s,%s,%s,%s,%s,%s);"
# data = [
# ('"+number+"',''"+name+"' , '"+author+"','"+publicationdate+"','"+location+"','"+remark+"')
# # ,
# # ('june', '258'),
# # ('marin', '369')
# ]
# # Splice and execute sql statements
# self.cur.executemany(sqlstr, data)
#Add a single piece of data
sqlstr="insert into book (number,name,author,publicationdate,location ,remark) " \
"values( '"+number+"','"+name+"','"+author+"','"+publicationdate+"','"+location+"','"+remark+"');"
self.cur.execute(sqlstr)
# Pay attention to submission when writing operations are involved
self.conn.commit()
#Delete books Delete based on book id
def delete_book(self, bookid):
sqlstr="delete from book where number = '" + bookid + "';"
#Splice and execute sql statements
self.cur.execute(sqlstr)
# Pay attention to submission when writing operations are involved
self.conn.commit()
# #Modify books
# Delete first and then add to implement the modification function
# def change_book(self,number,name,author,publicationdate,location ,remark):
# Find all books
def query_all_book(self):
self.cur.execute('SELECT number ,name,author,publicationdate,location ,remark FROM book order by number')
result = self.cur.fetchall()
return result
#Search for specified books (parameter: book title) Search for books based on book names
def query_one_book(self,name):
sqlstr="SELECT number ,name,author,publicationdate,location ,remark FROM book WHERE name = '"+name+"'"
self.cur.execute(sqlstr)
result = self.cur.fetchall()
return result
# Find the specified book (parameter: book id) Query the book based on the book name
def query_one_book_byid(self, id):
sqlstr = "SELECT number ,name,author,publicationdate,location ,remark FROM book WHERE number = '" + id + "'"
self.cur.execute(sqlstr)
result = self.cur.fetchall()
return result
# Borrowing record query
def query_borrowrecord(self):
# sqlstr = "SELECT number ,name,location ,borrowname,borrowtime FROM book WHERE isborrow = 1" # X_Change
sqlstr = "SELECT number ,name,location ,borrowname,borrowtime FROM book" # X_Change
self.cur.execute(sqlstr)
result = self.cur.fetchall()
return result
# Find reader information
def query_readerinfor(self):
sqlstr = "SELECT name,class,learnnumber,phonenumber,borrownumber FROM student order by id"
self.cur.execute(sqlstr)
result = self.cur.fetchall()
return result
#Registered administrator (parameters: account number, password)
def register_Admin(self,uername,password):
# Add a single piece of data
sqlstr = "insert into user (username,psw) values( '" + uername + "','" + password + "');"
self.cur.execute(sqlstr)
# Pay attention to submission when writing operations are involved
self.conn.commit()
#Administrator login (check password through account)
def query_Password(self,username):
sqlstr = "SELECT psw FROM user WHERE username='" + username + "'"
self.cur.execute(sqlstr)
result = self.cur.fetchall()
for row in result:
password=str(row[0])
return password
def query_LikeBook(self,book_name):
# sqlstr = "SELECT * FROM book WHERE name like '" + book_name + "'" # X_Change
sqlstr = "SELECT number,name,author,publicationdate,location ,remark FROM book WHERE name like '" + book_name + "'" # X_Change
self.cur.execute(sqlstr)
result = self.cur.fetchall()
return result