0

I need to import a csv file into a database in order to query it in a Python notebook and I tried several commands but every time I get a syntax error. I created a new empty database using

conn_british = sqlite3.connect('path\db name')

This is the code I tried

query='''BULK INSERT 
         {table_name}
         FROM '\Documents\file.csv'
         [WITH
         (
         [FORMAT = 'CSV'], 
         [FIELDQUOTE = '"'],
         [FIRSTROW = 2],
         [FIELDTERMINATOR = ','],  
         [ROWTERMINATOR = '\n'],   
         [TABLOCK]
         )]  '''

pd.read_sql_query(query,conn_british)

and the error : near "BULK": syntax error

I also tried

     '''COPY table_name 
     FROM '\Documents\file.csv' 
     DELIMITER ','
     '''

but I get the same error

5
  • Does this answer your question, stackoverflow.com/questions/14947916/import-csv-to-sqlite Commented May 26, 2020 at 12:09
  • Or this: stackoverflow.com/questions/18219779/… Commented May 26, 2020 at 12:15
  • @Sushanth no, i don't think it works like that in python notebook Commented May 26, 2020 at 12:21
  • 1
    Since ur using pandas, load the CSV as dataframe and use to_sql and dump to db stackoverflow.com/questions/14431646/… Commented May 26, 2020 at 12:27
  • @Sushanth I tried it and get another error: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': Error binding parameter 0 - probably unsupported type. Commented May 26, 2020 at 12:32

1 Answer 1

0

I think the question is you just want to run SQL queries on a CSV. You can use FugueSQL to do that.

A sample Python snippet would be:

from fugue_sql import fsql

query = """
df = LOAD "/path/to/myfile.csv"

SELECT * 
  FROM df
 WHERE col > 1
 PRINT
"""
fsql(query).run()

and this will use Pandas to run the query by default. There is also a SAVE keyword so you can save the output somewhere.

This pushes down the operations to Pandas, so you don't need this intermediate SQLite persistence.

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.