![]() Sqlite databases are great for local experimentation and are used extensively on mobile phones. Python’s build in sqlite library coupled with Pandas DataFrames makes it easy to load CSV data into sqlite databases. pd.read_sql('''SELECT * FROM users u LEFT JOIN orders o ON u.user_id = o.user_id''', conn) Next steps You can also read the SQL query directly into a Pandas DataFrame. Here’s the array that’s returned: [(1, 'pokerkid', 1, 1, 'speaker'), Join the users and orders tables on the user_id value and print the results: c.execute('''SELECT * FROM users u LEFT JOIN orders o ON u.user_id = o.user_id''') Orders.to_sql('orders', conn, if_exists='append', index = False) # write to sqlite table Fetch results of database join Orders = pd.read_csv('orders.csv') # load to DataFrame c.execute('''CREATE TABLE orders (order_id int, user_id int, item_name text)''') Suppose you have the following orders.csv file: order_id,user_id,item_nameĬreate a table and then load the orders data into the database. Cursors can be thought of as iterators in the database world. The fetchall() method returns an array of tuples.Ĭ.execute() returns a sqlite3.Cursor object. Fetch values from sqlite tableįetch all the rows from the users table: c.execute('''SELECT * FROM users''').fetchall() # The to_sql method makes it easy to write DataFrames to databases. Users.to_sql('users', conn, if_exists='append', index = False) Pandas makes it easy to load this CSV data into a sqlite table: import pandas as pd Suppose you have the following users.csv file: user_id,username c.execute('''CREATE TABLE users (user_id int, username text)''') Load CSV file into sqlite table import sqlite3Įxecute a query that’ll create a users table with user_id and username columns. You can create the file with touch my_data.db or with this equivalent Python code: from pathlib import PathĪ zero byte text file is a great starting point for a lightweight database! Creating sqlite tableĬreate a database connection and cursor to execute queries. Sqlite is a lightweight database that can be started as an empty text file. Python is perfect language for this task because it has great libraries for sqlite and CSV DataFrames. insert_all (, pk = "id" )Ĭheck out the full library documentation for everything else you can do with the Python library.This blog post demonstrates how to build a sqlite database from CSV files. Database ( "demo_database.db" ) # This line creates a "dogs" table if one does not already exist: db. You can also import sqlite_utils and use it as a Python library like this: import sqlite_utils db = sqlite_utils. See the full CLI documentation for comprehensive coverage of many more commands. Sqlite-utils memory lets you import CSV or JSON data into an in-memory database and run SQL queries against it in a single command: $ cat dogs.csv | sqlite-utils memory - "select name, age from stdin" Or for data in a CSV file: $ sqlite-utils insert dogs.db dogs dogs.csv -csv | sqlite-utils insert releases.db releases -pk id You can import JSON data into a new database table like this: $ curl \ ![]() $ sqlite-utils dogs.db "select * from dogs" -table ![]() $ sqlite-utils dogs.db "select * from dogs" -csv $ sqlite-utils dogs.db "select id, name from dogs" ![]() $ sqlite-utils insert dogs.db dogs dogs.csv -csv Now you can do things with the CLI utility like this: $ sqlite-utils memory dogs.csv "select * from t" Or if you use Homebrew for macOS: brew install sqlite-utils Read more on my blog, in this series of posts on New features in sqlite-utils and other entries tagged sqliteutils.
0 Comments
Leave a Reply. |