-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySQL Connection.py
182 lines (163 loc) · 5.17 KB
/
MySQL Connection.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'''
import mysql.connector
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "admin123",database = "pythonconn")
cur = myconn.cursor()
try:
dbs = cur.execute("create table Employee(name varchar(20) not null, id int(20) not null primary key, salary float not null, Dept_id int not null)")
except:
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
mysql.connector.connect
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "admin123",database = "pythonconn")
cur = myconn.cursor()
try:
dbs = cur.execute("create table Employee(name varchar(20) not null, id int(20) not null primary key, salary float not null, Dept_id int not null)")
except:
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "admin123",database = "pythonconn")
cur = myconn.cursor()
try:
cur.execute("alter table Employee add branch_name varchar(20) not null")
except:
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
myconn = mysql.connector.connect(host="localhost", user="root", passwd="admin123", database="pythonconn")
cur = myconn.cursor()
sql = "insert into Employee(name, id, salary, dept_id, branch_name) values (%s, %s, %s, %s, %s)"
values = ("John", 110, 25000.00, 201, "Newyork")
try:
cur.execute(sql, values)
myconn.commit()
except:
myconn.rollback()
print(cur.rowcount, "record inserted!")
myconn.close()
'''
'''
import mysql.connector
myconn = mysql.connector.connect(host="localhost", user="root", passwd="admin123", database="pythonconn")
cur = myconn.cursor()
sql = "insert into Employee(name, id, salary, dept_id, branch_name) values ("Tom", 220, 50000.00, 501, "DC")"
try:
cur.execute(sql)
myconn.commit()
except:
myconn.rollback()
print(cur.rowcount, "record inserted!")
myconn.close()
'''
'''
import mysql.connector
myconn = mysql.connector.connect(host="localhost", user="root", passwd="admin123", database="pythonconn")
cur = myconn.cursor()
sql = "insert into Employee(name, id, salary, dept_id, branch_name) values (%s, %s, %s, %s, %s)"
val = [("John", 102, 25000.00, 201, "Newyork"), ("David", 103, 25000.00, 202, "Port of spain"),
("Nick", 104, 90000.00, 201, "Newyork")]
try:
cur.executemany(sql, val)
myconn.commit()
print(cur.rowcount, "records inserted!")
except:
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
myconn=mysql.connector.connect(host="localhost",user="root",passwd="admin123",database="pythonconn")
cur=myconn.cursor()
sql = "insert into store(id,location,distance) values (10,'thailand','5km')"
try:
cur.execute(sql)
myconn.commit()
print(cur.rowcount, "record inserted! id:", cur.lastrowid)
except:
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
myconn = mysql.connector.connect(host="localhost", user="root", passwd="admin123", database="pythonconn")
cur = myconn.cursor()
try:
cur.execute("select * from books")
result = cur.fetchall()
for x in result:
print(x);
except:
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "admin123",database = "pythonconn")
cur = myconn.cursor()
try:
cur.execute("select id, name, price from books where name='ttt'")
result = cur.fetchone()
print(result)
except:
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "admin123",database = "pythonconn")
cur = myconn.cursor()
try:
cur.execute("select id,name,price from books")
result = cur.fetchall()
print("id Name Salary")
for row in result:
print("%d %s %d" %(row[0],row[1],row[2]))
except:
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
myconn = mysql.connector.connect(host="localhost", user="root", passwd="admin123", database="pythonconn")
cur = myconn.cursor()
try:
cur.execute("select books.id, books.name, books.price, store.location, store.distance from store join books on store.id = books.id GROUP BY books.name Desc")
print("id name price location distance")
for row in cur:
print("%d %s %d %s %d" %(row[0], row[1], row[2], row[3], row[4]))
except:
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
myconn=mysql.connector.connect(host="localhost", user="root", passwd="admin123", database="pythonconn")
cur=myconn.cursor()
rowcount=0
try:
cur.execute("delete from Employee where id between 200 to 400")
myconn.commit()
print(cur.rowcount, "row delete, id:", cur.lastrowid)
except:
print("cannot be deleted")
myconn.rollback()
myconn.close()
'''
'''
import mysql.connector
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "admin123",database = "pythonconn")
cur = myconn.cursor()
try:
cur.execute("UPDATE books SET price= 6700 WHERE id = 12")
myconn.commit()
except:
myconn.rollback()
myconn.close()
'''