You are on page 1of 2

Harri son i n Graphi ng / Chart i ng Mat pl ot l i b | June 14, 2014

SQL Database with Python


Part 1 Inserting into a
Database
How to use Pythons SQLite
This is part 6 of the massive Matplotlib tutorial series offered here.
This can also just be a stand-alone mini-series on Pythons SQL module sqlite.
In this tutorial, we talk about where to get sqlite, and how to set up a database.
Databases offer, typically, a superior method of high-volume data input and
output over a typical file such as a text file. sqlite is a light version that works
based on SQL syntax. SQL is a programming language in itself, but is a very
popular database language. Many websites use MySQL, for example.
Once you have sqlite, the first thing you must do is create the actual table itself.
This only needs to be done once, but of course must be done.
Here is the sample code that accompanies the video:


Python Programming Tutorials Image Recognition Data Analysis Robotics
Django Web Development Matplotlib (Graphing) PyGame Sentiment Analysis / NLP
Basics
1
2
3
4
5
6
7
import sqlite3

conn = sqlite3.connect('tutorial.db')
c = conn.cursor()

def tableCreate():
c.execute("CREATE TABLE stuffToPlot(ID INT, unix REAL, datestamp TEXT, keyword TEXT, value
The above code will first create a database file called tutorial. Then the code will
create a table called stuffToPlot with tableCreate(). This tableCreate function
should only be run ONCE. Not again. Subsequently, we can use dataEntry() to
actually enter some sample data into the database.
Notably, this code is problematic since we are having to still type out the values.
What if we have a program that derives values and needs to insert them into the
database? The next tutorial covers dynamically inserting values into our
sqlite database.

Write a Comment
RELATED CONTENT BY TAG CREATE TABLE DATABASE PYTHON SQLITE SQLITE3 STORING DATA
Independent Publisher empowered by WordPress
Harrison Published
June 1 4, 201 4
8
9
10
11
12
13
14
REAL)")


def dataEntry():
c.execute("INSERT INTO stuffToPlot VALUES(1, 1365952181.288,'2013-04-14 10:09:41','Python
Sentiment',5)")
c.execute("INSERT INTO stuffToPlot VALUES(2, 1365952257.905,'2013-04-14 10:10:57','Python
Sentiment',6)")
c.execute("INSERT INTO stuffToPlot VALUES(3, 1365952264.123,'2013-04-14 10:11:04','Python
Sentiment',4)")
conn.commit()

You might also like