【www.gdgbn.com--python】

python连接mysql教程数据库教程方法详解


host,连接的数据库服务器主机名,默认为本地主机(localhost)。  
        user,连接数据库的用户名,默认为当前用户。
        passwd,连接密码,没有默认值。
        db,连接的数据库名,没有默认值。
        conv,将文字映射到python类型的字典。默认为

mysqldb.converters.conversions
        cursorclass,cursor()使用的种类,默认值为mysqldb.cursors.cursor


        compress,启用协议压缩功能。
        named_pipe,在windows中,与一个命名管道相连接。
        init_command,一旦连接建立,就为数据库服务器指定一条语句来运行。

            
        read_default_file,使用指定的mysql配置文件。
        read_default_group,读取的默认组。
        unix_socket,在unix中,连接使用的套接字,默认使用tcp。
        port,指定数据库服务器的连接端口,默认是3306。

import mysqldb 

conn = mysqldb.connect(host="localhost",  

                       user="root",  

                       passwd="root",  

                       db="testdb") 

cursor = conn.cursor() 

cursor.execute("select * from users") 

res = cursor.fetchall() 

print res 

cursor.close() 

conn.close()

查询数据实例二
cn = connection("localhost", "root", "")
cn.select_db("yphp教程")
cur = cn.cursor()
cur.execute("set names utf8")
cur.execute("select id,url,content from yphp_spider_url where checked=0

limit 0,1")
rs = cur.fetchone()
print rs

mysql connector/python 是一个用 python 语言实现的 mysql 客户端服务器的连

接协议。该软件包无需安装任何 mysql 的软件。使用的是 python 3.1

  该版本支持回 python 2.4 版本,可处理大的数据包,支持连接压缩协议和

ssl。

  mysql connector/python 是 mysql 官方提供的 python 连接 mysql 数据库

的驱动程序。

本文来源:http://www.gdgbn.com/jiaocheng/29354/