unit 5 python
unit 5 python
DATABASE
Python MySQL
Python can be used in database applications. One of the most popular databases is MySQL.
Create Connection
Start by creating a connection to the database. Use the username and password from your
MySQL database:
Example:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password=" "
)
print(mydb)
Creating a Database
To create a database in MySQL, use the "CREATE DATABASE" statement:
Example:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password=" "
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
Or you can try to access the database when making the connection:
Example
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password=" "
database=”mydatabase”
)
Creating a Table
To create a table in MySQL, use the "CREATE TABLE" statement.
Make sure you define the name of the database when you create the connection
Example
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE students(rno INT(3),name VARCHAR(255), class
VARCHAR(10), college VARCHAR(50))")
mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
print(x)
Primary Key
When creating a table, you should also create a column with a unique key for each record.
This can be done by defining a PRIMARY KEY.
We use the statement "INT AUTO_INCREMENT PRIMARY KEY" which will insert a unique
number for each record. Starting at 1, and increased by one for each record.
Example
Create primary key when creating the table:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "INSERT INTO students (name, class, college) VALUES (%s, %s, %s)"
val = ("Kevin sunny", "bca6","christ")
mycursor.execute(sql, val)
mydb.commit()
mydb.commit()
val = [("meet","bca4","marwadi"),("richa","bca2","christ"),("harsh","bca2","atmiya")]
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
mydb.commit()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Selecting Columns
To select only some of the columns in a table, use the "SELECT" statement followed by the
column name(s).
Example
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT rno,name FROM students")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
myresult = mycursor.fetchone()
print(myresult)
myresult = mycursor.fetchall()
mydb.close()
myresult = mycursor.fetchall()
mydb.close()
myresult = mycursor.fetchall()
print("Name \t College")
for x in myresult:
print("%s \t %s"%(x[0],x[1]))
mydb.close()
myresult = mycursor.fetchall()
mydb.close()
ORDER BY DESC
mycursor.execute("SELECT * FROM students order by name DESC ")
Update Table
The UPDATE-SET statement is used to update any record inside the table.
Notice the WHERE clause in the UPDATE syntax: The WHERE clause specifies which
record or records that should be updated. If you omit the WHERE clause, all records will be
updated!
Example
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("UPDATE students SET class='bca6' WHERE rno=1")
myresult = mycursor.fetchall()
mydb.close()
Delete Record
You can delete records from an existing table by using the "DELETE FROM" statement. Here,
we must impose a condition using WHERE clause otherwise all the records from the table will
be removed.
Example
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("DELETE from students WHERE rno=1")
print(mycursor.rowcount, "record(s) deleted")
myresult = mycursor.fetchall()
mydb.commit()
mydb.close()
Important!: Notice the statement: mydb.commit(). It is required to make the changes, otherwise
no changes are made to the table.
Drop Table
Delete a Table
You can delete an existing table by using the "DROP TABLE" statement.
Example
Create a temporary table
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
print(x)
MySQL Limit
You can limit the number of records returned from the query, by using the "LIMIT" statement.
Example
Select the 3 first records in the "students" table.
mycursor.execute("SELECT * FROM customers LIMIT 3")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
for x in myresult:
print(x)
All the operations that modify the records of the database do not take place until the commit() is
called.
mydb.rollback()
mydb.close()