-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_and_books_traffic.py
160 lines (134 loc) · 6.14 KB
/
user_and_books_traffic.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
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Author: Nikos Sakellariou
#Date started: 11/15/2014
#Summary: A client for a library database administration. It admins its books
#users etc
#This is the user and books traffic table
import MySQLdb as mdb
import sys
con = ''
message = 'Αρχείο κίνησης βιβλίων \n'
print (message).decode('utf-8')
def init_con():
global con
con = mdb.connect(host="localhost", use_unicode = True, charset = "utf8", user='user', passwd='pass', db='test')
def create_traffic_table(con):
with con:
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS trafficTable")
cur.execute("CREATE TABLE trafficTable(Id INT PRIMARY KEY AUTO_INCREMENT, USER_ID VARCHAR(20), NAME VARCHAR(30),\
DATE_OF_REGISTER VARCHAR(30), RETURN_DATE VARCHAR(30), INSERTION_NUMBER VARCHAR(30), RETURN_D VARCHAR(20), FINE VARCHAR(20), PAID VARCHAR(20), RENEW VARCHAR(30) )")
def update(con):
with con:
cur = con.cursor()
id_ = raw_input("Enter the id of the raw you want to update\n")
user_id = raw_input("Enter the user id \n")
name = raw_input("Enter the name \n")
date_of_reg = raw_input("Enter the date of register\n")
return_date = raw_input("Enter the return date \n")
insertion_num = raw_input("Enter the insertion number\n")
return_k = raw_input("Enter the return(kati me epistrofh einai auto alla den... \n")
fine = raw_input("Enter the prostimo\n")
paid = raw_input("Enter if is paid \n")
renew = raw_input("renewal ananewsh \n")
cur.execute("UPDATE trafficTable SET USER_ID = %s, NAME = %s, DATE_OF_REGISTER = %s, RETURN_DATE = %s, INSERTION_NUMBER =%s, \
RETURN_D =%s, FINE =%s, PAID = %s, RENEW = %s WHERE Id = %s",
(user_id,name,date_of_reg,return_date,insertion_num,return_k,fine,paid,renew, id_))
print "Number of rows updated:", cur.rowcount
def print_data(con,rows):
with con:
print '--------------------------------------------------------------------------------'
for row in rows:
print
for element in row:
print element,"|",
print
print '--------------------------------------------------------------------------------'
def show_books(con):
with con:
names = '|Primary key|Κωδικός Χρήστη|Όνομα |Ημερομηνία δανειμου|Ημερομηνία επιστροφής|Αριθμός εισαγωγής|Επιστροφή|Πρόστιμο|Πληρωμή|Ανανέωση'
#names = (names).decode('utf-8')
cur = con.cursor()
cur.execute("SELECT * FROM trafficTable")
print "This is the traffic Table : "
rows = cur.fetchall()
print names
print_data(con, rows)
def load_from_file(con):
with con:
cursor = con.cursor()
#openfile = raw_input('Give the name of the file mapped. \n ex C:/Documents and Settings/user/Desktop/project/users.txt')
cursor.execute("LOAD DATA LOCAL INFILE 'C:/Documents and Settings/user/Desktop/library project/books.txt' INTO TABLE churchlib ;")
def insert_new(con):
with con:
cur = con.cursor()
print "Εισαγωγή νέου πεδίου \n".decode('utf-8')
user_id = raw_input("Enter the user id \n")
name = raw_input("Enter the name \n")
date_of_reg = raw_input("Enter the date of register\n")
return_date = raw_input("Enter the return date \n")
insertion_num = raw_input("Enter the insertion number\n")
return_k = raw_input("Enter the return(kati me epistrofh einai auto alla den... \n")
fine = raw_input("Enter the prostimo\n")
paid = raw_input("Enter if is paid \n")
renew = raw_input("renewal ananewsh \n")
#name = (name).decode('utf-8')
cur.execute("INSERT INTO trafficTable( USER_ID, NAME ,DATE_OF_REGISTER, RETURN_DATE, INSERTION_NUMBER, RETURN_D, FINE, PAID, RENEW) \
VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)",(user_id, name, date_of_reg, return_date, insertion_num, return_k, fine, paid, renew))
def search_user(con):
with con:
cur = con.cursor()
name = ''
id_ = ''
user_id = raw_input("Search by user id \n")
if user_id != '':
pass
id_ = raw_input("anazitisi me to id ths kataxwrishs\n")
if id_ != '':
cur.execute("SELECT * FROM trafficTable WHERE Id = %s", id_)
rows = cur.fetchall()
print_data(con,rows)
name = raw_input("Search by the name of the user and show user information \n")
if name != '':
cur.execute("SELECT *,Users.Proffesion FROM trafficTable INNER JOIN USERS ON trafficTable.USER_ID = USERS.ID")
rows = cur.fetchall()
print_data(con,rows)
#publisher = raw_input("Enter publisher(if exists) to search for \n")
rows = cur.fetchall()
print_data(con,rows)
def delete(con):
with con:
cur =con.cursor()
user_id = raw_input(" Enter the id of the raw to be deleted\n")
cur.execute("DELETE FROM trafficTable WHERE Id= %s",user_id)
def main():
global con
init_con()
with con:
#create_traffic_table(con)
show_books(con)
while(1):
print (message).decode('utf-8')
to_do = raw_input(' 1: show arxeio kinhshs table \n 2: insert new \n 3: Update \n 4: Search \n \
5: insert into database from file\n 6: Delete raw or type "exit" \n > ')
if to_do == '1':
show_books(con)
elif to_do == '2':
insert_new(con)
elif to_do == '3':
update(con)
elif to_do == '4':
search_user(con)
elif to_do == '5':
load_from_file(con)
elif to_do == '6':
delete(con)
elif to_do == 'exit':
print 'bye bye :) \n'
break
else:
print 'You entered wrong Value. Try again or type "exit" to quit \n'
con.close()
if __name__ == "__main__":
main()