0% found this document useful (0 votes)
15 views3 pages

MySQL Database Connection and Queries

The document contains Python code for connecting to a MySQL database using the mysql.connector library. It includes commented-out sections for executing SQL queries, fetching records, and inserting new data into tables. The code also demonstrates how to handle user input for searching and inserting records in the database.

Uploaded by

hitesh.aimes
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)
15 views3 pages

MySQL Database Connection and Queries

The document contains Python code for connecting to a MySQL database using the mysql.connector library. It includes commented-out sections for executing SQL queries, fetching records, and inserting new data into tables. The code also demonstrates how to handle user input for searching and inserting records in the database.

Uploaded by

hitesh.aimes
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

import mysql.

connector as sq
conn =
[Link](host='localhost',user='root',password='caca',database
='coaching')
#print(conn.is_connected())
if conn.is_connected()==False:
print('Problem connecting to DB')
cr=[Link]()
#[Link]('select * from emp')
#[Link]('SELECT * FROM EMP where empno=7782')

#d1=[Link]()
#n=[Link] #return -1 if no record is fectched

#print('Number of records=',n)

#print('All records')
#print('Method 1')
#print(d1)
#x=[Link]()
#print(x)
#print('\n\nMethod 2')
#for i in d1:
# print(i)
#print('\n\nMethod 3')
#for i in d1:
# for j in i:
# print(j,end='\t')
# print()
#print('Reading 1 record')
#d2=[Link]()
#print(d2)
#for i in d2:
# print(i,end='\t')
#d3=[Link]()
#print(d3)

#print('Reading n number of records')


#d4=[Link](30)
#print(d4)

#for i in d4:
# print(i)

#Searching for user given values


#t=int(input('Enter the deptno by which records are to be
searched'))
#[Link]('select * from emp where deptno={}'.format(t))
#[Link]("select ENAME from emp where deptno='%s'"%(t))
#d5=[Link]()
#print(d5)
#for i in d5:
# print(i)

#[Link]('INSERT INTO EMP


VALUES(7777,"XYZ","SALESMAN",7902,"1981-10-
25",1000,200,10)')
#[Link]()
#[Link]('CREATE TABLE STAFF(SRNO INT PRIMARY KEY,
NAME VARCHAR(20),DOB DATE)')

#s=int(input('Enter the serial no'))


#n=input('Enter the name')
#d=input('Enter the date of birth(yyyy-mm-dd)')
#[Link]("insert into staff values(%s,'%s','%s')"%(s,n,d))
#[Link]()
[Link]()

You might also like