
Let’s create employees with the following attributes: employees (id, name, salary, department, position, hireDate) Using the cursor object, call the execute method with create table query as the parameter.

From the connection object, create a cursor object.To create a table in SQLite3, you can use the Create Table query in the execute() method. Closing a connection is optional, but it is a good programming practice, so you free the memory from any unused resources. If there are no errors, the connection will be established and will display a message as follows.Īfter that, we have closed our connection in the finally block. Then we have except block, which in case of any exceptions prints the error message. Inside this function, we have a try block where the connect() function is returning a connection object after establishing the connection. Print("Connection is established: Database is created in memory")įirst, we import the sqlite3 module, then we define a function sql_connection.
Sqlite database tutorial code#
This database is called in-memory database.Ĭonsider the code below in which we have created a database with a try, except and finally blocks to handle any exceptions: import sqlite3


This database file is created on disk we can also create a database in RAM by using :memory: with the connect function. When you create a connection with SQLite, that will create a database file automatically if it doesn’t already exist.
