我有一个网络刮刀,可以在我的机器上本地将数据保存到sqlite数据库。在脚本的开头,我让它加载数据库,
conn = sqlite3.connect('named_database.db')
alldata = pd.read_sql_query("SELECT * FROM table_in_named_database", conn)
会抛出以下错误,
Traceback (most recent call last):
File "/Users/emporerblk/miniconda3/lib/python3.6/site-packages/pandas/io/sql.py", line 1404, in execute
cur.execute(*args)
sqlite3.OperationalError: no such table: table_in_named_database
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./path_to/this_script.py", line 127, in <module>
main()
File "./path_to/this_script.py", line 113, in main
alldata = pd.read_sql_query('SELECT * FROM table_in_named_database', conn)
File "/Users/emporerblk/miniconda3/lib/python3.6/site-packages/pandas/io/sql.py", line 332, in read_sql_query
parse_dates=parse_dates, chunksize=chunksize)
File "/Users/emporerblk/miniconda3/lib/python3.6/site-packages/pandas/io/sql.py", line 1439, in read_query
cursor = self.execute(*args)
File "/Users/emporerblk/miniconda3/lib/python3.6/site-packages/pandas/io/sql.py", line 1416, in execute
raise_with_traceback(ex)
File "/Users/emporerblk/miniconda3/lib/python3.6/site-packages/pandas/compat/__init__.py", line 344, in raise_with_traceback
raise exc.with_traceback(traceback)
File "/Users/emporerblk/miniconda3/lib/python3.6/site-packages/pandas/io/sql.py", line 1404, in execute
cur.execute(*args)
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT * FROM table_in_named_database': no such table: table_in_named_database
我知道数据存在,因为当我通过命令行访问它时,它可以工作。
>>> import sqlite3
>>> import pandas as pd
>>> conn = sqlite3.connect("./path_to/named_database.db")
>>> conn
<sqlite3.Connection object at 0x112efab90>
>>> df = pd.read_sql_query("SELECT * FROM table_in_named_database", conn)
>>> df.head(2)
column \ ...
0 data
1 data
...
没有明显的理由说明为什么同一行代码会在文件中引发错误但效果很好。我的问题是:
1)有谁知道为什么会抛出这个错误?
2)sqlite3是问题吗?如果是这样,你建议用什么替代品?