Skip to main content

How to create database in sqlite3 database engine in Python

hi, how are you today


Hey, today we will discuss how to create database  in SQLite3 database engine which is inbuilt in python.

What is SQLite3?

SQLite3 is a database engine, in which we can create one or more databases.


ADVANTAGES

It is easy to use.
It is very fast and lightweight.
Entire database is stored in a single disk.
It is self-contained, server-less.
used in lot of applications as an internal storage.




What is Database?

In simple words, database is a collection of tables and documents. For now, the database we create in SQLite3 database engine is collection table(s).


How to connect to SQLite3 in python?

As already informed it is inbuilt in python so we have to simply import it before use.

 import sqlite3   
 or  
 import sqlite3 as sql  

So as you can see above that in two ways we can import it. One without alias name and secondly with some alias name.

The only difference is if we import it with alias name than we have to go with alias name in the program and if not imported as alias name than we have to use module name.

We will discuss it with both the cases as the use of this concept is only to get connection with the database or to create database.



Let's create database and get connection


1:  import sqlite3   
2:  conn= sqlite3.connect("mydatabase.db")  
3:  print(conn)  
4:  conn.close()  

In above code, we directly imported sqlite3 without alias.

1:  import sqlite3 as sql  
2:  conn= sql.connect("mydatabase.db")  
3:  print(conn)  
4:  conn.close()  

In  above code we imported the sqlite3 with alias name 'sql'.

So only use is in the time of  getting connected to database.

connect() function will accept database name in parameter that u want to connect with. The basic role of this function is to get u connection to the database name you provide and returns the connection
The basic advantage is, if the database is not present, connect() will create the database.

You have option to create database whether you want to create database in same folder or different folder. If you want to create it in different folder, simply give he path to that folder.

 conn= sql.connect(r"path/mydatabase.db")  

Put 'r' before string to make it raw  .

 conn= sql.connect(mydatabase.db")  

so in above statement, "mydatabase.db" is the name of database. You have to provide extension .db manually.
So here we are connecting to database and collecting the connection in a variable named conn which we will use further to create table.

Always remember to close the connection before closing or quitting your software

 conn.close()  

Hope you all like this post and I wish this proves useful to you.
Keep sharing & give your views about post in comments.
Comments are always welcomed for better improvement.

Thanks :)


Comments

Translate to your language

Popular posts from this blog

Different Stages of I2C communication

Different Stages Of I2C

How to import Pygame in pycharm

hi, how are you today

Working demonstration of UART communication using 2 8051 uController on Proteus with Embedded C code.

Here we are going to use 2 at89c51 uControllers just to understand UART communication in 8051.