In [2]:
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()
    6 Komal           4500.00
    7 Muffy           10000.00
In [4]:
## 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()
Employees with salary between 2000 and 5000
    6 Komal           4500.00
Employees with salary between 1000 and 8000
    1 Ramesh          2000.00
    2 Khilan          1500.00
    3 kaushik         2000.00
    4 Chaitali        6500.00
    6 Komal           4500.00
In [5]:
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()
1  row(s) inserted
In [6]:
 
  File "<ipython-input-6-daa2d8d07267>", line 1
    select * from customers
             ^
SyntaxError: invalid syntax
In [ ]: