0% found this document useful (0 votes)
5 views

SQLite

The document provides an overview of databases, specifically focusing on SQLite and SQL commands for creating, inserting, updating, and retrieving data. It includes examples of how to connect to a database, execute SQL commands, and manage data types. Additionally, it highlights important concepts such as SQL injection and the use of order and limit clauses in queries.

Uploaded by

Bavly Zaher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQLite

The document provides an overview of databases, specifically focusing on SQLite and SQL commands for creating, inserting, updating, and retrieving data. It includes examples of how to connect to a database, execute SQL commands, and manage data types. Additionally, it highlights important concepts such as SQL injection and the use of order and limit clauses in queries.

Uploaded by

Bavly Zaher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Informations

1. Database Is A Place Where We Can Store Data


2. Database Organized Into Tables (Users, Categories)

3. Tables Has Columns (ID, Username, Password)


4. There’s Many Types Of Databases (MongoDB, MySQL, SQLite)
5. SQL Stand For Structured Query Language

6. SQLite => Can Run in Memory or in A Single File


7. Data Inside Database Has Types (Text, Integer, Date)

Import SQLite Module

import sqlite3

Create Database And Connect

Command info Example

Create Database
Connect db = sqlite3.connect(“File_Name.db”)
And Connect

Execute(create Create The db.execute(“create table if not exists Table_Name


table if not exists) Tables and Fields ( Field Type , Field Type , … )”)

Close Close Database db.close()

Insert Data Into Database

Command info Example

All Operation in SQL Done By


cursor cr = db.cursor()
Cursor Not The Connection Itself

commit Save All Changes db.commit()

Execute(insert cr.execute(f"insert into Table_Name


Insert
into tabel) (Field_1 , Field_2) values(…,…)")

Retrieve Data From Database

Command info Example

cr.execute(“select
Execute(select) Select field from
Table_Name”)
Command info Example

returns a single record or None if no more rows


fetchone print(cr.fetchone())
are available

fetches all the rows of a query result. It returns all


fetchall the rows as a list of tuples. An empty list is print(cr.fetchall())
returned if there is no record to fetch.

fetchmany(size) like fetct but writing the number of rows print(cr.fetchmany(2))

Update and Delete Data

Command info Example

Update cr.execute(“update Table_Name set field = Value where


Execute(Update)
Data field = Value”)

Delete
Execute(Delete) cr.execute(“delete from Table_Name where field = Value”)
Data

# Import SQLite Module


import sqlite3

# Create Database And Connect


db = sqlite3.connect("app.db")

# Setting Up The Cursor


cr = db.cursor()

# Create The Tables and Fields


cr.execute("create table if not exists users (user_id integer, name text)")
cr.execute("create table if not exists skills (name text, progress integer,
user_id integer)")
cr.execute("delete from users")

# Inserting Data
my_list = ["Ahmed", "Sayed", "Mahmoud", "Ali", "Kamel", "Ibrahim", "Enas"]
for key, user in enumerate(my_list):
cr.execute(f"insert into users(user_id, name) values({key +
1},'{user}')")
# cr.execute(f"insert into users values({key + 1},'{user}')") # we
couldn't write fields

# Update Date
cr.execute("update users set name = 'Mahmoud' where user_id = 1")

# Delete Date
cr.execute("delete from users where user_id = 5")

# Fetch Data
cr.execute("select * from users")

print(cr.fetchone()) # fetchone
print(cr.fetchmany(2)) # fetchmany
print(cr.fetchall()) # fetchall

# Save (Commit) Changes


db.commit()

# Close Database
db.close()

Important Informations

SQL Injection

my_tuple = ('Pascal', '65', 4)


# Inserting Data
cr.execute("insert into skills values(?, ?, ?)", my_tuple)

Select order

cr.execute("select * from skills order by Field_Name ...")


# ... >> asc ‫ تصاعديا‬, desc ‫تنازليا‬
cr.execute("select * from skills order by name limit 3 offset 2")
# limit >> ‫ بحدد عدد الداتا الي هيجيبها‬, offset >> ‫نقطة البداية بعد الرقم‬
‫الي بحدده‬

Select where

cr.execute("select * from skills where user_id > 1")


cr.execute("select * from skills where user_id not in(1, 2, 3)")

You might also like