SQL Cheat Sheet - Accessing Databases Using Python
SQL Cheat Sheet - Accessing Databases Using Python
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.
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.
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
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