Skip to content

BUG: df.to_sql() shows a case sensitive behaviour with an error message 'table ... already exists' #59886

@SteffenR1098

Description

@SteffenR1098

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import sqlite3
from sqlite3 import Error
import pandas as pd

#db_file_path given to local SQLite DB
print(db_file_path)
table_grouped = 'groupedData' #Table is named "GroupedData" in the SQLite DB

data = [[123, 10.0, '2020-01-01'], [123, 20.0, '2020-02-01'], [123, 15.0, '2020-03-01']]
df_grouped = pd.DataFrame(data, columns=['CustomerID', 'TotalSalesValue', 'SalesDate'])

try:
    db_conn = sqlite3.connect(db_file_path)
    db_conn.row_factory = sqlite3.Row
except Error as e:
    print(e)

#This try block works to show table columns and data in df:
try:
    db_colums = [x.keys() for x in db_conn.cursor().execute(f'select * from {table_grouped};').fetchall()]
    print(db_colums[0])
    print(df_grouped.head(5))
except Error as e:
    print('ERROR Try 1')
    print(e)

#This try block throws: "table "groupedData" already exists"
try: 
    df_grouped.to_sql(table_grouped, db_conn, if_exists='append', index=False) 
    db_conn.commit()
except Error as e:
    print('ERROR Try 2')
    print(e)

#Close db Connection
try:
    db_conn.close()
except Error as e:
    print(e)

Issue Description

Please also see: https://stackoverflow.com/questions/79019484/

Output of code on local machine:


[C:\Users\...SQLite\Data.db)
['CustomerID', 'TotalSalesValue', 'SalesDate']
   CustomerID  TotalSalesValue   SalesDate
0         123             10.0  2020-01-01
1         123             20.0  2020-02-01
2         123             15.0  2020-03-01
ERROR Try 2
table "groupedData" already exists

Using Panadas 2.2.3, sqlite3.version 2.6.0 and python 3.12.5, I get an error "table ... already exits" when using to_sql() with if_exists='append'. I just try to append some data from a Pandas df to a SQLite DB table. Using if_exists='replace' produces the same result. In order to make sure that the db connection is active and the columns match, I used some simple print statements in a previous try block and the failing to.sql in the try block with the to_sql command. Also a "select statement" from the same table is used before and works. The first block is executed without an exception and the second block throws the message 'table "groupedData" already exists'.

It turned out that the problem was case sensitivity. I named the SQLite db table "GroupedData" and used "groupedData" in Python with Pandas. While SQL table names are usually not case-sensitive and also sqlite3 is not case-sensitive with table names, to_sql() from Pandas shows a case sensitive behaviour with a error message not indicating the problem properly. Using table_grouped='GroupedData', the try block with to_sql() worked for both "append" and "replace".

Expected Behavior

Either giving a more specific error message or changing the behaviour of to_sql() to be also non case-sensitive would be my suggestion.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.5
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 170 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : de_DE.cp1252

pandas : 2.2.3
numpy : 2.1.1
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : 8.27.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : 3.9.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions