0% found this document useful (0 votes)
24 views3 pages

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)
24 views3 pages

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

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 = [Link](“File_Name.db”)
And Connect

Execute(create Create The [Link](“create table if not exists Table_Name


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

Close Close Database [Link]()

Insert Data Into Database

Command info Example

All Operation in SQL Done By


cursor cr = [Link]()
Cursor Not The Connection Itself

commit Save All Changes [Link]()

Execute(insert [Link](f"insert into Table_Name


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

Retrieve Data From Database

Command info Example

[Link](“select
Execute(select) Select field from
Table_Name”)
Command info Example

returns a single record or None if no more rows


fetchone print([Link]())
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([Link]())
returned if there is no record to fetch.

fetchmany(size) like fetct but writing the number of rows print([Link](2))

Update and Delete Data

Command info Example

Update [Link](“update Table_Name set field = Value where


Execute(Update)
Data field = Value”)

Delete
Execute(Delete) [Link](“delete from Table_Name where field = Value”)
Data

# Import SQLite Module


import sqlite3

# Create Database And Connect


db = [Link]("[Link]")

# Setting Up The Cursor


cr = [Link]()

# Create The Tables and Fields


[Link]("create table if not exists users (user_id integer, name text)")
[Link]("create table if not exists skills (name text, progress integer,
user_id integer)")
[Link]("delete from users")

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

# Update Date
[Link]("update users set name = 'Mahmoud' where user_id = 1")

# Delete Date
[Link]("delete from users where user_id = 5")

# Fetch Data
[Link]("select * from users")

print([Link]()) # fetchone
print([Link](2)) # fetchmany
print([Link]()) # fetchall

# Save (Commit) Changes


[Link]()

# Close Database
[Link]()

Important Informations

SQL Injection

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


# Inserting Data
[Link]("insert into skills values(?, ?, ?)", my_tuple)

Select order

[Link]("select * from skills order by Field_Name ...")


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

Select where

[Link]("select * from skills where user_id > 1")


[Link]("select * from skills where user_id not in(1, 2, 3)")

You might also like