11/26/24, 3:17 AM about:blank
SQL Cheat Sheet: Accessing Databases using Python
SQLite
Topic Syntax Description Example
Create a new database and
open a database
connection to allow
sqlite3 to work with it.
Call sqlite3.connect() import sqlite3
connect() sqlite3.connect() to create a connection to con = sqlite3.conn
the database
INSTRUCTOR.db in the
current working directory,
implicitly creating it if it
does not exist.
To execute SQL
statements and fetch
results from SQL queries, cursor_obj = con.c
cursor() con.cursor()
use a database cursor. Call
con.cursor() to create the
Cursor.
The execute method in
Python's SQLite library
allows to perform SQL
commands, including
retrieving data from a
table using a query like
"Select * from cursor_obj.execute
execute() cursor_obj.execute()
table_name." When you
execute this command, the
result is obtained as a
collection of table data
stored in an object,
typically in the form of a
list of lists.
The fetchall() method statement = '''SEL
in Python retrieves all the cursor_obj.execute
output_all = curso
fetchall() cursor_obj.fetchall() rows from the result set of for row_all in out
a query and presents them print(row_all)
as a list of tuples.
The fetchmany() method
retrieves the subsequent
group of rows from the
statement = '''SEL
result set of a query rather cursor_obj.execute
than just a single row. To output_many = curs
fetchmany() cursor_obj.fetchmany()
fetch a few rows from the for row_many in out
table, use print(row_many
fetchmany(numberofrows)
and mention how many
rows you want to fetch.
about:blank 1/4
11/26/24, 3:17 AM about:blank
read_sql_query() is a
function provided by the
Pandas library in Python,
and it is not specific to
MySQL. It is a generic
df = pd.read_sql_q
read_sql_query() read_sql_query() function used for
executing SQL queries on
various database systems,
including MySQL, and
retrieving the results as a
Pandas DataFrame.
It provides a tuple
indicating the shape of a
DataFrame or Series, df.shape
shape dataframe.shape
represented as (number of
rows, number of
columns).
con.close() is a method
used to close the
connection to a MySQL
database. When called, it
terminates the connection,
releasing any associated
resources and ensuring the con.close()
close() con.close()
connection is no longer
active. This is important
for managing database
connections efficiently
and preventing resource
leaks in your MySQL
database interactions.
The CREATE TABLE
statement is used to define
and create a new table
within a database. It
specifies the table's name,
CREATE TABLE INTER
the structure of its country VARCHAR(50
CREATE TABLE table_name ( columns (including data
CREATE first_name VARCHAR
column1 datatype
types and constraints), and last_name VARCHAR(
TABLE constraints, column2
test_score INT
datatype constraints, ... ); any additional properties
such as indexes. This );
statement essentially sets
up the blueprint for
organizing and storing
data in a structured format
within the database.
barplot() seaborn.barplot(x="x- seaborn.barplot() is a import seaborn
axis_variable", y="y- function in the Seaborn seaborn.barplot(x=
axis_variable", data=data)
Python data visualization
library used to create a bar
plot, also known as a bar
chart. It is particularly
used to display the
about:blank 2/4
11/26/24, 3:17 AM about:blank
relationship between a
categorical variable and a
numeric variable by
showing the average value
for each category.
read_csv() is a function
in Python's Pandas library
used for reading data from
a Comma-Separated
df = Values (CSV) file and import pandas
read_csv() df = pandas.read_c
pd.read_csv('file_path.csv') loading it into a Pandas
DataFrame. It's a common
method for working with
tabular data stored in CSV
format
df.to_sql() is a method
in Pandas, a Python data
manipulation library used
to write the contents of a import pandas
df.to_sql('table_name', DataFrame to a SQL df = pandas.read_c
to_sql() index=False) database. It allows to take df.to_sql("chicago_
data from a DataFrame
and store it structurally
within a SQL database
table.
read_sql() is a function
provided by the Pandas
library in Python for
executing SQL queries
and retrieving the results selectQuery = "sel
df = pd.read_sql(sql_query,
read_sql() conn)
into a DataFrame from an df = pandas.read_s
SQL database. It's a
convenient way to
integrate SQL database
interactions into your data
analysis workflows.
Db2
Topic Syntax Description Example
ibm_db.connect() is a
Python function provided
by the ibm_db library,
which is used for import ib
conn = establishing a connection
ibm_db.connect('DATABASE=dbname; conn = ib
connect() HOST=hostname;PORT=port;UID=username;
to an IBM Db2 or IBM HOST=exam
PWD=password;', '', '') Db2 Warehouse database. PWD=mypas
It's commonly used in
applications that need to
interact with IBM Db2
databases from Python.
about:blank 3/4
11/26/24, 3:17 AM about:blank
ibm_db.server_info(conn)
is a Python function
server =
provided by the ibm_db print ("D
server_info() ibm_db.server_info() library, which is used to print ("D
retrieve information about print ("D
the IBM Db2 server to
which you are connected.
con.close() is a method
used to close the
connection to a db2
database. When called, it
terminates the connection,
releasing any associated
resources and ensuring the con.close
close() con.close()
connection is no longer
active. This is important
for managing database
connections efficiently
and preventing resource
leaks in your db2 database
interactions.
ibm_db.exec_immediate()
is a Python function
provided by the ibm_db
library, which is used to
sql_statement = "SQL statement goes execute an SQL statement
here" # Lets fi
immediately without the dropQuery
exec_immediate() stmt = ibm_db.exec_immediate(conn,
sql_statement) need to prepare or bind it. dropStmt
It's commonly used for
executing SQL statements
that don't require input
parameters or don't need
to be prepared in advance.
Author(s)
Abhishek Gagneja
D.M Naidu
about:blank 4/4