In our last blog we gave an Overview of DSN Sqlite. Now we will move forward to an interesting topic that is Sqlite connect to python. Database programming with Python may not be considered as a trivial task. Reason being, there are many databases engines which are well supported by Python. Among the several databases available to developers, selecting the right one often depends on the basic requirement of developers and is not only restricted to functionality. However, Sqlite database engine is the popular choice of developers for a number of reasons which are pointed below.
Reasons for Opting Sqlite Database to Connect With Python
Connecting to the Sqlite Database Using Python
Python gained the Sqlite module way back from the 2.5 ver onwards which means that users do not require to install additional tools to access Sqlite 3 database as it comes free with Python.As Python provides the usage of a standard that helps to access all the databases so normally administrators won’t face any problem for connecting across Sqlite databases. On the other hand Sqlite serves as a zero connection embedded database application which employs almost no administrative head.
The whole process of connecting the Sqlite database consists of the following three steps:
Stages of connecting Python with Sqlite Database
Connecting With Sqlite Database Using Python
Python gained the Sqlite module way back from the 2.5 ver onwards which means that users do not require to install additional tools to access Sqlite 3 database as it comes free with Python.As Python provides the usage of a standard that helps to access all the databases so normally administrators won’t face any problem for connecting across Sqlite database.On the other hand Sqlite serves as a zero connection embedded database application which employs almost no administrative head.
The whole process of connecting the Sqlite database consists of the following three steps:
Stages of connecting Python with Sqlite Database
Connecting & Loading of Data into Sqlite Database
Here, an example has been illustrated which shows the creation of the database. The lines basically carries out the work of importing the module with the name Sqlite3 which is required to connect the Sqlite database.
The above statement creates a connection with the database which has been newly created that is known as mydatabasse2.db .Now, this can be also in the form of string such as “:memory:” . In such type of case the database which would be used is an in-memory database and this will get lost once the user exits from the program.
The third line of the coding refers to the creation of the cursor object. The creation of the cursor object helps in interacting with the created database and for adding records.
As soon as the database is created it is immediately reflected in the installed drive
Creation of Table
Here, we are making use of SQL query language to create table named mytable with one integer field and three text fields which are id, username, fname, lname. The SQL supports the usage of only five types of data type that are int, blob, integer, real, text.
Here, we pass in three values as a(in the form of list) list to the execute function which is given as execute().
Before we leave the Sqlite database we need to close the connection,for this we type the command conn.close .The query conn.commit is used for committing the transaction.
Some of the Module Level Functions
Connect()
It is used for making connection to a database which can consists of arguments that are passed as username, passwords,etc.
Cursor ()
This method creates an object cursor for managing the queries.
Execute ()
It is used to run one or more SQL commands against the database.
Commit ()
You can commit the database every time you change it and this is done by Commit ().
Retrieving the Database
For printing the above results,when the above queries were run on on the Python shell editor, the following results were displayed
This is how different items are stored in the database. It looks in the following manner when it is read with the help of Sqlite Browser.
Conclusion
Now, you must have got an idea for how to use Python for creating a Sqlite database. Further, users can also execute actions such as insertion, deletion of records as well as updating of records from the table of Sqlite database. You can also practically anlysis the database by using a Free tool such as Sqlite db viewer.