Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Conncectivity With Mysql

The document discusses connecting to a MySQL database in Python and performing CRUD operations like inserting, updating, deleting, and reading data from tables. It provides examples of inserting sample data into a Student table, updating and deleting rows, and using cursor methods like fetchone, fetchall, fetchmany to retrieve data.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Conncectivity With Mysql

The document discusses connecting to a MySQL database in Python and performing CRUD operations like inserting, updating, deleting, and reading data from tables. It provides examples of inserting sample data into a Student table, updating and deleting rows, and using cursor methods like fetchone, fetchall, fetchmany to retrieve data.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Interface of Python my

MySQL Inserting data into


Connecting with Database database
import mysql.connector
d=mysql.connector.connect(host="localhost Sample Table
",user="root", Student (Database – School
Id Name Marks
passwd="12345",database="School")

if d.is_connected(): #Check for


connectivity print("Database c.execute("insert into student
connected") values(105,'Diya',90)")
else: c.execute("insert into student
print("Not connected") values(108,'Suraj',83)")
c.execute("insert into student
c=d.cursor() #Cursor values(112,'Karan',98)")
c.execute("insert into student
values(115,'Riya',97)")
d.commit() #for saving data

Result Table
Student
Id Name Marks
105 Diya 90
108 Suraj 83
112 Karan 98
115 Riya 97
(105, 'Diya', 90)

Updating Data (108, 'Suraj', 83)

(112, 'Karan', 98)


c.execute("update student set marks=97
(115, 'Riya', 97)
where name='Suraj'")
d.commit() #for saving data

Result Table Function for Reading


Student
Id Name Marks Data
105 Diya 90
108 Suraj 97 Rowcount – Returns total number of
112 Karan 98 records in table.
115 Riya 97
Example

Deleting Data print("Total rows ", c.rowcount)

c.execute("delete from student where Output


id=108") 4
c.commit()
Fetchone()
Result Table
Reads one record at a time (next record)
Student
from cursor.
Id Name Marks
105 Diya 90
112 Karan 98
115 Riya 97 print("Data from table")

c.execute("select * from student")


Reading Data from Tables
p=c.fetchone()
c.execute("select * from student")
print("First record ", p)
for x in c:
p=c.fetchone()
print(x)
print("Second record ",p)
Ouput
Ouput
Data from table
Data from table

First record (105, 'Diya', 90)

Second record (108, 'Suraj', 83)

Fetchall()

Reads all records from cursor.

c.execute("select * from student")

p=c.fetchall()

print("All records - ", p)

Output

All records - [(105, 'Diya', 90), (108, 'Suraj',


83), (112, 'Karan', 98), (115, 'Riya', 97)]

Fetchmany(n)

Reads n records from cursor.

p=c.fetchmany(2)

print("First two records - ", p)

Ouput

First two records - [(105, 'Diya', 90), (108,


'Suraj', 83)]

You might also like