#coding=utf-8
import MySQLdb
import MySQLdb.cursors
conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='root',
db ='test',
cursorclass = MySQLdb.cursors.DictCursor, charset='utf8')
cur = conn.cursor()
#创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")
#插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")
#修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")
#删除查询条件的数据
#cur.execute("delete from student where age='9'")
cur.execute('select name from student')
for data in cur.fetchall():
print data['name']
conn.commit()
cur.close()
conn.close()Python操作MySQL数据库
最新推荐文章于 2025-06-16 12:59:03 发布
本文提供了一个使用 Python 操作 MySQL 数据库的具体示例,包括连接数据库、执行 SQL 查询并打印查询结果等基本操作。
3531

被折叠的 条评论
为什么被折叠?



