The purpose of writing this is for beginners who are curious about backend development or front-end developers who want to learn database technology with server-side programing language.
As we know that server-side language like python, java, and many others are not sufficient for backed developers and even a backend developer needs to be more knowledgeable in database technologies.
So let’s start with the basics of SQLite Database with python.
I chose the SQLite database is just an option, one can apply the same knowledge with any other database also like Mysql and Oracle or any other. the best part of database technology is all the databases are very similar for SQL concepts accept few new Databases.
We are not using any python package to make our job easy for learning these four basic operations.
Inserting or creating a new record within the table. so let’s create an example table within Sqlite Database.
# Creating table into database!!!
import sqlite3
# Connect to sqlite database
conn = sqlite3.connect('students.db')
# cursor object
cursor = conn.cursor()
# drop query
cursor.execute("DROP TABLE IF EXISTS STUDENT")
# create query
query = """CREATE TABLE STUDENT(
ID INT PRIMARY KEY NOT NULL,
NAME CHAR(20) NOT NULL,
ROLL CHAR(20),
ADDRESS CHAR(50),
CLASS CHAR(20) )"""
cursor.execute(query)
# commit and close
conn.commit()
conn.close()
The _conn = sqlite3.connect(‘students.db’) is the connection method and it is pretty simple with SQLite DB but it will differ with different databases.
The _cursor.execute() _ method execute sqlite queries.
"""
CREATE TABLE table_name (
column name datatype properity,
...
...
);
"""
The above syntax can be mapped with the query, there are three main attributes of a create query “column name datatype property”.
After every database operation, we should add a commit and close DB operation.
#sqlite #sql #database #crud #python