当前位置:Gxlcms > 数据库问题 > MySqlDB基本操作程序一览

MySqlDB基本操作程序一览

时间:2021-07-01 10:21:17 帮助过:10人阅读

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()

MySqlDB基本操作程序一览

标签:

人气教程排行