1

I am writing a python AWS Lambda to execute a MySQL file for RDS MySQL database.

So far it looks like it is working. And I was about to test it by printing how many rows are affected or any other response messages.

Here are how my code looks like after multiple changes. I used this stackoverflow link as a reference to update my original code.

import mysql.connector

def handler():
  config = { <RDS MySQL DB username, password and others...> }
  connect = mysql.connector.connect(**config)
  cursor = connect.cursor(dictionary=True)

  file = open('script.sql')
  sql = file.read()


  for result in cursor.execute(sql, multi=True):
    if result.with_rows:
      print("Rows produced by statement '{}':".format(result.statement))
      print(result.fetchall())
    else:
      print("Number of rows affected by statement '{}':{}".format(result.statement, result.rowcount))

This code throws Failed getting warnings; Use multi=True when executing multiple statements. At first, I was not sure why it is complaining about it as I already passed multi=True to execute funtion. However, I am guessing maybe the result variable holds the multi lines result and I have to loop them to see each query result.

I have tried to iterate them with for in, next, __itra__ and others. However, none of them worked.

I also rollback and debugged a bit.

def handler():
  config = { <RDS MySQL DB username, password and others...> }
  connect = mysql.connector.connect(**config)
  cursor = connect.cursor(dictionary=True)

  with open('scripts}', 'r') as sql:
    result = cursor.execute(sql.read(), multi=True)
    print(result)

The print function returns <generator object MySQLCursor._execute_iter at ...>

So the result is generator object. I tried to turn this into list and I got this error. Failed getting warnings; Use multi=True when executing multiple statements

Can someone please help me how to resolve this issue? Thank you very much in advance.

Shadow
  • 33,525
  • 10
  • 51
  • 64

0 Answers0