3

I have an sql file locally stored in my PC. I want to open and read it using the pandas library. Here it iswhat I have tried:

 import pandas as pd
 import sqlite3

 my_file = 'C:\Users\me\Downloads\\database.sql'
 #I am creating an empty database
 conn = sqlite3.connect(r'C:\Users\test\Downloads\test.db')
 #I am reading my file 
 df = pd.read_sql(my_file, conn)

However, I am receiving the following error:

DatabaseError: Execution failed on sql 'C:\Users\me\Downloads\database.sql': near "C": syntax error

7
  • 2
    try my_file = r'C:\Users\me\Downloads\\database.sql' so you pass a raw string, or use forward slashes Commented Mar 8, 2017 at 13:15
  • @EdChum Didn't work :( Commented Mar 8, 2017 at 13:17
  • remove the double \\ and change to a single \ in the .sql path, with the raw string encoding as what EdChum said earlier Commented Mar 8, 2017 at 13:18
  • how about my_file = 'C://Users/me/Downloads/database.sql' Commented Mar 8, 2017 at 13:18
  • To elaborate on @EdChum's comment, Python will try to convert everything after '\U' in 'C:\Users' to unicode which will break the "path string". Definitely use r to signify it's a raw string. Also see: en.wikipedia.org/wiki/… Commented Mar 8, 2017 at 13:19

1 Answer 1

1

Try moving the file to D:// Sometimes Python is not granted access to read/write in C. Hence may be that is an issue.

You can also try alternative method using cursors.

cur=conn.cursor()

r=cur.fetchall()

This r would contain a tuple of your dataset.

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.