0% found this document useful (0 votes)
11 views

Database Programs

The document discusses creating a MySQL database and table using Python. It shows how to connect to the database, create a table, insert data into the table via single and multiple row inserts, run select queries with and without conditions, update rows in the table, and delete rows and drop the entire table.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Database Programs

The document discusses creating a MySQL database and table using Python. It shows how to connect to the database, create a table, insert data into the table via single and multiple row inserts, run select queries with and without conditions, update rows in the table, and delete rows and drop the entire table.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#Create a Database:

collection of table is called database.

import mysql.connector # predefined package for connecting database

try:
mydb = mysql.connector.connect(
host="localhost",
user="root",
password=""
)

mycur = mydb.cursor() # statement or query

mycur.execute("create database Priya")


print("Database created...")

except Exception as e:
print(e)

# Create a table in the database:


import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="priya"
)
try:
mycur = mydb.cursor() #statment or for query

mycur.execute("create table student1(regno int not null primary key,name


varchar(30),age int,cont varchar(20),addr varchar(20))")
print("Table created successfully...") #more than one colms- unique single
colm-primary key

except Exception as e:
print(e)

#Insert a Data into the table


import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="priya"
)
# a=[] -List
mycursor = mydb.cursor()

try:
sql = "insert into student1 (regno,name,age,cont,addr) values (%s,%s,%s,%s,%s)"
val = ("123002","prabha","26","9876543990","chennai")
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")


except Exception as e:
print(e)

#Insert a Data into the table


import mysql.connector
try:
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="priya"
)

mycursor = mydb.cursor()
reg=input("Enter the regno:")
name=input("Enter the name:")
age=input("Enter the age:")
cont=input("Enter the contact:")
addr=input("Enter the address:")

sql = "insert into student1 (regno,name,age,cont,addr) values (%s,%s,%s,%s,%s)"


val = (reg,name,age,cont,addr)

mycursor.execute(sql, val) # single row inserted

mydb.commit() # save or update

print(mycursor.rowcount, "record inserted.")

except Exception as e:
print(e)

#Insert many rows into the table


import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="priya"
)

mycursor = mydb.cursor()

try:
sql = "insert into student1 (regno,name,age,cont) values (%s,%s,%s,%s)"
val = [("123003","Divya","25","7654321234"),
("123004","Rajesh","20",'8976543212'),
("123005",'Tamil','26','9098898987'),
('123006','priya','30','7598641236')]
mycursor.executemany(sql, val)
print(mycursor.rowcount, "records inserted.")

mydb.commit()
except Exception as e:
print(e)

# select query: Display the table


import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="Priya"
)

try:

mycursor = mydb.cursor()

mycursor.execute("select * from student1")

res = mycursor.fetchall()

for i in res:
print(i)

except Exception as e:
print(e)

# where condition for getting one row datas


import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="Priya"
)

mycursor = mydb.cursor()

sql = "SELECT regno,name FROM student1 where regno=123003"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="rajesh"
)

mycursor = mydb.cursor()

sql = "SELECT regno,name,age FROM student1 ORDER BY name"

mycursor.execute(sql)

myresult = mycursor.fetchall()

for x in myresult:
print(x)

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="priya"
)

mycursor = mydb.cursor()

sql = "UPDATE student1 SET addr='CMBT' WHERE regno = 123003"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) updated")

# Delete the single row in the table


import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="Priya"
)

try:
mycursor = mydb.cursor()
sql = "Delete from student1 where regno=123002"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) Deleted")


except Exception as e:
print(e)

# Delete the table data


import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="Priya"
)

try:
mycursor = mydb.cursor()
sql = "Delete from student1"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) Deleted")


except Exception as e:
print(e)

import mysql.connector # delete the table name

mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="Priya"
)

mycursor = mydb.cursor()

sql = "Drop table student1"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) deleted")

You might also like