Skip to main content

Command Palette

Search for a command to run...

Introduction to MySQL Database in Python: Day 31 Tutorial

First Steps with MySQL Database in Python

Published
2 min read
Introduction to MySQL Database in Python: Day 31 Tutorial
A

I'm Archana, pursuing Graduation in Information technology and Management. I'm a fresher with expertise in Python programming. I'm excited to apply my skills in AI/ML learning , Python, Java and web development. Looking forward to collaborating and learning from industry experts.

Introduction :

Welcome back to my Python journey! Yesterday, I laid the foundation with modules and packages in python.

Today, I dove into mysql database, Let's explore what I learned!

#Program for Linux:

from MySQLdb import *

conobj =connect()

curobj = conobj.cursor()

#create a new db

#curobj.execute('create database ONLINE_BATCH;')

#show list all DB

'''

curobj.execute('show databases;')

list_db =curobj.fetchall()

for name in list_db:

print ( name )

'''

curobj.execute('use ONLINE_BATCH;')

#curobj.execute ('create table STUDENT (RegNO int, Name varchar(30), Course_Name varchar(20), Duration varchar(20), Fees int); ')

'''

curobj.execute('show tables;')

list_table=curobj.fetchall()

print (list_table)

'''

#insert record

#curobj.execute ('insert into STUDENT values (1, "rajani","python", "90 days", 5000);')

#curobj.execute ('insert into STUDENT(RegNo, Name, Course_Name, Fees) values (2, "rajani kant" , "C", 4500 ); ')

#curobj.execute ('insert into STUDENT(RegNo, Name, Course_Name, Duration) values (3, "amit","java","60 days");')

#update query

curobj.execute ('update STUDENT set Duration = "45 days" where RegNO = 2 ;')

curobj.execute ('update STUDENT set Fees = 4000 where RegNo =3;')

#delete record :

curobj.execute ('delete from STUDENT where RegNO = 2;')

curobj.execute ('select * from STUDENT;')

record = curobj.fetchall()

print (record)

for RegNo, Name, Cource_Name, Duration, Fees in record :

print (RegNo, Name, Cource_Name, Duration, Fees)

curobj.close()

conobj.close()

Program for window:

#mysql connector driver

import mysql.connector as mysql

conobj =mysql.connect(host ="localhost",user ="",password ="")

curobj = conobj.cursor()

curobj.execute("show databases")

data = curobj.fetchall() print (data)

curobj.close() conobj.close()

\===========================================

#for pymysql

import pymysql

conobj =pymysql.connect(host ="localhost",user ="root",password ="")

curobj = conobj.cursor()

curobj.execute("show databases")

curobj.close()

conobj.close()

Challenges :

  1. Understanding python databases.

  2. Handling errors.

Resources :

  1. Official Python Documentation: python databases

  2. W3Schools' Python Tutorial: python databases

  3. Scaler's Python Courses : python databases

Goals for Tomorrow :

  1. Explore something more on mysql database.

Conclusion :

Day 31’s a success!

What are your favorite Python resources? Share in the comments below.

Connect with me :

GitHub: [ https://github.com/p-archana1 ]

LinkedIn : [ linkedin.com/in/archana-prusty-4aa0b827a ]

twitter : [ https://x.com/_archana77 ]

Join the conversation :

Share your own learning experiences or ask questions in the comments.

HAPPY LEARNING!!

THANK YOU!!

More from this blog

A

Archana's blog

41 posts

Python Insights: Exploring the world of Python programming, one code snippet at a time. Follow along for tutorials, projects, and insights on machine learning, web development, and data science.