打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[Python]Python连接MySQL的实例代码
!/usr/bin/env python    
 
-*-coding:UTF-8-*-   
这一句告诉python用UTF-8编码    
 
=========================================================================    
    
 
NAME: Python MySQL test    
 
 
AUTHOR: benyur    
 
DATE : 2004-12-28    
 
 
COMMENT: 这是一个python连接mysql的例子    
 
=========================================================================    
    
"""   
***** This is a MySQL test *****   
select:   
conn=Connection()   
conn.select_db('test')   
cur=conn.cursor()   
cur.execute('select * from user')   
cur.scroll(0)   
row1=cur.fetchone()   
row1[0]   
row1[1]   
row1[2]   
insert:   
cur.execute('insert into user (name,passwd) values(\'benyur\',\'12345\')')   
cur.insert_id()   
update:   
cur.execute('update user set passwd=\'123456\' where name=\'benyur\'')   
delete:   
cur.execute('delete from user where id=2')   
**********************************   
"""    
    
from MySQLdb import *    
    
    
def conn():    
conn=Connection()    
conn.select_db('test')    
cur=conn.cursor()    
cur.execute('select * from user')    
cur.scroll(0)    
row1=cur.fetchone()    
row1[0]    
row1[1]    
row1[2]    
    
    
def usage():    
print __doc__    
    
    
if __name__=='__main__':    
usage()  
!/usr/bin/env python 
-*-coding:UTF-8-*-
这一句告诉python用UTF-8编码 
========================================================================= 
 
NAME: Python MySQL test 

AUTHOR: benyur 
DATE : 2004-12-28 

COMMENT: 这是一个python连接mysql的例子 
========================================================================= 
 
""" 
***** This is a MySQL test ***** 
select: 
conn=Connection() 
conn.select_db('test') 
cur=conn.cursor() 
cur.execute('select * from user') 
cur.scroll(0) 
row1=cur.fetchone() 
row1[0] 
row1[1] 
row1[2] 
insert: 
cur.execute('insert into user (name,passwd) values(\'benyur\',\'12345\')') 
cur.insert_id() 
update: 
cur.execute('update user set passwd=\'123456\' where name=\'benyur\'') 
delete: 
cur.execute('delete from user where id=2') 
********************************** 
""" 
 
from MySQLdb import * 
 
 
def conn(): 
conn=Connection() 
conn.select_db('test') 
cur=conn.cursor() 
cur.execute('select * from user') 
cur.scroll(0) 
row1=cur.fetchone() 
row1[0] 
row1[1] 
row1[2] 
 
 
def usage(): 
print __doc__ 
 
 
if __name__=='__main__': 
usage()
MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/
下载解压缩后放到%Python_HOME%\Lib\site-packages目录中,python会自动找到此包。
MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0。
其他:
1. 平台及版本
linux 内核2.6,gcc 3.4.4,glibc 2.4
python 2.4.3
mysql 5.0.19
mysql-python 1.2.1-p2

2. 安装mysql-python
tar xvfz MySQL-python-1.2.1_p2.tar.gz
cd MySQL-python-1.2.1_p2
python setup.py build
python setup.py install
3. 使用
import MySQLdb

3.1. 连接
conn = MySQLdb.Connection(host, user, password, dbname)

3.2. 选择数据库
conn.select_db(’database name’)

3.3. 获得cursor
cur = conn.cursor()

3.4. cursor位置设定
cur.scroll(int, mode)
mode可为相对位置或者绝对位置,分别为relative和absolute。

3.5. select
cur.execute(‘select clause’)
例如
cur.execute(‘select * from mytable’)

row = cur.fetchall()
或者:
row1 = cur.fetchone()

3.6. insert
cur.execute(‘inset clause’)
例如
cur.execute(‘insert into table (row1, row2) values (\’111\’, \’222\’)’)
conn.commit()
3.7. update
cur.execute(‘update clause’)
例如
cur.execute(“update table set row1 = ‘’ where row2 = ‘row2 ‘ ”)
conn.commit()

3.8. delete
cur.execute(‘delete clause’)
例如
cur.execute(“delete from table where row1 = ‘row1’ ”)

conn.commit()
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wenhai_zhang/archive/2009/12/14/5006526.aspx
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python操作MySQL简单实现方法
Python学习之路(二):MySQLdb的几种安装方式,以及用Python测试连接MySql
Python 数据库开发 - 天生我材必有用,千金散尽还复来 - JavaEye技术网站
Python操作mysql数据库:MySQLdb模块
Python使用MySQL数据库的方法以及一个实例
python操作MySQL数据库
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服