Know How to Use Sqlite in Python

admin | June 3rd, 2015 | General

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

  • Firstly, Python provides an interface for operating the Sqlite databases and is generally available free with the Sqlite 3 database as standard
  • Sqlite database does not employ any constraints on the implementation of types. There are many databases where the type defined for a specific column rigidly specifies as what users can store in that column. Whereas in Sqlite, users won’t find such kind of restrictions. You can possibly store a string value in a column whose type may be an Integer. This way of managing data over the tables and columns is very similar to that of Python.
  • The composite framework of sustained reliability, type system very much similar to Python’s own approach of dealing with values, availability as a free database present an environment for which Sqlite database engine forms as the most suitable option.

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 to the Sqlite database engine
  • Creating the table and inserting items into it
  • Running queries against the created table for updation, deletion, etc.
  • Reading out the final results from Sqlite database engine The composite framework of sustained reliability, type system very much similar to Python’s own approach of dealing with values, availability as a free database present an environment for which Sqlite database engine forms as the most suitable option.

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 to the Sqlite database engine
  • Creating the table and inserting items into it
  • Running queries against the created table for updation, deletion, etc.
  • Reading out the final results from Sqlite database engine

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.

phython sqlite command 1

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 drivepython sqlite command 2

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.

python sqlite command 3Insertion of Items into Table

Here, we pass in three values as a(in the form of list) list to the execute function which is given as execute().

python sqlite command 4

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
python Sqlite command 5
For printing the above results,when the above queries were run on on the Python shell editor, the following results were displayed
python sqlite command 6

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.
python Sqlite command 7Conclusion

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.

Disclaimer : – © 2020 Sqlite Viewer is an independent provider of Sqlite products & services. Sqlite Viewer is not in affiliation with any of the third–party organizations unless it is expressed explicitly. Read More...