简体   繁体   English

使用pandas读取sql文件时出错

[英]Error in reading an sql file using pandas

I have an sql file locally stored in my PC. 我有一个本地存储在我的PC中的SQL文件。 I want to open and read it using the pandas library. 我想用pandas库打开并阅读它。 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 DatabaseError:在sql'C:\\ Users \\ me \\ Downloads \\ database.sql'上执行失败:在“C”附近:语法错误

Try moving the file to D:// Sometimes Python is not granted access to read/write in C. Hence may be that is an issue. 尝试将文件移动到D://有时Python不被允许访问C中的读/写。因此可能是一个问题。

You can also try alternative method using cursors. 您还可以尝试使用游标的替代方法。

cur=conn.cursor() CUR = conn.cursor()

r=cur.fetchall() R = cur.fetchall()

This r would contain a tuple of your dataset. 这个r将包含数据集的元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM