1

Hi I am trying to write a dataframe to my sql database using df.to_sql however I am getting the error message: TypeError: cannot use a string pattern on a bytes-like object. I am using Python 3.

I am using a path on my drive which I can unfortuantly not share. But it works fine when I just want to open the csv file using. df = pd.read_csv(path, delimiter=';', engine='python', low_memory=True, encoding='utf-8-sig') I am using the encoding item because otherwise their is a strange object at my index column. But also without using encoding I get the same error.

I have also used df.dtypes on my dataframe but no bytes objects. Only int, objects and floats. I have also tried to figure out if I have bytes-like objects in my dataframe using bytes(df[x]).decode('utf-8') but I only get integer columns and not object columns. Which I tried to decode and then use to_sql upon but nothing helps. df.to_sql('DMM_2300', con=engine, index=False, if_exists='append')

Can somebody please help?

    method=method,
  File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 521, in to_sql
    method=method,
  File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 1317, in to_sql
    table.insert(chunksize, method=method)
  File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 755, in insert
    exec_insert(conn, keys, chunk_iter)
  File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 669, in _execute_insert
    conn.execute(self.table.insert(), data)
  File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1449, in execute
    params)
  File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1584, in _execute_clauseelement
    compiled_sql, distilled_params
  File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1680, in _execute_context
    context)
  File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\connectors\mysqldb.py", line 55, in do_executemany
    rowcount = cursor.executemany(statement, parameters)
  File "C:\Users\Bkuijer\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\cursors.py", line 189, in executemany
    m = RE_INSERT_VALUES.match(query)
TypeError: cannot use a string pattern on a bytes-like object ```

1 Answer 1

1

I found the answer. I installed an older version of sqlaclhemy 0.7. After updating it to the latest 1.3.18 version, the error was gone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.