0% found this document useful (0 votes)
35 views4 pages

DataBase Progs

This document contains Python code to create a database and table, insert data, search records, and delete records from a MySQL database. The code connects to a MySQL database, creates a table, inserts sample data, searches by brand, and deletes a record by serial number.

Uploaded by

Black Ice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

DataBase Progs

This document contains Python code to create a database and table, insert data, search records, and delete records from a MySQL database. The code connects to a MySQL database, creates a table, inserts sample data, searches by brand, and deletes a record by serial number.

Uploaded by

Black Ice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Write program to create database and table in python :

import mysql.connector

try:

conn = mysql.connector.connect(host='localhost',

user='root',

password='root')

mySql_create_query = """CREATE TABLE if not exists LAPTOP(LNAME VARCHAR(20),LSNO INT,

BRAND VARCHAR(20),PRICE FLOAT,CPU FLOAT)"""

cursor = conn.cursor()

cursor.execute("create database if not exists school")

cursor.execute("use school")

cursor.execute(mySql_create_query)

conn.commit()

print("Table created in database ")

cursor.close()

except mysql.connector.Error as error:

print("Failed to create table {}".format(error))

finally:

if (conn.is_connected()):

conn.close()
Programe to insert data in the database using an python program :

import mysql.connector

try:

connection = mysql.connector.connect(host='localhost',

user='root',

password='root')

if (connection.is_connected()):

cursor = connection.cursor()

cursor.execute("CREATE DATABASE IF NOT EXISTS TEST")

connection.commit()

cursor.execute("USE TEST")

mySql_create_query = """CREATE TABLE if not exists COMPUTER(CNAME VARCHAR(20),CSNO INT,

BRAND VARCHAR(20),PRICE FLOAT,COLOR VARCHAR(20))"""

result = cursor.execute(mySql_create_query)

connection.commit()

print("Table created in database")

sql_insert="""INSERT INTO COMPUTER(CNAME,CSNO,BRAND,PRICE,COLOR) VALUES(%s,%s,%s,%s,%s)"""

val=[('Aspire',59302,'WIPRO',34904.23,'BLACK'),

('EzeeBee',26848,'HCL',36521.23,'WHITE'),

('Purnima',85692,'DELL',44904.23,'BLACK'),

('EzeeBee2',35693,'HCL',42369.12,'RED'),

('Aspire1',26341,'WIPRO',36521.23,'WHITE'),

('RAYS',46215,'ACER',36987.14,'BLACK')]

cursor.executemany(sql_insert,val)

connection.commit()

print("Data inserting in the table ")

cursor.close()

except mysql.connector.Error as error:

print("Failed to create table {}".format(error))

finally:

if (connection.is_connected()):

connection.close()

print("MySQL connection is closed")

input("Exiting Now..")
Programe to search an record from the database using an python program :

import mysql.connector

try:

mycon = mysql.connector.connect(host='localhost',

database='school',

user='root',

password='kv31')

cursor = mycon.cursor()

val=input("Enter the BRAND of laptop to SEARCH :: ")

mySql_select_query= "Select * from LAPTOP where BRAND = "+"'"+val+"'"

cursor.execute(mySql_select_query)

if(cursor.rowcount == 0):

print("Record not found...")

else:

print("The following are the matching Records")

result=cursor.fetchall()

for rec in result:

print(rec)

cursor.close()

except mysql.connector.Error as error:

print("Failed to SEARCH record from table {}".format(error))

finally:

if (mycon.is_connected()):

mycon.close()

print("MySQL Connection is closed")


Programe to delete record from the database using an python program :

import mysql.connector

from mysql.connector import Error

from mysql.connector import errorcode

try:

mycon = mysql.connector.connect(host='localhost',

database='school',

user='root',

password='kv31')

val=input("Enter the Serial Number of laptop to remove :: ")

mySql_delete_query = "delete from laptop where lsno = "+val

cursor = mycon.cursor()

cursor.execute(mySql_delete_query)

mycon.commit()

if(cursor.rowcount == 0):

print("Record not found...")

else:

print("Record removed...")

print("Record deleted successfully from LAPTOP Table")

cursor.close()

except mysql.connector.Error as error:

print("Failed to delete record from table {}".format(error))

finally:

if (mycon.is_connected()):

mycon.close()

print("MySQL Connection is closed")

You might also like