0

Below is my code:

# export data to sqlite
    with sqlite3.connect('realtime_crypto.sqlite') as conn:
        df.to_sql('data', con=conn, if_exists='append', index=False)
    

Below is the error showing

OperationalError                          Traceback (most recent call last)
Input In [5], in <cell line: 218>()
    232 # export data to sqlite
    233 with sqlite3.connect('realtime_crypto.sqlite') as conn:
--> 234     df.to_sql('data', con=conn, if_exists='append', index=False)
OperationalError: near ")": syntax error

I tried changing the format still not working.

2
  • What are the columns? Are they all strings? Commented Dec 4, 2022 at 4:44
  • Why are you using sqlite3 connection context manager with pandas? Do you understand what it does and when it should be used? Commented Dec 4, 2022 at 9:24

1 Answer 1

0

Most probably you have single quotes in your dataframe.

I would recommend to use pyathena.pandas.util.to_sql function so if the value contains single quotes, it will be automatically escaped:

import pandas as pd
from pyathena.pandas.util import to_sql

df = pd.DataFrame({"a": ["foo", "bar", "jim o'rourke"]})
to_sql(df, "test", conn)

How do I convert a string into safe SQL String?

https://github.com/laughingman7743/PyAthena/issues/238

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.