Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 8

Open mysql software

1. Create a database with name covid


Mysql>create database covid;

2. Open database
Mysql>use covid;
3. To create table by using field names
Mysql> create table patient (pno int, name
varchar(15), age int, address varchar(15),
contactno int);
4. Insert four records in the patient table
Mysql>insert into patient values(1,’akash’,
…………);
5. Open a new file in python
USING SELECT COMMAND IN PYTHON-MYSQL CONNECTIVITY

Patient- Pno, name, age,address,contactno

import mysql.connector as sqltor


mydb=sqltor.connect(host="localhost",use
r="root",password="arshad123",database
="arshad")
mycursor=mydb.cursor()
mycursor.execute("sele
ct * from ali")
result=mycursor.fetchall()
for i in result:
print(i)
WE CAN USE FETCHONE() ALSO IN PLACE OF FETCHALL() IF WE WANT ONLY ONE RECORD
output:

>>>

== RESTART: C:\Users\gateway\AppData\Local\Programs\Python\Python38\sqltor.py ==

(1, 'arshad', 'singer_cricketer')

(2, 'irshad', 'singer_cricketer')

show tables command


import mysql.connector as sqltor

mydb=sqltor.connect(host="localhost",user="root",password="arshad123",database="arshad")

mycursor=mydb.cursor()

mycursor.execute("show tables")

for i in mycursor:

print(i)

output:
== RESTART: C:\Users\gateway\AppData\Local\Programs\Python\Python38\sqltor.py ==

('ali',)

insert command

import mysql.connector as sqltor


mydb=sqltor.connect(host="localhost",user="ro
ot",password="arshad123",database="arshad")
mycursor=mydb.cursor()
mycursor.execute("insert into ali
values(1,'Rahul','artist')")
mydb.commit()
output:

+------+--------+------------------+

| No | Name | qualities |

+------+--------+------------------+

| 1 | arshad | singer_cricketer |

| 2 | irshad | singer_cricketer |

| 1 | Rahul | artist |

+------+--------+------------------+

create table commannd

import mysql.connector as sqltor

mydb=sqltor.connect(host="localhost",user="root",password="arshad123",database="arshad")

mycursor=mydb.cursor()

mycursor.execute("create table stu(name varchar(20),percentage int)")

mydb.commit()
output:

mysql> desc stu;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| name | varchar(20) | YES | | NULL | |

| percentage | int(11) | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

command using where clause


import mysql.connector as sqltor

mydb=sqltor.connect(host="localhost",user="root",password="arshad123",database="arshad")

mycursor=mydb.cursor()

mycursor.execute("select name from ali where no=1")

for i in mycursor:

print(i)

output:

== RESTART: C:\Users\gateway\AppData\Local\Programs\Python\Python38\sqltor.py ==
('arshad',)

('Rahul',)

update command
import mysql.connector as sqltor

mydb=sqltor.connect(host="localhost",user="root",password="arshad123",database="arshad")

mycursor=mydb.cursor()

mycursor.execute("update ali set name='steve' where no=1")

mydb.commit()

output:

+------+--------+------------------+

| No | Name | qualities |

+------+--------+------------------+

| 1 | steve | singer_cricketer |

| 2 | irshad | singer_cricketer |

| 1 | steve | artist |

+------+--------+------------------+

show databases
import mysql.connector as sqltor

mydb=sqltor.connect(host="localhost",user="root",password="arshad123",database="arshad")
mycursor=mydb.cursor()

mycursor.execute("show databases")

for i in mycursor:

print(i)

output:

== RESTART: C:\Users\gateway\AppData\Local\Programs\Python\Python38\sqltor.py ==

('information_schema',)

('apollo',)

('arshad',)

('biology',)

('candidate',)

('class',)

('cricket',)

('employee',)

('factory',)

('hospital',)

('lib',)

('mysql',)

('performance_schema',)

('student',)

('test',)
delete
command
import mysql.connector as sqltor

mydb=sqltor.connect(host="localhost",user="root",password="arshad123",database="arshad")

mycursor=mydb.cursor()

mycursor.execute("delete from ali where name='steve'")

mydb.commit()

output:

+------+--------+------------------+

| No | Name | qualities |

+------+--------+------------------+

| 2 | irshad | singer_cricketer |

+------+--------+------------------+

* -------------------------------------------------------*

You might also like