SQL Cheat Sheet: Accessing Databases using Python
SQL Cheat Sheet: Accessing Databases using Python
SQLite
Topic | Syntax | Description | Example |
---|---|---|---|
connect() | sqlite3.connect() | Create a new database and open a database connection to allow sqlite3 to work with it. Call sqlite3.connect() to create a connection to the database INSTRUCTOR.db in the current working directory, implicitly creating it if it does not exist. |
|
cursor() | con.cursor() | To execute SQL statements and fetch results from SQL queries, use a database cursor. Call con.cursor() to create the Cursor. |
|
execute() | cursor_obj.execute() | 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 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. |
|
fetchall() | cursor_obj.fetchall() | The |
|
fetchmany() | cursor_obj.fetchmany() | The |
|
read_sql_query() | read_sql_query() |
|
|
shape | dataframe.shape | It provides a tuple indicating the shape of a DataFrame or Series, represented as (number of rows, number of columns). |
|
close() | con.close() | 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 connection is no longer active. This is important for managing database connections efficiently and preventing resource leaks in your MySQL database interactions. |
|
CREATE TABLE | CREATE TABLE table_name ( column1 datatype constraints, column2 datatype constraints, ... ); | The CREATE TABLE statement is used to define and create a new table within a database. It specifies the table's name, the structure of its columns (including data types and constraints), and 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-axis_variable", y="y-axis_variable", data=data) | seaborn.barplot() is a function in the Seaborn Python data visualization library used to create a bar plot, also known as a bar chart. It is particularly used to display the relationship between a categorical variable and a numeric variable by showing the average value for each category. |
|
read_csv() | df = pd.read_csv('file_path.csv') | read_csv() is a function in Python's Pandas library used for reading data from a Comma-Separated Values (CSV) file and loading it into a Pandas DataFrame. It's a common method for working with tabular data stored in CSV format |
|
to_sql() | df.to_sql('table_name', index=False) | df.to_sql() is a method in Pandas, a Python data manipulation library used to write the contents of a DataFrame to a SQL database. It allows to take data from a DataFrame and store it structurally within a SQL database table. |
|
read_sql() | df = pd.read_sql(sql_query, conn) | read_sql() is a function provided by the Pandas library in Python for executing SQL queries and retrieving the results into a DataFrame from an SQL database. It's a convenient way to integrate SQL database interactions into your data analysis workflows. |
|
Db2
Topic | Syntax | Description | Example |
---|---|---|---|
connect() | conn = ibm_db.connect('DATABASE=dbname; HOST=hostname;PORT=port;UID=username; PWD=password;', '', '') | ibm_db.connect() is a Python function provided by the ibm_db library, which is used for establishing a connection to an IBM Db2 or IBM Db2 Warehouse database. It's commonly used in applications that need to interact with IBM Db2 databases from Python. |
|
server_info() | ibm_db.server_info() | ibm_db.server_info(conn) is a Python function provided by the ibm_db library, which is used to retrieve information about the IBM Db2 server to which you are connected. |
|
close() | con.close() | 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 connection is no longer active. This is important for managing database connections efficiently and preventing resource leaks in your db2 database interactions. |
|
exec_immediate() | sql_statement = "SQL statement goes here" | ibm_db.exec_immediate() is a Python function provided by the ibm_db library, which is used to execute an SQL statement immediately without the need to prepare or bind it. It's commonly used for executing SQL statements that don't require input parameters or don't need to be prepared in advance. |
|
Comments
Post a Comment