MySqlDB基本操做程序一覽

import MySQLdbconn = 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()
相關文章
相關標籤/搜索