I'm stuck working on an Python script. This script should to populate a MySQL table getting values from select statement.
The problem is when I get the values from the select statement, insert into statement doesn't work correctly and not insert all the records. I have check the same select statement in MySQL WorkBench and returns the correct number of records that I want to put in the destination table. The select statement is this and the number of rows expected are more than 100.000 (is a big database):
SELECT MAX(c1),MAX(c2),MAX(c3),MAX(c4),c5,c6,MAX(c7)
FROM Table1 as t1
INNER JOIN Table2 as t2 ON t1.Id = t2.Id
INNER JOIN Table3 as t3 ON t1.Id = t3.Id
WHERE c4 NOT LIKE '%A%'
GROUP BY t1.c1;
This select statement should return records that not contains 'A' character.
And this is my Python code:
import MySQLdb
db = MySQLdb.connect(host,user,pass,dbName)
cursor = db.cursor()
cursor.execute("SELECT MAX(c1),MAX(c2),MAX(c3),MAX(c4),c5,c6,MAX(c7)
FROM Table1 as t1
INNER JOIN Table2 as t2 ON t1.Id = t2.Id
INNER JOIN Table3 as t3 ON t1.Id = t3.Id
WHERE c4 NOT LIKE '%A%'
GROUP BY t1.c1")
results = cursor.fetchall()
for row in results:
cursor.execute("INSERT INTO DestinationTable(col1,col2,col3,col4,col5,col6)VALUES('%s','%s','%s','%s','%s','%s')"
%(str(row[1]),str(row[2]),str(row[3]),str(row[4]),str(row[5]),str(row[6]))
db.commit()
I think the problem is in the Insert into statement, but I'm not sure.