Problem with GSpread, Python, and MySQL 'list_to_mysql'

24 Views Asked by At

I have been struggling with multiple versions of this file, and have tried so many things. I originally needed it to automatically pull a spreadsheet out of GSpread, and put it into mySQL. I got one of my versions to do that part, but now I am using the same connection to start bringing up results in another file. The error is the same in that, so the problem is in the connection in this file somewhere. I tried using things like ','.join with various parts of both the data I am bringing in, and the connection string itself. I realize much of my code here is redundant, but it was during a long process of commenting things out and adding back in until I figured out certain aspects.

`

# Import the required libraries
import gspread
import mysql.connector
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
import sqlalchemy
import sqlalchemy.engine
from pandas.io import sql
#import MySQLdb
import pymysql
from sqlalchemy import create_engine



# Define the scope for gspread
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

# Load the credentials from the JSON file
creds = ServiceAccountCredentials.from_json_keyfile_name('redacted.json', scope)

# Authorize the client
client = gspread.authorize(creds)

# Define the sheet name and worksheet index
sheet_name = 'https://redacted'


mysql_host = 'redacted'
mysql_user = 'redacted'
mysql_password = 'redacted'
mysql_database = 'redacted'
mysql_table = 'redacted'

# Connect to the MySQL database
mysql_conn = mysql.connector.connect(
    host="redacted",
    user="redacted",
    passwd="redacted",
    database="redacted"
)
gc = gspread.service_account(filename='redacted.json')
sh = gc.open_by_key('redacted')
worksheet = sh.sheet1
gendata = worksheet.get_all_values()[1:]
#expectedheaders = sheet_ref.row_values(1)
all_records = worksheet.get_all_records()
values = worksheet.get_all_values()
dataframe = pd.DataFrame(values[1:], columns=values[0])

spreadsheet_id = "redacted"
sheet_id = "0"



db_url = "mysql+mysqlconnector://{USER}:{PWD}@{HOST}/{DBNAME}"
db_url = db_url.format(
    USER = "redacted", 
    PWD = "redacted",
    HOST = "redacted",
    DBNAME = "redacted"
)
engine = create_engine(db_url, echo=False)
print(engine)

con = engine.connect()  # may need to add some other options to connect
dataframe.to_sql(name='school', con=con, if_exists='replace', index=True)

mysql_cursor = mysql_conn.cursor()

mysql = """INSERT INTO `questions`.`booksnew` VALUES ({})')"""
#newval=(columns,rows)
mysql_cursor.execute(mysql, gendata)
mysql_conn.commit()


# Commit the changes
mysql_conn.commit()

# Close the cursor and the connection
mysql_cursor.close()
mysql_conn.close()

**The error code I get is: **

Traceback (most recent call last):
  File "C:\**redacted**.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\conversion.py", line 179, in to_mysql
    return getattr(self, "_{0}_to_mysql".format(type_name))(value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'MySQLConverter' object has no attribute '_list_to_mysql'. Did you mean: '_int_to_mysql'?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\**redacted**.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\cursor.py", line 417, in _process_params
    res = [to_mysql(i) for i in res]
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\**redacted**.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\cursor.py", line 417, in <listcomp>
    res = [to_mysql(i) for i in res]
           ^^^^^^^^^^^
  File "C:\**redacted**.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\conversion.py", line 181, in to_mysql
    raise TypeError("Python '{0}' cannot be converted to a "
TypeError: Python 'list' cannot be converted to a MySQL type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:**redacted**bookapp.py", line 6, in <module>
    from update import mysql_conn
  File "c:**redacted**update.py", line 72, in <module>
    mysql_cursor.execute(mysql, gendata)
  File "C:\**redacted**.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\cursor.py", line 539, in execute
    psub = _ParamSubstitutor(self._process_params(params))
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\**redacted**.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\mysql\connector\cursor.py", line 421, in _process_params
    raise errors.ProgrammingError(
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'list' cannot be converted to a MySQL type

I have tried mainly adding various types of string and join conversions, different overall methods, and commenting possibilities in and out. I have checked forums and websites. I am currently gaining experience learning python, and I think I am just stuck on something here I have not quite learned to figure out yet.

0

There are 0 best solutions below