python Mysql 数据连接

 python 链接数据库需要下载一个插件

 

下面这个地址只有32位的 64位还需求其他地址

https://sourceforge.net/projects/mysql-python/#

 

数据库链接代码

 

import MySQLdb

conn = MySQLdb.Connect(

    host     = '127.0.0.1',

    port     = 3306,

    user     = 'root',

    passwd   = 'root',

    db       = 'test',

    charset  = 'utf8'

    )

 

print conn

 

#链接成功打印

<_mysql.connection open to '127.0.0.1' at 26a3be8>

 

#编写查询语句

 

#获取游标

cursor = conn.cursor()

#游标执行查询语句

cursor.execute('select * from users limit 100')

#python查询获取到数据后会进入缓冲区,从缓冲区提取数据

result =cursor.fetchall()

for row in result:

    print row

 

 

猜你喜欢

转载自helanhe.iteye.com/blog/2394974