Skip to content

Instantly share code, notes, and snippets.

@fclesio
Created May 20, 2020 12:51
Show Gist options
  • Save fclesio/052f85db9bd5a3841a6c2dc8c9932475 to your computer and use it in GitHub Desktop.
Save fclesio/052f85db9bd5a3841a6c2dc8c9932475 to your computer and use it in GitHub Desktop.
From SQLite to Pandas
import sqlite3
import pandas as pd
conn = sqlite3.connect('prod.db')
c = conn.cursor()
query_create_table = '''
CREATE TABLE IF NOT EXISTS toplines(
source_id TEXT,
source_name TEXT,
author TEXT,
title TEXT,
description TEXT,
url TEXT,
url_to_image TEXT,
published_at TIMESTAMP,
content TEXT
);
'''
c.execute(query_create_table)
conn.commit()
df____ = pd.read_csv('fake_data.csv', header=None)
df____.columns = ['id', 'natural', 'status', 'city']
df____.to_sql('toplines',
conn,
if_exists='replace',
index = False)
c.execute('''
SELECT * FROM toplines
''')
df = pd.DataFrame(c.fetchall(), columns=['id', 'natural', 'status', 'city'])
df.head(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment