

For more information on SQLite, I'd recommend checking out the official documentation. People = db.execute("SELECT * FROM users")Īnd that’s it for now. To delete all the rows in the table, just remove the WHERE clause: db.execute("DELETE FROM users") # :( people = db.execute("SELECT * FROM users") You can do this by running the following code.
#Install sqlite python how to#
The code for adding another user (in this case, Bob), would be this: db.execute("INSERT INTO users (name, age, fav_food) VALUES(?, ?, ?)", "bob", 20, "burgers") How to read from the databaseĪfter this, we can attempt to read all the users from the database. The value “eesa” gets inserted into the name column, the value 14 is inserted into the age column, and the value “pizza” is inserted into the fav_food column. db.execute("INSERT INTO users (name, age, fav_food) VALUES(?, ?, ?)", "eesa", 14, "pizza") You can use the INSERT operation to add a user. If the table users doesn’t exist, create a table with the name users, with the column names name, age, and fav_food, with the data types for each value specified. To break this down, db is the database that the data is written to. The code for this is: db.execute("CREATE TABLE IF NOT EXISTS users (name TEXT, age NUMBER, fav_food STRING)")

SQL stores data in tables, which are similar to tables found in Excel or Google Sheets. The next step is to create a table in the database. The first step is to create a database.db file in the root directory, which you can do by entering the following command in the terminal: touch database.dbĪt this point, the following code should be added to main.py: from cs50 import SQL

#Install sqlite python install#
I’ll be using CS50’s SQL library, which you can install by running pip3 install cs50. I’m using Replit’s online IDE, but you are welcome to follow along on any IDE you like.įirst, I’ll create a Python project with a main.py file. Here is an example of using SQLite with Python.
#Install sqlite python software#
Many software developer positions involve working with databases, and if you ever consider creating a full-scale application (such as a social media app or an online game), you'll definitely need a database too.

Learning SQLite is a great way to learn how databases operate and how to perform basic CRUD (create, read, update, delete) operations. In this tutorial, you'll learn how to use SQLite with Python. After all, we need to collect data in a location where we can digitally access it for reading, writing, updating, and deleting. Databases are a crucial component in software development.
