SQL Cheat Sheet Accessing Databases using Python
SQL Cheat Sheet Accessing Databases using Python
Copied!
1. 1
The fetchmany() method 2. 2
retrieves the subsequent 3. 3
group of rows from the 4. 4
result set of a query rather 5. 5
than just a single row. To 1. statement = '''SELECT * FROM INSTRUCTOR'''
fetchmany() cursor_obj.fetchmany()
fetch a few rows from the 2. cursor_obj.execute(statement)
table, use 3. output_many = cursor_obj.fetchmany(2)
fetchmany(numberofrows) 4. for row_many in output_many:
and mention how many 5. print(row_many)
rows you want to fetch. Copied!
read_sql_query() is a
function provided by the
Pandas library in Python,
and it is not specific to 1. 1
MySQL. It is a generic
read_sql_query() read_sql_query() function used for 1. df = pd.read_sql_query("select * from instructor;", conn)
executing SQL queries on
Copied!
various database systems,
including MySQL, and
retrieving the results as a
Pandas DataFrame.
It provides a tuple
indicating the shape of a 1. 1
DataFrame or Series, 1. df.shape
shape dataframe.shape
represented as (number of
rows, number of Copied!
columns).
close() con.close() con.close() is a method 1. 1
used to close the 1. con.close()
connection to a MySQL
database. When called, it Copied!
terminates the connection,
releasing any associated
resources and ensuring the
connection is no longer
active. This is important
about:blank 1/3
7/24/24, 5:18 PM about:blank
for managing database
connections efficiently
and preventing resource
leaks in your MySQL
database interactions.
The CREATE TABLE
statement is used to define 1. 1
and create a new table 2. 2
within a database. It 3. 3
4. 4
specifies the table's name, 5. 5
the structure of its 6. 6
CREATE TABLE table_name ( columns (including data
CREATE column1 datatype 1. CREATE TABLE INTERNATIONAL_STUDENT_TEST_SCORES ( <br>
types and constraints), and
TABLE constraints, column2 2. country VARCHAR(50), <br>
datatype constraints, ... ); any additional properties 3. first_name VARCHAR(50), <br>
such as indexes. This 4. last_name VARCHAR(50), <br>
statement essentially sets 5. test_score INT
up the blueprint for 6. );
organizing and storing
data in a structured format Copied!
within the database.
seaborn.barplot() is a
function in the Seaborn
Python data visualization
library used to create a bar 1. 1
plot, also known as a bar 2. 2
seaborn.barplot(x="x- chart. It is particularly
barplot() axis_variable", y="y- 1. import seaborn
axis_variable", data=data) used to display the 2. seaborn.barplot(x='Test_Score',y='Frequency', data=dataframe)
relationship between a
categorical variable and a Copied!
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 1. 1
a Comma-Separated 2. 2
df = Values (CSV) file and 1. import pandas
read_csv() pd.read_csv('file_path.csv') loading it into a Pandas 2. df = pandas.read_csv('https://data.cityofchicago.org/resource/jcxq-k9xf.csv
DataFrame. It's a common
method for working with Copied!
tabular data stored in CSV
format
df.to_sql() is a method
in Pandas, a Python data 1. 1
manipulation library used 2. 2
to write the contents of a 3. 3
df.to_sql('table_name', DataFrame to a SQL 1. import pandas
to_sql() index=False) database. It allows to take 2. df = pandas.read_csv('https://data.cityofchicago.org/resource/jcxq-k9xf.csv
data from a DataFrame 3. df.to_sql("chicago_socioeconomic_data", con, if_exists='replace', index=Fal
and store it structurally
within a SQL database Copied!
table.
read_sql() is a function
provided by the Pandas
library in Python for 1. 1
executing SQL queries 2. 2
and retrieving the results
df = pd.read_sql(sql_query, 1. selectQuery = "select * from INSTRUCTOR"
read_sql() conn)
into a DataFrame from an
SQL database. It's a 2. df = pandas.read_sql(selectQuery, conn)
convenient way to Copied!
integrate SQL database
interactions into your data
analysis workflows.
Db2
about:blank 2/3
7/24/24, 5:18 PM about:blank
4. print ("DB_NAME: ", server.DB_NAME)
Copied!
con.close() is a method
used to close the
connection to a db2
database. When called, it
terminates the connection,
releasing any associated 1. 1
resources and ensuring the 1. con.close()
close() con.close()
connection is no longer
active. This is important Copied!
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 1. 1
library, which is used to 2. 2
sql_statement = "SQL statement goes execute an SQL statement 3. 3
here" immediately without the
exec_immediate() stmt = ibm_db.exec_immediate(conn, 1. # Lets first drop the table INSTRUCTOR in case it exists from a p
sql_statement) need to prepare or bind it. 2. dropQuery = "drop table INSTRUCTOR"
It's commonly used for 3. dropStmt = ibm_db.exec_immediate(conn, dropQuery)
executing SQL statements
that don't require input Copied!
parameters or don't need
to be prepared in advance.
Author(s)
Abhishek Gagneja
D.M Naidu
about:blank 3/3