import mysql.connector
def query(db):
cursor = db.cursor()
q = '''
SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000 AND age < 25
'''
cursor.execute(q)
for (id, name, salary) in cursor:
print("{0:5d} {1:15s} {2:7.2f}".format(id, name, salary))
cursor.close()
def main():
db = mysql.connector.connect(user='raj', password='r123', database='raj')
query(db)
db.close()
main()
## import mysql.connector
def query2(db,low,high):
cursor = db.cursor()
q = '''
SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > %s AND SALARY < %s
'''
cursor.execute(q,(low,high))
for (id, name, salary) in cursor:
print("{0:5d} {1:15s} {2:7.2f}".format(id, name, salary))
cursor.close()
def main():
db = mysql.connector.connect(user='raj', password='r123', database='raj')
print("Employees with salary between 2000 and 5000")
query2(db,2000,5000)
print("Employees with salary between 1000 and 8000")
query2(db,1000,8000)
db.close()
main()
def insert(db):
cursor = db.cursor()
q = 'INSERT INTO CUSTOMERS VALUES (%s,%s,%s,%s,%s)'
cursor.execute(q,(9,'Raj2',60,'Atlanta',80000.00))
print(cursor.rowcount," row(s) inserted")
cursor.close()
def main():
db = mysql.connector.connect(user='raj', password='r123', database='raj')
insert(db)
db.commit()
db.close()
main()