-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql-world.py
More file actions
52 lines (38 loc) · 1.4 KB
/
mysql-world.py
File metadata and controls
52 lines (38 loc) · 1.4 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
import mysql
import mysql.connector
mydb = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='world')
cur = mydb.cursor()
# Use all the SQL you like
cur.execute("SELECT * FROM city ")
# print all the first cell of all the rows
for row in cur.fetchall():
print(row[0],"-->",row[1],"@",row[2],"@",row[3],"@",row[4])
print("---------------------------------------")
print(" WHERE population > 150000")
print("---------------------------------------")
cur.execute("SELECT * FROM city WHERE population > 150000")
# print all the first cell of all the rows
for row in cur.fetchall():
print(row[0],"-->",row[1],"@",row[2],"@",row[3],"@",row[4])
print("---------------------------------------")
print(" WHERE value = 150000")
print("---------------------------------------")
value = "150000"
cur.execute("SELECT * FROM city WHERE population = %s" %(value))
# print all the first cell of all the rows
for row in cur.fetchall():
print(row[0],"-->",row[1],"@",row[2],"@",row[4])
table = 'City'
column = 'Name'
term = 's%'
statement = """select * from %s where %s like '%s'""" %(table, column,term)
command = cur.execute(statement)
results = cur.fetchall()
print(results)
record_list = []
for record in results:
record_list.append(record[0])
for i in range(0, len(record_list)):
print ("%s. %s" %(i+1, record_list[i]))