Action

Those import statement is where we put all required libraries. The con variable will handle the connection from / into sqlite database which named “test.db”. Don’t mind the space between import and con, it’s just a habit to give 2 empty line between import statement and code below it.

with con:
con.row_factory = sqlite3.Row # this one
cur = con.cursor()

Now, “this one” is what I prefer because we will get a dictionary when we select some data from sqlite. We can ignore it of course, but the result from our query will be tuple which I do not prefer.

print(data[0], data[1], ..., data[n])

People will not understand what we print unless we tell them.

print(data['id'], data['email'], data['username'], ...)

This one will be more understandable.

“with” statement will handle the resource. We can also handle the resource manually (close the connection) and use try-except to handle the exception.

This is a little code to show how we drop or create table and insert some data into sqlite.

Some explanation

We create new table which named users. It has several field which is id, email, username, password, and authenticated.

We insert a new user into users table.

We find a user with a specified id.

To keep in mind:

I use parameterized queries to insert data. Look for it, it will be helpful for your project.
I also use hashlib to hash the password. In real case, you should use a better security like bcrypt or others.

The next code will show you how to do batch insert with sqlite. Instead of execute, we can use executemany to insert data.