MySqlDB基本操作程序一览

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()
posted @ 2016-01-21 17:21  浪里白条_双河  阅读(211)  评论(0编辑  收藏  举报