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