首页 > 数据库技术 > 详细

Python3中使用Mysql的用法。

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

一、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的用法。

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!