1

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

1
  • Your table is exported to your database called 'sample_database'. You have to query your databse to see the result. You cannot 'open it on your desktop'. Download sqlite to query your db. Commented Jul 18, 2019 at 9:20

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

1 Comment

It works, i got the db.sqlite file on deskop, but when I open it on my deskop, its all NULL values, how to see values from datagrame

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.