Python学习笔记8(mysql类操作)

Python学习笔记7(mysql类操作)

1:安装MySQLdb,下载地址

http://sourceforge.net/projects/mysql-python/

2:连接mysql例程

# -*- coding: utf-8 -*-

import MySQLdb

conn=MySQLdb.connect(host='127.0.0.1',port=3307,user='root',passwd='usbw',db='test')

cursor = conn.cursor()

cursor.execute("select * from guestbook")

datas = cursor.fetchall()

for data in datas:

print data[1]

print cursor.rowcount,"rows in tatal"

conn.close

注意下:python的代码还是自己一行一行的敲出来比较好,对学习有帮助,复制和粘贴

容易出问题,报语法错误,这也算是python的特色吧!

3:运行结果

开始-运行-CMD

你可能感兴趣的:(Python学习笔记8(mysql类操作))