Python SQLite3 Database Tutorial

This tutorial will show how to start using the built in database that Python provides. The library comes pre-installed with Python and is named SQLite3 . SQLite can be used to create a tables , databases, and run queries.

To learn more about SQLite and what it can do, check out their site . They provide very good documentation , here is the link to reference for this tutorial.

Step One :

First, the sqlite3 library must be imported. Then the database file must be created. Connection or connect allows the sending and receiving of data such as strings. The Cursor instance allows the connection to send and receive data.

import sqlite3

#Creating table
dataBase = sqlite3.connect('Test.db')

cur = dataBase.cursor()

Step Two:

Now that the table has been created, the data it contains must be given a label. Here the table will store the user data. Each user will be given a Name and Phone number. In SQLite tables, the values each label is going to store needs to be set.

Notice how the Name label is set to store a text value and the Phone is set to store a real value. Once the label are created, user data can then be added to the table

#Setting table labels
cur.execute('''CREATE TABLE user
             (Name text, Phone real)''')

# Insert  data
cur.execute("INSERT INTO user VALUES ('Joe',1234567)")

Final Step

Lastly, the connection to the database must be closed and the changes to the database must be Committed.

# Save (commit) the changes. ALWAYS COMMIT any changes
dataBase.commit()

#Closing connection
dataBase.close()

Testing Database

Lets check if the database file is running correctly. Here the execute method is used to check if the SQL table is storing the user’s Name Joe“. If the table contains a user with a Name equal to Joe, it will print out that user’s info. The method fetchone() will be used because only one user will be returned

import sqlite3

#Creating table
dataBase = sqlite3.connect('Test.db')
cur = dataBase.cursor()

var = ('Joe',)
#Check if Name is being stored
cur.execute('SELECT * FROM user WHERE Name=?', var)
print (cur.fetchone())

Output:

('Joe', 1234567)

Android Studio Loading Animation Between Activities

Progress Dialog is dialog showing a progress indicator and an optional text message or view. The methods of Progress Dialog being used in this tutorial are: ProgressDialog.setTitle() – Used to set title of dialog box ProgressDialog.setMessage() – Used to set dialog message being displayed ProgressDialog.setProgressStyle() – Choose the style of indicator ProgressDialog.dismiss() – Dismiss the … Read more Android Studio Loading Animation Between Activities

Android Studio Tutorial SeekBar

SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task. In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value … Read more Android Studio Tutorial SeekBar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s