Database
Database
All these database can be accessed with their respective Python Application Program Interfaces
(APIs),
The database is a collection of prearranged information that can effortlessly be used, managed, revised. The
key features of a DB API are,
The execute() methods run the SQL query and return the result.
executemany()
For all the listed parameters in the sequence, the given SQL statement is executed.
import mysql.connector
#Create the connection object
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google")
#printing the connection object
print(myconn)
Here, we must notice that we can specify the database name in the connect() method if we want to connect to a
specific database.
Example
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google", database = "mydb")
Creating a cursor object
The cursor object can be defined as an abstraction specified in the Python DB-API 2.0. It facilitates us to have
multiple separate working environments through the same connection to the database. We can create the cursor
object by calling the 'cursor' function of the connection object. The cursor object is an important aspect of executing
queries to the databases.
import mysql.connector
#Create the connection object
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google", database = "mydb")
#printing the connection object
print(myconn)
#creating the cursor object
cur = myconn.cursor()
print(cur)
Creating MySQL Database
To create a database, we will use CREATE DATABASE database_name statement and we will
execute this statement by creating an instance of the ‘cursor’ class.
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "yourusername",
password = "your_password"
)
cursor = mydb.cursor()
So make sure that the new database that you are creating does not have the same name as the
database already you created or exists previously.
Now to check the databases that you created, use “SHOW DATABASES” – SQL statement i.e.
cursor.execute(“SHOW DATABASES”)
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
password = "1234"
)
cursor = mydb.cursor()
cursor.execute("SHOW DATABASE")
for x in cursor:
print(x)
Creating Tables
Now to create tables in a database, first, we have to select a database and for that,
we will pass database = “NameofDatabase” as your fourth parameter in connect() function.
Since we have created a database with the name SJU above, so we will use that and create our tables. We
will use CREATE TABLE gfg (variableName1 datatype, variableName2 datatype) statement to create our
table with the name ‘gfg’.
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "yourusername",
password = "your_password",
database = “sSJU"
)
cursor = mydb.cursor()
So make sure that the new table that you are creating does not have the same name as the table
already you created or exists previously.
Now to check tables that you created, use “SHOW TABLES” – SQL statement i.e.
cursor.execute(“SHOW TABLES”)
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root
password = "1234",
database = “SJU"
)
cursor = mydb.cursor()
cursor.execute("SHOW TABLES")
for x in cursor:
print(x)
•mysql.connector allows Python programs to access MySQL databases.
•connect() method of the MySQL Connector class with the arguments will connect to
MySQL and would return a MySQLConnection object if the connection is established
successfully.
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "username", Note:
password = "password", •The cursor() is used in order to iterate through the rows.
database = "database_name" •Without the command mydb.commit() the changes will not be
) saved.
mycursor = mydb.cursor()
mycursor.execute(sql, val)
mydb.commit()
mydb.close()
To insert multiple values at once, executemany() method is used. This method iterates through the sequence of
parameters, passing the current parameter to the execute method.
mycursor.executemany(sql, val)
mydb.commit()
mydb.close()
Update Clause
The update is used to change the existing values in a database. By using update a specific value can be corrected or
updated. It only affects the data and not the structure of the table.
import mysql.connector
mydb = mysql.connector.connect(
host ='localhost',
database ='College',
user ='root’,
)
cs = mydb.cursor()
cs.execute(statement)
mydb.commit()