python连接mysql数据库

本文详细介绍了如何在Python中连接MySQL数据库,包括安装、配置、连接、使用cursor执行SQL操作(如插入、更新、删除、选择数据)等关键步骤。

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

python连接mysql数据库[复制链接]

  
跳转到指定楼层
1#
发表于 2008-12-6 12:39:00 | 只看该作者 | 倒序浏览
1.   平台及版本 WkFTechFeel
linux 内核2.6,gcc 3.4.4,glibc 2.4 WkFTechFeel
python 2.4.3 WkFTechFeel
mysql 5.0.19 WkFTechFeel
mysql-python 1.2.1-p2 WkFTechFeel
2.   安装mysql-python WkFTechFeel
tar xvfz MySQL-python-1.2.1_p2.tar.gz WkFTechFeel
cd MySQL-python-1.2.1_p2 WkFTechFeel
python setup.py build WkFTechFeel
python setup.py install WkFTechFeel
WkFTechFeel
3.   使用 WkFTechFeel
import MySQLdb WkFTechFeel
3.1.   连接 WkFTechFeel
conn =   MySQLdb.Connection(host, user, password, dbname) WkFTechFeel
3.2.   选择数据库 WkFTechFeel
conn.select_db(’database name’) WkFTechFeel
3.3.   获得cursor WkFTechFeel
cur =   conn.cursor() WkFTechFeel
3.4.   cursor位置设定 WkFTechFeel
cur.scroll(int, mode) WkFTechFeel
mode可为相对位置或者绝对位置,分别为relative和absolute。 WkFTechFeel
WkFTechFeel
3.5.   select WkFTechFeel
cur.execute(‘select clause’) WkFTechFeel
例如 WkFTechFeel
cur.execute(‘select * from mytable’) WkFTechFeel
WkFTechFeel
row = cur.fetchall() WkFTechFeel
或者: WkFTechFeel
row1 = cur.fetchone() WkFTechFeel
3.6.   insert WkFTechFeel
cur.execute(‘inset clause’) WkFTechFeel
例如 WkFTechFeel
cur.execute(‘insert  into table (row1, row2) values (\’111\’, \’222\’)’) WkFTechFeel
WkFTechFeel
conn.commit() WkFTechFeel
WkFTechFeel
3.7.   update WkFTechFeel
cur.execute(‘update  clause’) WkFTechFeel
例如 WkFTechFeel
cur.execute(“update  table set   row1 = ‘’  where  row2 = ‘row2 ‘  ”) WkFTechFeel
WkFTechFeel
conn.commit() WkFTechFeel
WkFTechFeel
3.8.   delete WkFTechFeel
cur.execute(‘delete  clause’) WkFTechFeel
例如 WkFTechFeel
cur.execute(“delete from  table  where   row1 = ‘row1’  ”) WkFTechFeel
WkFTechFeel
conn.commit() WkFTechFeel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值