SQL Sesion O - O

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

SQL

Structured Query Language


SQL is a database management language for relational databases.

Most commonly used:

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

Open source and available for free. (Yay!!)


Lightweight database and easy to implement
Rapid data storage and retrieval.
Simple to use: Easy to manage using free tools.
Serverless database engine.
Good community support available online for troubleshooting
Supported by almost all popular programming languages.
Supports all popular operating systems.

Cons

Scalability is limited to small applications only.


Multi-user not supported.
Data encryption is missing.
Limited in size of data tables.

Which companies use sqlite?

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!

Installing SQLite in Python:

Using Pip: pip install pysqlite


In Anaconda: python -m pip install --upgrade pip
In Ubuntu:
sudo apt update
sudo apt install sqlite3
sqlite3 --version
sudo apt install sqlitebrowser

In [1]: import sqlite3


import pandas as pd

Define connection and cursor

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)

close(): This method is used to close the current cursor object.**

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!

In [3]: #I'll run this to start the example again

command1 = "DROP TABLE purchases"


cursor_1.execute(command1)
print('Success!')

Success!

Create "purchases" table

In [4]: command1 = """


CREATE TABLE IF NOT EXISTS
purchases
(
purchases_id INTEGER PRIMARY KEY,
store_id INTEGER,
total_cost FLOAT
)
"""
cursor_1.execute(command1)

print('Success!')

Success!

Getting the structure of our "purchases" table

In [5]: command1 = """


PRAGMA table_info('purchases')
"""
cursor_1.execute(command1)

print(['cid', 'name', 'type', 'notnull', 'dflt_value', 'pk'])


cursor_1.fetchall()

['cid', 'name', 'type', 'notnull', 'dflt_value', 'pk']

Out[5]: [(0, 'purchases_id', 'INTEGER', 0, None, 1),


(1, 'store_id', 'INTEGER', 0, None, 0),
(2, 'total_cost', 'FLOAT', 0, None, 0)]

What are the values we get from Table_info?

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.

SQLite data types

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()

Out[6]: [('integer', 'real', 'text', 'null', 'blob')]

Insert new data into purchases table

In [7]: cursor_1.execute("""
INSERT INTO purchases(purchases_id, store_id, total_cost)
VALUES(1, 21, 2247)
""")

print('Success!')

Success!

Insert value without PRIMARY KEY column:

In [8]: cursor_1.execute("""
INSERT INTO purchases(store_id, total_cost)
VALUES(20, 457),
(12, 885),
(20, 454),
(21, 104)
""")

#View all the elements in my table


cursor_1.execute("""
SELECT * FROM purchases
""")

cursor_1.fetchall()

Out[8]: [(1, 21, 2247.0),


(2, 20, 457.0),
(3, 12, 885.0),
(4, 20, 454.0),
(5, 21, 104.0)]

Insert value without mention column list:

In [9]: cursor_1.execute("""
INSERT INTO purchases
VALUES(6, 20, 457),
(7, 12, 885),
(8, 20, 454),
(9, 21, 104)
""")

#View all the elements in my table


cursor_1.execute("""
SELECT * FROM purchases
""")

cursor_1.fetchall()

Out[9]: [(1, 21, 2247.0),


(2, 20, 457.0),
(3, 12, 885.0),
(4, 20, 454.0),
(5, 21, 104.0),
(6, 20, 457.0),
(7, 12, 885.0),
(8, 20, 454.0),
(9, 21, 104.0)]

Insert values for specific columns

In [10]: cursor_1.execute("""
INSERT INTO purchases(store_id, total_cost)
VALUES(10, 157),
(11, 725),
(12, 16)
""")

#View all the elements in my table


cursor_1.execute("""
SELECT * FROM purchases
""")

cursor_1.fetchall()

Out[10]: [(1, 21, 2247.0),


(2, 20, 457.0),
(3, 12, 885.0),
(4, 20, 454.0),
(5, 21, 104.0),
(6, 20, 457.0),
(7, 12, 885.0),
(8, 20, 454.0),
(9, 21, 104.0),
(10, 10, 157.0),
(11, 11, 725.0),
(12, 12, 16.0)]

Insert values for specific columns, getting 'NULLS'

In [11]: cursor_1.execute("""
INSERT INTO purchases(purchases_id, total_cost)
VALUES(13, 140),
(14, 256),
(15, 108)
""")

#View all the elements in my table


cursor_1.execute("""
SELECT * FROM purchases
""")

cursor_1.fetchall()

Out[11]: [(1, 21, 2247.0),


(2, 20, 457.0),
(3, 12, 885.0),
(4, 20, 454.0),
(5, 21, 104.0),
(6, 20, 457.0),
(7, 12, 885.0),
(8, 20, 454.0),
(9, 21, 104.0),
(10, 10, 157.0),
(11, 11, 725.0),
(12, 12, 16.0),
(13, None, 140.0),
(14, None, 256.0),
(15, None, 108.0)]

In [12]: # create the dataframe from a query


df = pd.read_sql_query("SELECT * FROM purchases", connection_1)
df

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

In [26]: df.plot.bar(x= 'purchases_id', y= 'total_cost', color = 'orange', rot=0)

Out[26]: <AxesSubplot:xlabel='purchases_id'>

In [14]: # Run just in case you want to finish the connection


# connection.close()

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.

In [15]: import pandas as pd

# import sys to get more detailed Python exception info


import sys

# import the connect library for psycopg2


import psycopg2

# import the error handling libraries for psycopg2


from psycopg2 import OperationalError, errorcodes, errors

In [16]: #Establishing the connection.

myconnection = psycopg2.connect(user="postgres",
password="karens123",
host="localhost",
port="5432",
database="postgres")

#If you had success, you'll see this printed!


print('Success! Yay!')

Success! Yay!

In [17]: # Create a cursor to perform database operations


cursor = myconnection.cursor()

In [18]: # Print PostgreSQL details


print("**PostgreSQL server information**")
print(myconnection.get_dsn_parameters(), "\n")

**PostgreSQL server information**


{'user': 'postgres', 'channel_binding': 'prefer', 'dbname': 'postgres', 'host': 'localhost', 'port':
'5432', 'tty': '', 'options': '', 'sslmode': 'prefer', 'sslcompression': '0', 'ssl_min_protocol_versi
on': 'TLSv1.2', 'gssencmode': 'disable', 'krbsrvname': 'postgres', 'target_session_attrs': 'any'}

In [19]: # Executing a SQL query


cursor.execute("SELECT version();")
# Fetch result
record = cursor.fetchone()
print("You are connected to ->", record)

You are connected to -> ('PostgreSQL 11.9, compiled by Visual C++ build 1914, 64-bit',)

In [20]: cursor = myconnection.cursor()

cursor.execute("SELECT * FROM public.clones_tb LIMIT 5;")

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

In [21]: #Try any simple query


cursor.execute('''
SELECT * FROM public.clones_tb
;''')
print("Result ", cursor.fetchall())
print("So far, so good")

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

Close the connection

In [23]: #Close the connection


cursor.close()
myconnection.close()
print("Connection is closed")

Connection is closed

You might also like