Python 和 MySQL-连接器-Python
Python and MySQL-Connector-Python
注意我在 Windows OS 上使用以下版本的 Python:
(venv) C:\>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
*下面的 Python 脚本是 运行 并且输出显示在它下面的目录中。此脚本的大部分内容来自以下来源:
https://pynative.com/python-mysql-select-query-to-fetch-data/
脚本 1
import mysql.connector
from mysql.connector import Error
try:
cnx = mysql.connector.connect(user='root', password='coldplay123',
host='localhost',
database='nathan_test_1')
cursor_1 = cnx.cursor()
s1="select * from dataframe"
cursor_1.execute(s1)
data1 = cursor_1.fetchall()
print("Total number of dataframes: ", cursor_1.rowcount)
for i1 in data1:
print(i1)
cursor_1.close()
except Error as e1:
print("Failure to connect ... ", e1)
cnx.close()
脚本 1 的输出
Total number of dataframes: 1
('a', 'b', 'c', 699)
*现在,我只更改脚本 1 中的两行
在中间通过简单地将它们注释掉
并生成以下输出:
脚本 2
import mysql.connector
from mysql.connector import Error
try:
cnx = mysql.connector.connect(user='root', password='coldplay123',
host='localhost',
database='nathan_test_1')
cursor_1 = cnx.cursor()
#s1="select * from nathan"
#cursor_1.execute(s1)
data1 = cursor_1.fetchall()
print("Total number of dataframes: ", cursor_1.rowcount)
for i1 in data1:
print(i1)
cursor_1.close()
except Error as e1:
print("Failure to connect ... ", e1)
cnx.close()
脚本 2 的输出
Failure to connect ... No result set to fetch from.
*很容易看出是什么导致了这个错误,但是why却没有
允许 'cursor_1' 执行给定的 SQL 查询导致此
错误?
根据 PEP 249 - Python 数据库 API 规范 v2.0
、fetchone
, fetchmany
, fetchall
documentation、
An Error (or subclass) exception is raised if the previous call to .execute*() did not produce any result set or no call was issued yet.
fetch*()
调用后应进行 execute
调用。
注意我在 Windows OS 上使用以下版本的 Python:
(venv) C:\>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
*下面的 Python 脚本是 运行 并且输出显示在它下面的目录中。此脚本的大部分内容来自以下来源:
https://pynative.com/python-mysql-select-query-to-fetch-data/
脚本 1
import mysql.connector
from mysql.connector import Error
try:
cnx = mysql.connector.connect(user='root', password='coldplay123',
host='localhost',
database='nathan_test_1')
cursor_1 = cnx.cursor()
s1="select * from dataframe"
cursor_1.execute(s1)
data1 = cursor_1.fetchall()
print("Total number of dataframes: ", cursor_1.rowcount)
for i1 in data1:
print(i1)
cursor_1.close()
except Error as e1:
print("Failure to connect ... ", e1)
cnx.close()
脚本 1 的输出
Total number of dataframes: 1
('a', 'b', 'c', 699)
*现在,我只更改脚本 1 中的两行 在中间通过简单地将它们注释掉 并生成以下输出:
脚本 2
import mysql.connector
from mysql.connector import Error
try:
cnx = mysql.connector.connect(user='root', password='coldplay123',
host='localhost',
database='nathan_test_1')
cursor_1 = cnx.cursor()
#s1="select * from nathan"
#cursor_1.execute(s1)
data1 = cursor_1.fetchall()
print("Total number of dataframes: ", cursor_1.rowcount)
for i1 in data1:
print(i1)
cursor_1.close()
except Error as e1:
print("Failure to connect ... ", e1)
cnx.close()
脚本 2 的输出
Failure to connect ... No result set to fetch from.
*很容易看出是什么导致了这个错误,但是why却没有 允许 'cursor_1' 执行给定的 SQL 查询导致此 错误?
根据 PEP 249 - Python 数据库 API 规范 v2.0
、fetchone
, fetchmany
, fetchall
documentation、
An Error (or subclass) exception is raised if the previous call to .execute*() did not produce any result set or no call was issued yet.
fetch*()
调用后应进行 execute
调用。