SQL Sesion O - O
SQL Sesion O - O
SQL Sesion O - O
MySQL
PostgreSQL
Oracle
Mongo DB
Microsoft SQL Server
SQLite
SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The
code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. SQLite is one of the most widely
deployed databases in the world.
Pros
Cons
Google Chrome
WordPress
Instructions
1. Go to https://www.sqlite.org/index.html
2. Go to //www.sqlite.org/download.html
3. Look for Precompiled Binaries for Windows
4. find sqlite-tools-win32-x86-3360000.zip (1.82 MiB)
5. Download sqlite-tools-win32-x86-3360000.zip
6. Unzip it.
7. Open the new unziped folder.
8. Copy/cut the file: "sqlite3.exe"
9. Paste this file in your desktop.
10. Go to your C:\ folder.
11. Create a new folder and name it "sqlite3"
12. Open new folder "sqlite3".
13. Go to your desktop and cut the file "sqlite3.exe", then go to your new folder located in C:\sqlite3\, and paste it there.
14. You can close everything.
15. Open "Edit system environment variables".
16. Add sqlite to your Path....
17. Open command prompt.
18. Type sqlite3 --version Voila!, it's ready to use!
Object: Cursors are used to execute statements to communicate with the SQL database.
Iportant methods:
execute(): This method accepts a MySQL query as a parameter and executes the given query.
fetchall(): This method retrieves all the rows in the result set of a query and returns them as list of tuples. (If we execute this after
retrieving few rows it returns the remaining ones)
In [2]: #If you don't have an SQL database created in SQLite, then a new one will be created
connection_1 = sqlite3.connect('transactions_world.db')
#Create cursor
cursor_1 = connection_1.cursor()
print('Success!')
Success!
Success!
print('Success!')
Success!
cid(Column ID)
notnull Not null constraint prevents a value from being null, that is, it forces to have a «real» value.
dflt_value The DEFAULT keyword provides a default value to a column when the SQL Server INSERT INTO statement does not provide a
specific value. The default value can be a literal value, an expression, or a SQL Function, such as GETDATE().
pk (Primary key) A primary key is a column or a group of columns that uniquely identifies each row in a table. You create a primary key for a
table by using the PRIMARY KEY constraint.
In [6]: cursor_1.execute("""
SELECT
typeof( 42 ) i,
typeof( 99.9 ) r,
typeof('string' ) t,
typeof( null ) n,
typeof( x'deadbeef') b
""")
cursor_1.fetchall()
In [7]: cursor_1.execute("""
INSERT INTO purchases(purchases_id, store_id, total_cost)
VALUES(1, 21, 2247)
""")
print('Success!')
Success!
In [8]: cursor_1.execute("""
INSERT INTO purchases(store_id, total_cost)
VALUES(20, 457),
(12, 885),
(20, 454),
(21, 104)
""")
cursor_1.fetchall()
In [9]: cursor_1.execute("""
INSERT INTO purchases
VALUES(6, 20, 457),
(7, 12, 885),
(8, 20, 454),
(9, 21, 104)
""")
cursor_1.fetchall()
In [10]: cursor_1.execute("""
INSERT INTO purchases(store_id, total_cost)
VALUES(10, 157),
(11, 725),
(12, 16)
""")
cursor_1.fetchall()
In [11]: cursor_1.execute("""
INSERT INTO purchases(purchases_id, total_cost)
VALUES(13, 140),
(14, 256),
(15, 108)
""")
cursor_1.fetchall()
Out[12]:
purchases_id store_id total_cost
0 1 21.0 2247.0
1 2 20.0 457.0
2 3 12.0 885.0
3 4 20.0 454.0
4 5 21.0 104.0
5 6 20.0 457.0
6 7 12.0 885.0
7 8 20.0 454.0
8 9 21.0 104.0
9 10 10.0 157.0
10 11 11.0 725.0
11 12 12.0 16.0
12 13 NaN 140.0
13 14 NaN 256.0
14 15 NaN 108.0
Out[26]: <AxesSubplot:xlabel='purchases_id'>
PostgreSQL
psycopg is the new implementation of the most used, reliable and feature-rich PostgreSQL adapter for Python.
Postgres was born at the University of California at Berkeley. The implementation of POSTGRES began in 1986. Wow!
The object-relational database management system now known as PostgreSQL is derived from the POSTGRES package written at the
University of California at Berkeley. With over two decades of development behind it, PostgreSQL is now the most advanced open-source
database available anywhere.
PostgreSQL is an open-source descendant of this original Berkeley code. It supports a large part of the SQL standard and offers many
modern features:
complex queries
foreign keys
triggers
updatable views
transactional integrity
multiversion concurrency control
Also, PostgreSQL can be extended by the user in many ways, for example by adding new:
data types
functions
operators
aggregate functions
index methods
procedural languages
And because of the liberal license, PostgreSQL can be used, modified, and distributed by anyone free of charge for any purpose, be it
private, commercial, or academic.
myconnection = psycopg2.connect(user="postgres",
password="karens123",
host="localhost",
port="5432",
database="postgres")
Success! Yay!
You are connected to -> ('PostgreSQL 11.9, compiled by Visual C++ build 1914, 64-bit',)
rows = cursor.fetchall()
for r in rows:
print (f"id {r[0]} name {r[1]}")
id A name 121
id A name 33
id A name 414
id A name 2
id A name 45
Result [('A', 121), ('A', 33), ('A', 414), ('A', 2), ('A', 45), ('A', 12), ('A', 393), ('A', 44),
('A', 2), ('A', 45), ('A', 12), ('A', 38), ('A', 44), ('A', 2), ('A', 45), ('A', 12), ('A', 343),
('A', 54), ('A', 2), ('A', 45), ('A', 12), ('A', 3), ('A', 44), ('A', 27), ('A', 456)]
So far, so good
Connection is closed