#!/usr/bin/env python
#coding:utf-8
import MySQLdb
'''
#打开数据库链接
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
# 使用cursor()方法获取操作游标
cur = conn.cursor()
# 使用execute方法执行SQL语句
reCount = cur.execute('insert into UserInfo(Name,Address) values(%s,%s)',('alex','usa'))
# 提交到数据库执行
conn.commit()
cur.close()
conn.close()
print reCount
'''
'''
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
cur = conn.cursor()
reCount = cur.execute('delete from UserInfo')
conn.commit()
cur.close()
conn.close()
print reCount
'''
'''
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
cur = conn.cursor()
#插入多条数据数据库
li =[
('alex','usa'),
('sb','usa'),
]
reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li)
conn.commit()
cur.close()
conn.close()
print reCount
'''
'''
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
cur = conn.cursor()
reCount = cur.execute('update UserInfo set Name = %s',('alin',))
conn.commit()
cur.close()
conn.close()
print reCount
'''
'''
#fetchone/fetchmany(num)
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
cur = conn.cursor()
reCount = cur.execute('select * from UserInfo')
print cur.fetchone()
print cur.fetchone()
#绝对定位
cur.scroll(-1,mode='absolute')
#相对定位
cur.scroll(-1,mode='relative')
print cur.fetchone()
print cur.fetchone()
cur.scroll(0,mode='absolute')
# 使用 fetchone() 方法获取一条数据
print cur.fetchone()
print cur.fetchone()
cur.close()
conn.close()
print reCount
'''
Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。
fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
fetchall():接收全部的返回结果行.
rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。
#fetchall
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
#以字典的方式填充结果cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
#cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
cur = conn.cursor()
reCount = cur.execute('select Name,Address from UserInfo')
nRet = cur.fetchall()
cur.close()
conn.close()
print reCount
print nRet
for i in nRet:
print i[0],i[1]