-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql_select.py
More file actions
64 lines (55 loc) · 2.02 KB
/
mysql_select.py
File metadata and controls
64 lines (55 loc) · 2.02 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
## Connecting to the database
## importing 'mysql.connector' as mysql for convenient
import mysql.connector as mysql
from tqdm import tqdm
## connecting to the database using 'connect()' method
## it takes 3 required parameters 'host', 'user', 'passwd'
db = mysql.connect(
host = "192.168.14.50",
user = "root",
passwd = "avis@123qwe",
database = "luce_dev"
)
print(db)
## creating an instance of 'cursor' class which is used to execute the 'SQL' statements in 'Python'
cursor = db.cursor()
## getting all the tables which are present in 'datacamp' database
cursor.execute("SHOW TABLES")
tables = cursor.fetchall() ## it returns list of tables present in the database
## showing all the tables one by one
for table in tables:
print(table)
err_records = []
MAX_REC = 81429
## defining the Query
for i in tqdm(range(1,MAX_REC)):
cursor.execute('SELECT cid,lac,dtype FROM t_data WHERE id = {0}'.format(i))
cursor.statement #=> 'SELECT id,cid,lac,dtype FROM users WHERE id = 1'
test_rec = cursor.fetchone() #=> (1, 'foo')
#print(test_rec)
#print(i)
is_exist = False
query = "SELECT id FROM t_data where cid={0} and lac={1} and dtype !={2} and id > {3}".format(test_rec[0],test_rec[1],test_rec[2],i)
#print(query)
## getting records from the table
cursor.execute(query)
## fetching all records from the 'cursor' object
records = cursor.fetchall()
## Showing the data
for record in records:
err_records.append(record[0])
is_exist = True
#print(record)
if is_exist == True:
err_records.append(i)
print(err_records)
if len(err_records) > 0 :
query = "SELECT * FROM t_data where id in {0}".format(','.join(['%d'] * len(err_records)))
print(query)
## getting records from the table
cursor.execute(query, err_records)
## fetching all records from the 'cursor' object
records = cursor.fetchall()
## Showing the data
for record in records:
print(record)