Introduction To SQLite 1
SQLite for PYTHON :
INTRODUCTION to SQLite :
SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It’s also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle.SQLite implements a self-contained , server-less, zero-configuration SQL database engine.
SQLite is one of the fastest-growing database engines around not only this its size is nothing as compared to its massive popularity.
Benefits of SQLite :
- Does not require a separate server process or system to operate.
- SQLite need no no setup or administration.
- Cross-platform(UNIX, win32, OS-X, Android, iOS).
- SQLite is very small and sizes less than 400KB.
- Self contained and no external dependencies.
- SQLite is written in ANSI-C and provides simple and easy to use API.
SQLite Commands
DDL- Data Definition Language
CREATE - Creates a new table ,
ALTER - Modify a existing database object ,
DROP - Deletes a entire table.
DML - Data Manipulation Language
INSERT - Insert a records ,
DELETE - To delete records,
UPDATE - Modifies records.
DQL - Data Query Language
SELECT - Retrieves certain records from one or more tables.
SELECT
First import sqlite :
#this will import sqlite as sql in python script>>import sqlite3 as sql
Now make a connection :
To use the module, you must first create a Connection object that represents the database. Here the data will be stored in the student.db file:
>>conn = sql.connect('student.db')
If there is no such database file in the current dir then it will create one, but if there exist a database file with same name and extension(.db)
Then it will be connected to conn.
A (.db) file is not readable directly only sqlite module knows the way to read it.
Now make a cursor :
This cursor will be used to perform various query and commands on the current database which is connected.
>>cur = conn.cursor()
Now your database is connected and ready to be used .
Lets create a table :
>>c.execute(""" CREATE TABLE student_data(first text,last text,age integer) """)
This will make a table with three columns first, last and age :
point to be noted is that there are some types of data that sqlite holds and they are
SQLite_type Python type
NULL None
INTEGER int or long, depending on size
REAL float
TEXT depends on text_factory, unicode by default
BLOB buffer
To insert a row in the above table :
c.execute("INSERT INTO student_data VALUES ("rick","williams",13)")
To print the row that we just inserted :
>>c.execute("SELECT * FROM student_data")
>>print(c.fetchall())
CHECK OUT THE NEXT BLOG FOR MORE ! ! !
Comments
Post a Comment