MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
December
Sun Mon Tue Wed Thu Fri Sat
 
28 29 30 31      
2025
Months
Dec


Fri, Dec 12, 2025 7:28 pm

Problem with SQLIte database and table names being the same in Python code

I encountered a problem today when I created a Python program to help me automate the process of entering data into an SQLite database. I've been using DB Browser for SQLite on a laptop running Windows 11 to maintain the database and I installed WinPython 3.12.4.1 today to help me automate some tasks related to SQLite databases I maintain on the laptop. I had the Python program prompt me for data to enter into fields in the database for a new entry in the database. But after I entered data at the prompts, I saw the following error message:
Traceback (most recent call last):
  File "N:\Sites\AddReference.py", line 65, in <module>
    main()
  File "N:\Sites\AddReference.py", line 58, in main
    prompt_for_entry(cur, conn)
  File "N:\Sites\AddReference.py", line 44, in prompt_for_entry
    insert_entry(cur, conn, ID, Title, Location)
  File "N:\Sites\AddReference.py", line 12, in insert_entry
    cur.execute(
sqlite3.OperationalError: near "References": syntax error

Line 12 in the program was as a cur.execute command in the following function:

def insert_entry(cur, conn, ID, Title, Location):
    # Insert an entry into the database.
    try:
        cur.execute(
            "INSERT INTO References (ID, Title, Location) VALUES (?, ?, ?)",
            (ID, Title, Location)
        )
        conn.commit()
        print("Entry added successfully.\n")
    except sqlite3.IntegrityError as e:
        print(f"Error: {e}\n")

The database was named References and the table into which I was attempting to add entries was also named References. That doesn't pose an SQLite problem — a table within a database can have the same name as the database itself, but that was posing a problem in the Python code that resulted in the error message I received. When I placed single quotes around References in the command, I no longer received the error message and was able to successfully add new entries to the database. I.e., I now have the following Python code, instead.

def insert_entry(cur, conn, ID, Title, Location):
    # Insert an entry into the database.
    try:
        cur.execute(
            "INSERT INTO 'References' (ID, Title, Location) VALUES (?, ?, ?)",
            (ID, Title, Location)
        )
        conn.commit()
        print("Entry added successfully.\n")
    except sqlite3.IntegrityError as e:
        print(f"Error: {e}\n")

[/languages/python] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo