'Table 不存在',使用带 'multi=True' 参数的 execute() 函数创建
'Table doesn't exist' with creation using execute() function with 'multi=True' parameter
MySQL5.6.
我不明白为什么如果我使用 multi=True 参数 table 没有创建。我在文档中找不到有关它的任何信息。如果我将所有查询分开,它会起作用,但我不想分开它们。
from mysql import connector as connector
conn = connector.connect(
user='root',
password='root',
host='127.0.0.1',
database='ra'
)
conn.autocommit = True
cursor = conn.cursor(dictionary=True, buffered=True)
sql = """
DROP TABLE IF EXISTS tab01;
CREATE TABLE tab01 (nzu DECIMAL(5,0) PRIMARY KEY,
nbr DECIMAL(3,0) DEFAULT NULL,
nland DECIMAL(5,0) DEFAULT NULL,
nlandtype DECIMAL(5,0) DEFAULT NULL,
nzuarea DECIMAL(12,1) DEFAULT NULL,
nzuareaos DECIMAL(12,1) DEFAULT NULL) ENGINE = MyISAM;
INSERT INTO tab01 (SELECT zu.nzu, zu.nbr, zu.nland,
IF(ISNULL(lands.nlandtype),0,lands.nlandtype) AS nlandtype,
0.0 AS nzuarea, 0.0 AS nzuareaos
FROM (SELECT * FROM zu WHERE nhoz = '6204000001') AS zu
LEFT JOIN lands AS lands ON zu.nland=lands.nland
ORDER BY nzu);
"""
cursor.execute(sql, multi=True)
cursor.execute('SELECT * FROM tab01')
# mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'ra.tab01' doesn't exist
result = cursor.fetchone()
您确定您的 CREATE
有效吗?我相信 ENGINE
选项必须在 CREATE
语句的末尾:
CREATE TABLE tab01 (
nzu DECIMAL(5,0) PRIMARY KEY,
nbr DECIMAL(3,0) DEFAULT NULL,
nland DECIMAL(5,0) DEFAULT NULL,
nlandtype DECIMAL(5,0) DEFAULT NULL,
nzuarea DECIMAL(12,1) DEFAULT NULL,
nzuareaos DECIMAL(12,1) DEFAULT NULL
) ENGINE = MyISAM;
还有这个声明:
cursor.execute(sql, multi=True)
在结果上创建一个迭代器。看起来它是惰性的(即,它只在需要时执行 SQL 语句)。您永远不会要求第二条语句的结果,因此它只执行第一条语句。尝试:
for _ in cursor.execute(sql, multi=True): pass
MySQL5.6.
我不明白为什么如果我使用 multi=True 参数 table 没有创建。我在文档中找不到有关它的任何信息。如果我将所有查询分开,它会起作用,但我不想分开它们。
from mysql import connector as connector
conn = connector.connect(
user='root',
password='root',
host='127.0.0.1',
database='ra'
)
conn.autocommit = True
cursor = conn.cursor(dictionary=True, buffered=True)
sql = """
DROP TABLE IF EXISTS tab01;
CREATE TABLE tab01 (nzu DECIMAL(5,0) PRIMARY KEY,
nbr DECIMAL(3,0) DEFAULT NULL,
nland DECIMAL(5,0) DEFAULT NULL,
nlandtype DECIMAL(5,0) DEFAULT NULL,
nzuarea DECIMAL(12,1) DEFAULT NULL,
nzuareaos DECIMAL(12,1) DEFAULT NULL) ENGINE = MyISAM;
INSERT INTO tab01 (SELECT zu.nzu, zu.nbr, zu.nland,
IF(ISNULL(lands.nlandtype),0,lands.nlandtype) AS nlandtype,
0.0 AS nzuarea, 0.0 AS nzuareaos
FROM (SELECT * FROM zu WHERE nhoz = '6204000001') AS zu
LEFT JOIN lands AS lands ON zu.nland=lands.nland
ORDER BY nzu);
"""
cursor.execute(sql, multi=True)
cursor.execute('SELECT * FROM tab01')
# mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'ra.tab01' doesn't exist
result = cursor.fetchone()
您确定您的 CREATE
有效吗?我相信 ENGINE
选项必须在 CREATE
语句的末尾:
CREATE TABLE tab01 (
nzu DECIMAL(5,0) PRIMARY KEY,
nbr DECIMAL(3,0) DEFAULT NULL,
nland DECIMAL(5,0) DEFAULT NULL,
nlandtype DECIMAL(5,0) DEFAULT NULL,
nzuarea DECIMAL(12,1) DEFAULT NULL,
nzuareaos DECIMAL(12,1) DEFAULT NULL
) ENGINE = MyISAM;
还有这个声明:
cursor.execute(sql, multi=True)
在结果上创建一个迭代器。看起来它是惰性的(即,它只在需要时执行 SQL 语句)。您永远不会要求第二条语句的结果,因此它只执行第一条语句。尝试:
for _ in cursor.execute(sql, multi=True): pass