What is SQLite3 in Python:
Introduction of SQLIte3 In Python:-
- SQLite3 in Python is popular, open source, Relational Database Management System(RDBMS).
- SQLite3 library is written in c Programming language.
- They provide a lightweight disk-based database.
- That does not required a separate server process and allows accessing the database using a non-standers variant of the the SQL query language.
- SQL stands for structure Query Language.
- This is built-ed module of Python.
- Some application can use SQLite for internal data store.
Key feature of SQLite3 in Python:
- Self contained:– This means that the entire database system is contained in a single library file.
- Server less:- Sqlite3 in Python does not require a server process, making it easy to integrate into application. This data base is store directly in file system.
- Zero Configuration:-No setup or administration is required. It does not required any special configuration to set it up. It run without any administration.
- Cross Platform:- SQLite3 runs on various operating system(OS) such as Windows, Linux,mac-OS,etc.
- Small and Lightweight:- The code base of SQLite3 is very small,making it ideal for embedded systems and mobile applications.
Data Base(DB) Create and connection in SQLite3 in Python:
Steps of data base creation and connection:
step 1:- Import sqlite3 library in python file
import sqlite3
step 2:- Create connection with database abcde.db
con=sqlite3.connect(“abcde.db”)
where:-
‘con’ is a variable,
‘abcde’ is data base name and
‘.db’ is extension of data base.
Types of data type in database SQLite3 in Python:
Number Data Type
- Tiny Int(1-3 digits)
- INT(1-12 digits)
- Big INT(1-20 digits)
String Data Type
- VAR CHAR(0-255 char)
- Text(0-6000 char)
- Long Text(above 6000)
Date and time data type
- Date Time(yyyy-mm-dd HH:MM:SS)
- Date(yyyy-mm-dd)
- Time Stame(Take the current time.)
- Time(HH:MM:SS)
- Year(yyyy)
Type of keys in SQLite3 database in Python:
There are manly three types of keys in SQLite3 in Python
- Primary Key
- Unique Key
- Foreign Key
Primary Key:-
- The primary key constraint uniquely identifies each row in table.
- It must contain UNIQUE value and has an implicit NOT NULL constraint.
Unique Key:-
- A unique key constraint uniquely identified each records in the database.
- This is provides uniqueness for the column or set of column.
- A unique constraint ensure that all value in a column are different.
- This provide uniqueness for the column(s) and helps identify each row uniquely.
- Unique key, there can be multiple unique constraint defined per table.
Foreign Key:-
- A foreign key comprise of single or collection of field in a table that essentially refer to the primary key in another table.
- Foreign key constraint ensure referential integrity in the relation between two table.
- The table with the foreign key constraint is labelled as the child table, and the table containing the candidate key is labelled as the refereed and parent table.
Difference between Primary key and Unique key:
PRIMARY KEY | UNIQUE KEY |
Primary key cannot accept NULL value. | Unique key accept NULL value. |
A table contain only one primary key. | More than one unique present at a time. |
A cluster index automatically created when a primary key is defined. | Unique key generates the non-cluster index. |
How to create Table in Database SQLite3 in Python:
Steps of create table in database SQLite3 in Python:-
Step 1:-Create connection with data base
import sqlite3
con=sqlite3.connect(“abcde.db”)
Step 2:- Create table in data base.
con.execute(”’
create table table_name( column_name_1 Data type,
column_name_2 Data type,
column_name_3 Data type)
”’)
step 3:- Close the connection.
con.close()
Create table with single field/column as a primary key:
In this table one field/column is primary key.
Create table with single field/column as a unique key:
In this table single field/column as a unique key
In this above table student_1 Mo_no as a unique key.
Create table with multiple field/column as unique key:
In this table more than one field as a unique key
In this above table st_email,Mo_no,State all three field has unique key.
How to insert Data into a table in a SQLite3 Database in a Python:
Step to insert data into a table in a SQLite3 database in a Python.
step 1:- Create connection with database
import sqlite3
con=sqlite3.connect(“database_name.db”)
step 2:-write insert Data query
ins=”’insert into table_name(column_1, column_2,column_3,……..,column_n)VALUES(value_1,value_2,value_3,……..,value_n)”’
step 3:- Execute insert data query
con.execute(ins)
step 4:- commit all transaction/connection(Save all Transaction/connection)
con.commit()
step 5:- Close connection to database
con.close()
SQLite3 constraint in Python:
Introduction of SQLite3 constraint in Python:
Sqlite3 in Python constraint are rules used to limit the type of data that can go into a table, to maintain the accuracy and integrity of data inside table.
constraint are used to make sure that the integrity of data is maintained in the database.
Types of constraint:
Following are the most used constraint that can be applied to a table of SQLite3 in Python:
- NOT NULL
- UNIQUE
- PRIMARY KEY
- FOREIGN KEY
- CHECK
- DEFAULT
NOT NULL:-
- By default a column can hold NULL value.
- If you do not want a column to have a NULL value, use the NOT NULL constraint.
- It is used when you want to restricts a column having a NULL value.
Example:- Table with NOT NULL constraint:-
In this above table student_4 St_name field/column has NOT NULL constraint.
NOTE:-More than one field/column in the table can have a NOT NULL constraint defined.
CHECK:-
It is used to restrict the value of a column between a range.
It perform check on the value, before storing them into the database.
Its like condition checking before saving data into a column.
Example:- Table with CHECK constraint:-
DEFAULT Constraint:
The DEFAULT constraint provides a default value for a column when no value is specified.
UNIQUE:-
It ensure that a column will only have unique value.
A unique constraint field can not have any duplicate data.
Example:- Table with UNIQUE constraint:-
MCQ related all about SQLite3 in Python:-
Question 1. What is SQLite in Python?
A. A type of operating system
B. A database engine
C. A programming language
D. A web server
Answer: A database engine
Question 2. Which Python module is used to interact with SQLite databases?
A. sqlite
B. sqlite3
C. sqlmodule
D. sqliteconnector
Answer: B. sqlite3
Question 3. Which method is used to create table in sqlite3 in Python?
A.cursor.create_table()
B. cursore.table()
C.cursore.execute()
D.cursore.create()
Answer:c. cursore.execute()
Question 4. Which method is used to execute SQL queries in SQLite3 in Python?
A. run()
B. exec()
C. executequery()
D. execute()
Answer:D. execute()
Question 5. What is the correct way to close the database connection in SQLite3 in Python?
A. db.close()
B. db.stop()
C. db.shutdown()
D. db.exit()
Answer:A. db.close()
Question 6. How do you create a new SQLite3 database in Python?
A. db=sqlite3.new(‘database.db’)
B. db=sqlite3.connect(‘database.db’)
C. db=sqlite3.open(‘database.db’)
D. db=sqlite3.create(‘database.db’)
Answer:B sqlite3.connect(‘database.db’)
Question 7. Which of the following is not a constraint?
A. UNIQUE
B. Primary Key
C. OVER
D. CHECK
Answer:C OVER
Question 8. Which is correct about primary key?
A. primary key contain duplicate value.
B. Primary key contain Null Value.
C. Primary key is a unique identification of records/row.
D. Primary key is a unique identification of field/column.
Answer:C. Primary key is a unique identification of records/rows.
Question 9. Which is the correct syntax of insert data into table in SQLite3 in Python.
A. Con.execute(”insert into table_name()”)
B. Con.execute(insert from table_name())
C. Con.execute(inset into table_name())
D. Con.execute(‘append into table_name()’)
Answer:A. Con.execute(“insert into table_name()”)
Question 10. Which option is correct for difference between primary key and unique key?
A. Primary key accept null value but unique key not accept null value.
B. Primary key not accept null value but unique key accept null value.
C. A table contain more than one primary key as well as unique.
D. A table contain one unique key but more than one primary key.
Answer:B. Primary key not accept null value but unique key accept null value.
[…] Click here for “all about Introduction of SQLite3 in Python.” Previous articleAll about SQLite3 in Python ankushraj8757377106@gmail.comhttp://studymoral.com RELATED ARTICLES SQLITE […]
[…] click here “Introduction of SQLite3 in Python” click here “All about SLite3 commands in Python” Previous articleAll about SQLite3 command in Python. ankushraj8757377106@gmail.comhttp://studymoral.com var block_tdi_23 = new tdBlock(); block_tdi_23.id = "tdi_23"; block_tdi_23.atts = '{"title_tag":"div","modules_on_row":"eyJhbGwiOiIzMy4zMzMzMzMzMyUiLCJwaG9uZSI6IjEwMCUifQ==","limit":"3","modules_category":"image","show_btn":"none","show_excerpt":"none","ajax_pagination":"next_prev","sort":"","category_id":"_related_cat","f_title_font_size":"eyJwaG9uZSI6IjE0IiwiYWxsIjoiMTMiLCJwb3J0cmFpdCI6IjEyIn0=","f_title_font_line_height":"eyJwaG9uZSI6IjEuNCIsImFsbCI6IjEuNCJ9","modules_gap":"eyJhbGwiOiIyMCIsInBvcnRyYWl0IjoiMTUiLCJwaG9uZSI6IjE1In0=","show_com":"none","show_date":"eyJhbGwiOiJub25lIiwicGhvbmUiOiJpbmxpbmUtYmxvY2sifQ==","show_author":"none","image_height":"70","f_title_font_weight":"500","all_modules_space":"eyJhbGwiOiIyMCIsImxhbmRzY2FwZSI6IjIwIiwicG9ydHJhaXQiOiIxNSIsInBob25lIjoiMjYifQ==","custom_title":"RELATED ARTICLES","image_floated":"eyJwaG9uZSI6ImZsb2F0X2xlZnQifQ==","image_width":"eyJwaG9uZSI6IjMwIn0=","meta_info_align":"","meta_margin":"eyJwaG9uZSI6IjAgMCAwIDE2cHgifQ==","meta_padding":"eyJwaG9uZSI6IjAifQ==","video_icon":"eyJwb3J0cmFpdCI6IjI0IiwicGhvbmUiOiIyNCJ9","image_size":"td_485x360","art_title":"eyJwaG9uZSI6IjAgMCA2cHggMCJ9","block_type":"td_flex_block_1","separator":"","custom_url":"","block_template_id":"","mc1_tl":"","mc1_title_tag":"","mc1_el":"","post_ids":"-473","taxonomies":"","category_ids":"","in_all_terms":"","tag_slug":"","autors_id":"","installed_post_types":"","include_cf_posts":"","exclude_cf_posts":"","popular_by_date":"","linked_posts":"","favourite_only":"","offset":"","open_in_new_window":"","show_modified_date":"","time_ago":"","time_ago_add_txt":"ago","time_ago_txt_pos":"","review_source":"","el_class":"","td_query_cache":"","td_query_cache_expiration":"","td_ajax_filter_type":"","td_ajax_filter_ids":"","td_filter_default_txt":"All","td_ajax_preloading":"","container_width":"","m_padding":"","modules_border_size":"","modules_border_style":"","modules_border_color":"#eaeaea","modules_border_radius":"","modules_divider":"","modules_divider_color":"#eaeaea","h_effect":"","image_alignment":"50","image_radius":"","hide_image":"","show_favourites":"","fav_size":"2","fav_space":"","fav_ico_color":"","fav_ico_color_h":"","fav_bg":"","fav_bg_h":"","fav_shadow_shadow_header":"","fav_shadow_shadow_title":"Shadow","fav_shadow_shadow_size":"","fav_shadow_shadow_offset_horizontal":"","fav_shadow_shadow_offset_vertical":"","fav_shadow_shadow_spread":"","fav_shadow_shadow_color":"","video_popup":"yes","video_rec":"","spot_header":"","video_rec_title":"","video_rec_color":"","video_rec_disable":"","autoplay_vid":"yes","show_vid_t":"block","vid_t_margin":"","vid_t_padding":"","video_title_color":"","video_title_color_h":"","video_bg":"","video_overlay":"","vid_t_color":"","vid_t_bg_color":"","f_vid_title_font_header":"" […]