简体   繁体   中英

How to convert Pandas dataframe to SQL

want to convert pandas dataframe to sql. I also want to get the .sql on my desktop with my sql table.

This is the code that I have:

import pandas as pd
from sqlalchemy import create_engine

df = pd.DataFrame({'second':[5,6,7,8,9],
                   'first':['ne', 'da', 'ne', 'da', 'da'],
                   'third':[213,151,16,641,64]})

print(df)

engine = create_engine('sqlite://', echo=False)

sample_sql_database = df.to_sql('sample_database', con=engine)
sample_sql_database = engine.execute("SELECT * FROM sample_database").fetchall()

print(sample_sql_database)

Im using to_sql , my code does not shows any errors, but I do not know how to get my table to appears on deskop

You can't see it on your desktop because you did not mention correct db_uri. Please mention name atleast after the basic syntax:

create_engine('sqlite://** mention name here **', echo=False)

This worked for me, try it:

import pandas as pd
from sqlalchemy import create_engine

df = pd.DataFrame({'second':[5,6,7,8,9],
                   'first':['ne', 'da', 'ne', 'da', 'da'],
                   'third':[213,151,16,641,64]})
print(df)

db_uri = 'sqlite:///file.db'
engine = create_engine(db_uri, echo=False)

sample_sql_database = df.to_sql('sample_database', con=engine)

sample_sql_database = engine.execute("SELECT * FROM sample_database").fetchall()

print(sample_sql_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