# prepare the statement
insert_statement = 'INSERT INTO database.tables VALUES (%s,%s, %s, %s, %s);'
# loop through each BQ row
for row in query_response['rows']:
# prepare the set of values
# strongly advise sanitizing the values before inserting
# type checks, value checks, SQL injection checks, etc.
values_to_insert = (
row[0],
row[1],
row[2],
row[3],
row[4])
# insert data
cursor.execute(insert_statement, values_to_insert)
On Tuesday, June 14, 2016 at 3:31:02 PM UTC-4, truptanand_badatya@homedepot.com wrote:
My Python program connects to big query and fetching data which I want to insert into a mysql table.
Its successfully fetching the results from bigquery. Its also successfully connecting to mysql DB. but its not inserting the data I see its complaining for the row[1] .
Whats the right way to insert the values from bigquery response into mysql table columns.
I was following the sample code @ https://cloud.google.com/
bigquery/create-simple-app- but my requirement is not to pring but to insert the data into mysql table/api#completecode query_data = {mybigquery}
query_response = query_request.query(projectId=
'myprojectid',body=query_data) .execute() for row in query_response['rows']: cursor.execute ("INSERT INTO database.table VALUES ('row[0]','row[1]','row[2]','
row[3]','row[4]');") Traceback (most recent call last): File "./myfile.py", line 32, in <module> cursor.execute ("INSERT INTO database.datable VALUES ('row[0]','row[1]','row[2]','
row[3]','row[4]');") File "/usr/lib64/python2.7/site- packages/MySQLdb/cursors.py", line 174, in execute self.errorhandler(self, exc, value) File "/usr/lib64/python2.7/site- packages/MySQLdb/connections. py", line 36, in defaulterrorhandler raise errorclass, errorvalue_mysql_exceptions. OperationalError: (1366, "Incorrect integer value: 'row[0]' for column 'CountAll' at row 1")
Also, I tried to use
cursor.execute ("INSERT INTO database.table VALUES (%s,%s,%s,%s,%s);")
Traceback (most recent call last): File "./myfile.py", line 32, in <module> cursor.execute ("INSERT INTO database.table VALUES (%s,%s,%s,%s,%s);") File "/usr/lib64/python2.7/site-
packages/MySQLdb/cursors.py", line 174, in execute self.errorhandler(self, exc, value) File "/usr/lib64/python2.7/site- packages/MySQLdb/connections. py", line 36, in defaulterrorhandler raise errorclass, errorvalue_mysql_exceptions. ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s,%s,%s,%s,%s)' at line 1") or
cursor.execute ("INSERT INTO database.table VALUES (row[0],row[1],row[2],row[3],
row[4]);") Traceback (most recent call last): File "./myfile.py", line 32, in <module> cursor.execute ("INSERT INTO database.table VALUES (row[0],row[1],row[2],row[3],
row[4]);") File "/usr/lib64/python2.7/site- packages/MySQLdb/cursors.py", line 174, in execute self.errorhandler(self, exc, value) File "/usr/lib64/python2.7/site- packages/MySQLdb/connections. py", line 36, in defaulterrorhandler raise errorclass, errorvalue_mysql_exceptions. ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[0],row[1],row[2],row[3],row[ 4])' at line 1") But in all it fails while inserting values in mysql table
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-cloud-sql-discuss/b29a0057-cacc-44c2-82d6-bf59b31215a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment