Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a Tkinter desktop app with SQLalchemy as a database. When I insert a data in database the error below incurred:

What I have tried:

SQL
from sqlalchemy import text, create_engine

engine = create_engine("sqlite:///my_db.db")
my_conn = engine.connect()

def data_insert():
    # Read from the entries box
    my_name = name.get()
    my_class = class1.get()
    my_mark = mark1.get()
    my_gender = gender.get()
    my_address = address.get("1.0", "end")
    my_hostel = hostel.get()

    with engine.connect() as my_conn:
        sql = text("insert into student_address(name, class, mark, gender, hostel, address) values(?, ?, ?, ?, ?, ?)")
        val = (my_name, my_class, my_gender, my_mark, my_hostel, my_address)
        data = my_conn.execute(sql, val)
    
        my_conn.commit()
        my_conn.close()
        print("One student record was added")


The error incurred in my terminal:
Traceback (most recent call last):
  File "/nix/store/ip1pq4prmdrb0jkhv2iqv6rkrwv31gs3-python3-3.11.7/lib/python3.11/tkinter/__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "/home/runner/CTK-CRUD1/.pythonlibs/lib/python3.11/site-packages/customtkinter/windows/widgets/ctk_button.py", line 554, in _clicked
    self._command()
  File "/home/runner/CTK-CRUD1/main.py", line 46, in data_insert
    result = my_conn.execute(sql, val)
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/CTK-CRUD1/.pythonlibs/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1416, in execute
    distilled_parameters = _distill_params_20(parameters)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/sqlalchemy/cyextension/util.pyx", line 32, in sqlalchemy.cyextension.util._distill_params_20
  File "lib/sqlalchemy/cyextension/util.pyx", line 23, in sqlalchemy.cyextension.util._check_item
sqlalchemy.exc.ArgumentError: List argument must consist only of tuples or dictionaries


How could I fix it?
Posted

The problem appears to be that you are relying on positional placement of your parameters, which SQLAlchemy doesn't know how to interpret. Try creating a dictionary instead:
Python
parameters=[
  "name", my_name,
  "class", my_class,
  "gender", my_gender,
  "mark", my_mark,
  "hostel", my_hostel,
  "address", my_address]
Then, pass that into your execute instead of val.
 
Share this answer
 
v2
Comments
Maciej Los 27-Mar-24 3:03am    
5ed!
Pete O'Hanlon 27-Mar-24 3:06am    
Thank you.
sql = "INSERT INTO student_address (name, class, mark, gender, address, hostel) \
               VALUES(:name, :class, :mark, :gender, :address, :hostel)"

           my_data = {
                "name": my_name,
                "class": my_class,
                "mark": my_mark,
                "gender": my_gender,
                "address": my_address,
                "hostel": my_hostel,
           }

           with engine.connect() as my_conn:
               data = my_conn.execute(text(sql), my_data)
               my_conn.commit()
           print("One student record was added")
           print(data)

This is work for me!
 
Share this answer
 
Comments
Maciej Los 27-Mar-24 3:05am    
You should say "THANKS" to Pete, because His answer was very helpful in resolving your issue.
I'd suggest to remove your answer as it is a duplicate of Pete's solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900