zoukankan      html  css  js  c++  java
  • Python3中使用Mysql的用法。

    一、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.")
  • 相关阅读:
    第五章 数据的共享与保护
    实验6
    实验5
    实验4 类与对象2)
    实验三 类与对象
    实验2
    2018—3-21第二章程序例题(2)
    第二章思维导图
    2018—3-18C++第二章程序例题
    汇编实验九
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/7056953.html
Copyright © 2011-2022 走看看