python 如何操作mysql 数据库

本文提供了一个使用 Python 和 MySQLdb 连接 MySQL 数据库并进行数据操作的示例代码,包括连接数据库、执行 SQL 查询、获取查询结果等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,首先安装python,mysqldb,mysql 

以下是连接代码:

#coding=utf-8
import MySQLdb,sqlite3
import time

conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='shopweb1.5.5sp5'
        )
cur = conn.cursor()

#创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")
#cur.execute("create table stu(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("insert into stu values('2','jone','3 year 3 class','10')")
# sqli="insert into student values(%s,%s,%s,%s)"
# cur.executemany(sqli,[
#     ('3','Tom','1 year 1 class','6'),
#     ('3','Jack','2 year 1 class','7'),
#     ('3','Yaheng','2 year 2 class','7'),
#     ])
#修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")

#删除查询条件的数据
#cur.execute("delete from student where age='9'")

#查询表中有多少条数据
a = cur.execute("select * from hs_goods where sku=1002")
print a
#打印表中的多少数据
info=cur.fetchmany(a)
for ii in info:
   print ii
cur.close()
conn.commit()
conn.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值