

You can get all of the code and data you’ll see in this tutorial by clicking on the link below:ĭownload the sample code: Click here to get the code you’ll use to learn about data management with SQLite and SQLAlchemy in this tutorial.
#Python sqlite how to
In this tutorial, you’ll learn how to use: Below, you’ll explore using SQL databases and flat files for data storage and manipulation and learn how to decide which approach is right for your program. Flat files are often human-readable text files-though they can also be binary data-with a structure that can be parsed by a computer program. You can achieve similar results using flat files in any number of formats, including CSV, JSON, XML, and even custom formats. Python, SQLite, and SQLAlchemy give your programs database functionality, allowing you to store data in a single file without the need for a database server. conn = sql.connect('weather.db') weather = pd.All programs process data in one form or another, and many need to be able to save and retrieve that data from one invocation to the next. Here’s the code (the query is passed as a string). It means select all columns from the table called weather and return that data. This is just about the simplest SQL query you can make. But to do this we have to sendĪ query to the database: SELECT * FROM weather Then we use the sql_read method from Pandas to read in the data. Here is how we load the database table into a dataframe.įirst, we make a connection to the database in the same way as before. So, we have a database with our weather data in it and now and we want to read it into a dataframe.
#Python sqlite download
We could now start a new notebook or Python program to do the rest of this tutorial or simply comment out the code to download and create the database. Let’s assume that we have run the code above once and we have our database, weather, with the table, also called weather, in it. We don’t need to run this code ever again unless the original data changes and, indeed, we shouldn’t, because SQLIte will not allow us to create a new table in the database if one of the same name already exists. conn = sql.connect('weather.db') weather.to_sql('weather', conn) Now we are going to save the dataframe in an SQLite database.įirst we open a connection to a new database (this will create the database if it doesn’t already exist) and then create a new table in that database called weather. The temperatures are in degrees Celsius, the rainfall is in millimetres and ‘Sun’ is the total number of hours sunshine for the month. The data is recorded for each month of the year. We download it like this: weather = pd.read_csv('')Īnd this is what it looks like.

There’s about 70 years worth of temperature, rainfall and sunshine data in a csv file.

Obviously, we need the SQLite and Pandas libraries and we’ll get matplotlib as well because we are going to plot some graphs. Let’s start by importing the libraries: import sqlite3 as sql import pandas as pd import matplotlib.pyplot as plt It’s derived from public domain data from the UK Met Office and you can download it from my Github account.Īll the code here was written in a Jupyter notebook but should run perfectly well as a standalone Python program, too. I’m going to demonstrate a few simple techniques using SQLite and Pandas using my favourite London weather data set.

You can select and filter the data using simple SQL commands: this saves you having to process the dataframe itself. You can permanently store your dataframes in a table and read them directly into a new dataframe as you need them.īut it isn’t just the storage aspect that is so useful. Using it with Pandas is simple and really useful. It is not a complete implementation of SQL but it has all the features that you need for a personal database or even a backend for a data-driven web site. The SQLite database is a built-in feature of Python and a very useful one, at that.
