Repository navigation
Expand file tree
/
Copy pathL14_mysql.py
More file actions
67 lines (47 loc) · 1.61 KB
/
Copy pathL14_mysql.py
File metadata and controls
67 lines (47 loc) · 1.61 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
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="coder",
password="coder",
database="coderdb"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
if ('coderdb',) in mycursor:
print("Database already exists!")
#mydb._database = "coderdb"
#mycursor.execute("SHOW TABLES")
#[print(x) for x in mycursor]
else:
mycursor.execute("CREATE DATABASE coderdb")
mycursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))")
mydb.commit()
"""
mycursor.execute("ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")
mycursor.execute("SHOW TABLES")
[print (x) for x in mycursor]
#for x in mycursor:
# print(x)
print("Enter the number of lines to be inputed:", end=" ")
n = int(input().strip())
val = []
for i in range(n):
print("Enter the name to insert:", end=" ")
name = input().strip()
print("Enter the address to insert:", end=" ")
address = input().strip()
val.append((name,address))
if len(name)<255 and len(address)<255 :
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
# val = (name,address)
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "rows entered! ID:", mycursor.lastrowid)
sql = "SELECT name, address FROM customers WHERE address like %s ORDER BY name DESC"
adr = ( "%way%", )
mycursor.execute(sql, adr)
print(mycursor.fetchall())
#[print(x) for x in mycursor.fetchall()]
#for x in mycursor.fetchall():
# print (x)
"""