简体   繁体   中英

How to convert pandas dataframe to sqlite database

I have a single database .pd which I want to disaggregate into several tables to form a single sql database .db

I have previously created the empty .db

data_path = r'C:\Users\User\Desktop\homeworkpython\db.db'

conn = sql.connect(data_path)
cursor = conn.cursor()

The mother base is 'df'.

so, df.head(1) show

coordinates     created_at     extended_entities     geo      id
  None          Sat Dec 14           NaN            None      1

I create my first table

df1 = df[['created_at ','id']]
from sqlalchemy import create_engine
engine = create_engine(r'sqlite:///C:\Users\User\Desktop\homeworkpython\db.db')

df1.to_sql('tab1', engine)

But i have doubts about whether the table was well created and is inside the .db base, how to know?

option 1

read table data into dataframe, you may use these link

df2 = pd. read_sql('select * from tab1', engine)
print(df2)

option 2

You can install any sqlite database explorer like one in these link

Using these tool, you can explore the database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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