Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error opening sqlite table using pandas

Tags:

python

pandas

   RUN    YR  AP15  PMTE     RSPC  NPPC      NEE     SSF      PRK  QDRN 
0    1  2008  4.53  0.04   641.21  16.8   624.41  328.66  2114.51     0 
1    1  2009  3.17  0.03  1428.30   0.0  1428.30   23.58     3.20     0 
2    1  2010  6.20  0.03  1124.97   0.0  1124.97   23.94    18.45     0 
3    1  2011  5.38  0.02   857.76   0.0   857.76   28.40    42.54     0 
4    1  2012  7.32  0.02   831.42   0.0   831.42   23.92    25.58     0 

I am storing the above dataframe in a sqlite db as follows:

from sqlalchemy import create_engine
db_name = 'sqlite:///C:\\tmp.db'
engine  = create_engine(db_name)
df.to_sql(db_name, engine, if_exists='append')

However, I get an error when I try to read it back:

df = pandas.read_sql_table(db_name, 'sqlite:///C:\\tmp.db')

ValueError: Table sqlite:///C:tmp.db not found

The tmp.db is created, since I can see it in SQLite studio. What am I doing wrong?

like image 939
user308827 Avatar asked Oct 12 '15 14:10

user308827


People also ask

Does pandas work with SQLite?

SQLite via Pandas One cool thing you can do is use both SQLite and Pandas. Pandas has a read_sql_query method that will allow you to return the data as a Pandas dataframe. From there, you can more easily manipulate the data in Pandas.


1 Answers

Specifying a string URL instead of an engine object is only added in the recently released 0.17.0. So you need to first construct the engine object:

engine = sqlalchemy.create_engine('sqlite:///C:\\tmp.db')
df = pandas.read_sql_table(db_name, engine)
like image 75
joris Avatar answered Oct 03 '22 20:10

joris