sqlalchemy.exc.ArgumentError: list argument must contain only tuples or dictionaries
P粉080643975
P粉080643975 2023-11-07 21:06:02
0
1
849

I've been trying to use sqlalchemy to dump data into a mysql database. When I try to do this, it gives the errorsqlalchemy.exc.ArgumentError: List arguments must contain only tuples or dictionaries. The following code is used for insertion.

def insert_data(db, table, rows): db.execute(f"INSERT INTO {table} VALUES (%s)", rows) db.commit()
The content in

rowsis as follows.

[(1, 'asdsewadada', 'lajsdljasld', 'lol@gmail.com', 51)]

So, I'm inserting a list of tuples, but still getting the same error.

P粉080643975
P粉080643975

reply all (1)
P粉990568283

Starting with SQLAlchemy version 2, you should use dictionaries instead of tuples:

So this should fix your code:

def insert_data(db: sqlalchemy.engine.base.Engine, query: str, parameters: dict): log_headline: str = "insert_data() ::" """ :param db: :param query: INSERT INTO votes (time_cast, candidate) VALUES (:time_cast, :candidate) :param parameters: {"time_cast": time_cast, "candidate": team} :return: """ # Insert stmt = sqlalchemy.text(query) try: # Using a with statement ensures that the connection is always released # back into the pool at the end of statement (even if an error occurs) with db.connect() as conn: conn.execute(stmt, parameters=parameters) conn.commit() print(f"{log_headline} OK inserted data ") except Exception as e: # If something goes wrong, handle the error in this section. This might # involve retrying or adjusting parameters depending on the situation. print(f"{log_headline} Error {e}")
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!