python mysql
python mysql
Installation:
conn=mysql.connector.connect(host='localhost',user='root',password='
mysql123',db='employee')
cur=conn.cursor()
cur.execute('show databases')
output=cur.fetchall()
print(output)
conn.close()
Output:
[('information_schema',), ('employee',), ('mysql',),
('performance_schema',), ('sakila',), ('sys',), ('world',)]
Creating Database
After connecting to the MySQL, For this, we will first create a cursor()
object and will then pass the SQL command as a string to the execute()
method. The SQL command to create a database is –
Creating Tables
For creating tables we will follow the similar approach of writing the SQL
commands as strings and then passing it to the execute() method of the
cursor object. SQL command for creating a table is –
CREATE TABLE
(
column_name_1 column_Data_type,
column_name_2 column_Data_type,
:
:
column_name_n column_Data_type
);
Output:
Syntax:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="sameena",
password="mysql123",
database="employee"
mycursor = mydb.cursor()
mycursor.execute(sql, val)
mydb.commit()
output=cur.fetchall()
print(output)
conn.close()
Output:
1 record inserted.
[('John', 'Highway 21')]
Program:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="selvam",
password="mysql123",
database="employee"
mycursor = mydb.cursor()
mycursor.executemany(sql, val)
mydb.commit()
output=mycursor.fetchall()
print(output)
conn.close()
Output:
Fetching Data
We can use the select query on the MySQL tables in the following ways –
In order to select particular attribute columns from a table, we write
the attribute names.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="sameena",
password="mysql123",
database="employee"
)
mycursor = mydb.cursor()
conn.close()
Output:
Where Clause
Where clause is used in MySQL database to filter the data as per the
condition required. You can fetch, delete or update a particular set of data in
MySQL database by using where clause.
Syntax:
SELECT column1, column2, …. columnN FROM [TABLE NAME] WHERE
[CONDITION];
Example: Where clause in MySQL using Python
Program:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="sameena",
password="mysql123",
database="employee"
)
mycursor = mydb.cursor()
conn.close()
Output:
Order By Clause
Program:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="sameena",
password="mysql123",
database="employee"
)
mycursor = mydb.cursor()
conn.close()
Output:
Program 2 :
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="sameena",
password="mysql123",
database="employee"
mycursor = mydb.cursor()
output=mycursor.fetchall()
print(output)
conn.close()
Output:
Limit Clause
Syntax:
SELECT * FROM tablename LIMIT limit;
SELECT * FROM tablename LIMIT limit OFFSET offset;
Program:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="sameena",
password="mysql123",
database="employee"
mycursor = mydb.cursor()
output=mycursor.fetchall()
print(output)
print(output)
conn.close()
Output:
Update Data:
Syntax:
UPDATE tablename
SET ="new value"
WHERE ="old value";
Program:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="sameena",
password="mysql123",
database="employee"
mycursor = mydb.cursor()
output=mycursor.fetchall()
print(output)
output=mycursor.fetchall()
print(output)
conn.close()
Output:
Program:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="sameena",
password="mysql123",
database="employee"
)
mycursor = mydb.cursor()
conn.close()
Output:
[('John', 'Highway 21'), ('ravi', 'Highway 21'), ('senthil', 'narapally
22'), ('ravi', 'Highway 21'), ('senthil', 'narapally 22'), ('ravi',
'Highway 21'), ('senthil', 'narapally 22')]
[('ravi', 'Highway 21'), ('senthil', 'narapally 22'), ('ravi', 'Highway
21'), ('senthil', 'narapally 22'), ('ravi', 'Highway 21'), ('senthil',
'narapally 22')]
Drop Tables
Drop command affects the structure of the table and not data. It is
used to delete an already existing table. For cases where you are not sure if
the table to be dropped exists or not DROP TABLE IF EXISTS command is
used. Both cases will be dealt with in the following examples.
Syntax:
DROP TABLE tablename;
DROP TABLE IF EXISTS tablename;
Program:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="sameena",
password="mysql123",
database="employee"
mycursor = mydb.cursor()
mycursor.execute("show tables")
output=mycursor.fetchall()
print(output)
mycursor.execute("show tables")
output=mycursor.fetchall()
print(output)
conn.close()
Output:
[('customer',), ('emp',)]
[('emp',)]