SQLite for python

My main project for the day is to make a mini-SQLite project. SQLite is simply a lightweight version of a SQL database. The database is created on the disk of the computer. Compared to spinning up a whole server and worrying about configuration. SQLite can be used a small to medium sized program where a whole SQL server is not needed. The advantage of SQLite that it can be ported to a whole SQL server if needed. So SQLite can be used for prototyping SQL servers.

The tutorial I used was from Corey Schafer a youtuber that makes the many informative videos about python and coding in general. I will highly recommend you watch his videos and subscribe to him if you don't already do so. I will note that I haven't finished the project or the tutorial so I will be doing that later next week.

To start the program you need to import your libraries as you would do in any other program. The SQLite import name is called sqlite3. It is part of the python standard library so you don't need to do any pip installations. This makes with dealing the library much easier as your like likely to have installation headaches like trying to find the link for the library making sure the library suit requirements, or making sure the WHL file is correct for the PC. (This is something you may have dealt with if you're trying to learn about machine learning. Cough, cough tensorflow)

After typing the import statement then you need to make a command to connect the program to a database. conn = sqlite3.connect('Insert path here') Should do the job. The brackets are where you will simply give the name of the database as a string form. If the program does not have the database named in the connect statement. the library will make the database for you. So each time you run this statement for the first time the program should create the database for you. After that, you need a way to sure SQL commands. To navigate the database. The next line will include c = conn.cursor() which will make it possible to run SQL commands using the execute function.

c.execute("""CREATE TABLE employees(

first text,

last text,

pay integer) """)

Should make the SQL table for the employees. Using the doc strings will allow us to make commands will multiple lines without any line break. Other people have other ways to make multiple lines commands and statements but this is the one we are sticking to right now. Inside the doc strings and brackets, you can see the SQL code that is used. After typing up your SQL commands you can not run it from there.

You next need to use the commit function will send to SQL statements you have typed up to the database. Think of this command like the git commit command. Running those commands should send all of the statement to the database and return no errors. The line after should have the conn.close command to close the connection between the database. This is simply a good habit to get into.

$\setCounter{0}$
Previous
Previous

Learning Django

Next
Next

Starting my mini SQL project