码迷,mamicode.com
首页 > 数据库 > 详细

Python3中使用Mysql的用法。

时间:2017-06-21 00:21:47      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:mysqld   fetch   localhost   local   finally   success   sql   span   记录   

一、Python2中一般使用MySqldb来调用Mysql,但是在Python3中不支持该包,使用pymysql来代替了,用法一模一样。

二、安装:

pip install pymysql

三、例子:

#coding utf-8
import pymysql
try:
    conn = pymysql.connect(host=localhost,user="root",password=123456,database=datarepair,port=3306,charset=utf8)
    cursor = conn.cursor()
    #cursor.execute("select rowId,Name from ruletab where ruletype=%s",(10))
    cursor.executemany("select rowId,Name from ruletab where ruletype=%s",[10,20]) #一般用于批量增删改中。
    print(cursor.rowcount,cursor.rownumber)
    for x in cursor.fetchall():
        print("rowId:{0[0]} Name:{0[1]} RowNumber:{1}".format(x,cursor.rownumber))
    print(- * 50)
    for x in cursor.fetchmany(2):
        print("rowId:{0[0]} Name:{0[1]} RowNumber:{1}".format(x,cursor.rowcount))
    print(- * 50)
    cursor.scroll(0,absolute) #将游标调回0位置处。
    for x in cursor.fetchmany(2):  #取2条记录。
        print("rowId:{0[0]} Name:{0[1]} RowNumber:{1}".format(x,cursor.rownumber))
    print(cursor.rowcount,cursor.rownumber)
    cursor.close()


finally:
    if conn in dir() and callable(conn) and conn.open:
        conn.close()
        print("closed successfully.")

 

Python3中使用Mysql的用法。

标签:mysqld   fetch   localhost   local   finally   success   sql   span   记录   

原文地址:http://www.cnblogs.com/songxingzhu/p/7056953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!