📅  最后修改于: 2020-11-07 07:44:00             🧑  作者: Mango
mysql-connector-python的MySQLCursor(和类似的库)用于执行与MySQL数据库通信的语句。
使用它的方法,您可以执行SQL语句,从结果集中获取数据,调用过程。
您可以使用Connection对象/类的cursor()方法创建Cursor对象。
import mysql.connector
#establishing the connection
conn = mysql.connector.connect(
user='root', password='password', host='127.0.0.1', database='mydb'
)
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
以下是Cursor类/对象提供的各种方法。
Sr.No | Methods & Description |
---|---|
1 |
callproc() This method is used to call existing procedures MySQL database. |
2 |
close() This method is used to close the current cursor object. |
3 |
Info() This method gives information about the last query. |
4 |
executemany() This method accepts a list series of parameters list. Prepares an MySQL query and executes it with all the parameters. |
5 |
execute() This method accepts a MySQL query as a parameter and executes the given query. |
6 |
fetchall() This method retrieves all the rows in the result set of a query and returns them as list of tuples. (If we execute this after retrieving few rows it returns the remaining ones) |
7 |
fetchone() This method fetches the next row in the result of a query and returns it as a tuple. |
8 |
fetchmany() This method is similar to the fetchone() but, it retrieves the next set of rows in the result set of a query, instead of a single row. |
9 |
fetchwarnings() This method returns the warnings generated by the last executed query. |
以下是Cursor类的属性-
Sr.No | Property & Description |
---|---|
1 |
column_names This is a read only property which returns the list containing the column names of a result-set. |
2 |
description This is a read only property which returns the list containing the description of columns in a result-set. |
3 |
lastrowid This is a read only property, if there are any auto-incremented columns in the table, this returns the value generated for that column in the last INSERT or, UPDATE operation. |
4 |
rowcount This returns the number of rows returned/updated in case of SELECT and UPDATE operations. |
5 |
statement This property returns the last executed statement. |