import MySQLdb conn = MySQLdb.connect(host = "localhost", user = "root" ,passwd = "gongbo0801")cur = conn.cursor()#创建数据库# cur.execute("drop database iamgongbo")cur.execute("create database if not exists iamgongbo")cur.execute("use iamgongbo")#创建数据库表cur.execute("""create table if not exists one( id INT(20), name CHAR(20) ) """)#插入数据cur.execute("insert into one(id,name) values(20,‘gongbo‘)")try: #更新数据 cur.execute("update one set id=23 where name=‘gongbo‘")except: conn.rollback()#获得数据cur.execute("select * from one")list = cur.fetchall()for i in list: print i[0] print i[1]#删除数据cur.execute("delete from one where id = 23")conn.commit()conn.close()
时间: 2024-12-28 11:35:33