如何在python数据库中插入,更新删除select with loop

时间:2017-07-07 09:33:47

标签: python python-2.7

import MySQLdb conn=MySQLdb.connect("localhost","root","","python")
c=conn.cursor()      
city=raw_input("Enter city name :")
c.execute("insert into city (city_name) values (%s)",(city))

c.execute("select * from city") 
rows=c.fetchall() 
for row in rows:
    print row dno=input("Enter id you want to delete :")
c.execute("delete from city where city_id='%s'",(dno))

eno=input("Enter the id you want to UPDATE :")
cityName=raw_input("Enter name you want to change :");
update=("update city set city_name=%s where city_id=%s")
c.execute(update,(cityName,eno))

c.execute("select * from city") 
rows=c.fetchall() 
for row in rows:
    print row

conn.commit() 
conn.close() 
print "Data are finised"

1 个答案:

答案 0 :(得分:0)

此代码适合您。

import MySQLdb
conn=MySQLdb.connect("128.66.203.247","it1618a14","sumo@123","it1618a14")
c=conn.cursor()
# sudo apt-get install python-mysqldbstrong text
task=1
while(task!=0):
    print "==============================================";
    print "0. Exit";
    print "1. Insert Data";
    print "2. Show Data";
    print "3. Delete Data";
    print "4. Update Data";
    print "==============================================";
    task=input("Enter your choice : ");
    print "==============================================";
    if(task==1):
        # Insert Data Into table
        city=raw_input("Enter city name :")
        c.execute("insert into city (city_name) values (%s)",(city))
        conn.commit()
        # Select Data from Table
        print "===========After Inserting data records are===========";
        c.execute("select * from city")
        rows=c.fetchall()
        for row in rows:
            print row
        print "==============================================";
    elif(task==2):
        # Select Data from Table
        print "===========All Data from table=================";
        c.execute("select * from city")
        rows=c.fetchall()
        for row in rows:
            print row
        print "==============================================";
    elif(task==3):
        # Select Data from Table
        c.execute("select * from city")
        rows=c.fetchall()
        for row in rows:
            print row
        # Delete Data
        print "======Check The data you want to delete===========";
        dno=input("Enter id you want to delete :")
        c.execute("delete from city where city_id='%s'",(dno))
        conn.commit()
        # Select Data from Table
        c.execute("select * from city")
        rows=c.fetchall()
        for row in rows:
            print row
        print "=========After deleted Data=====================";
    elif(task==4):
        # Select Data from Table
        c.execute("select * from city")
        rows=c.fetchall()
        for row in rows:
            print row
        print "=======Check The data you want to update=========";
        # Update Data Code
        eno=input("Enter the id you want to UPDATE :")
        cityName=raw_input("Enter name you want to change :");
        update=("update city set city_name=%s where city_id=%s")
        c.execute(update,(cityName,eno))
        conn.commit()
        print "=========After updated data================";
        # Select Data from Table
        c.execute("select * from city")
        rows=c.fetchall()
        for row in rows:
            print row
    elif(task>4):
        print "Your choice is wrong!! Please Enter right choice";
        print "";
    elif(task<0):
        print "Your choice is wrong!! Please Enter right choice";
        print "";
conn.commit()
conn.close()
print "You have exit the program!!!!"
print "";