在 Python MySQL 中输出时未定义值?
Values not being defined when outputted in Python MySQL?
在先前提出的问题的帮助下,请参阅(), I'm attempting to apply to a MySQL connector script written in python. As seen by @Popeye 's fiddle http://sqlfiddle.com/#!9/997790/25,我现在尝试返回所有行,其中 C 的值为空,但其输出如下所示
import mysql.connector
db = mysql.connector.connect(
host="localhost",
port="3307",
user="root",
passwd="#",
database="#"
)
mycursor = db.cursor()
mycursor.execute("CREATE TABLE trial157 (Name TEXT(13) NULL , A1 TEXT(13) NULL , A2 TEXT(13) NULL , A3 TEXT(13) NULL , A4 TEXT(13) NULL)")
sql = "INSERT INTO trial157(Name, A1, A2, A3, A4) VALUES (%s, %s, %s, %s, %s)"
val = [
('Person A', 'C', 'T', 'R', 'S'),
('Person B', 'M', 'V', 'R', 'S'),
('Person C', 'M', None , 'R', 'C')
]
mycursor.executemany(sql, val)
db.commit()
mycursor.execute("SELECT * From (select name_new, max(case when name = 'PersonA' then A end) as PersonA, max(case when name = 'PersonB' then A end) as PersonB, max(case when name = 'PersonC' then A end) as PersonC from (select name, 'A1' name_new, A1 A from trial157 union all select name, 'A2' name_new, A2 A from trial157 union all select name, 'A3' name_new, A3 A from trial157 union all select name, 'A4' name_new, A4 A from trial157 ) t group by name_new) t where PersonC is null")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
- OUTPUT
('A1', None, None, None)
('A2', None, None, None)
('A3', None, None, None)
('A4', None, None, None)
[Finished in 1.2s]
您使用了错误的名称。(在 case
中使用的表达式名称没有 space
。您应该按照 val
使用它完全相同)
替换
max(case when name = 'PersonA' then A end) as PersonA
有
max(case when name = 'Person A' then A end) as PersonA
参见name
中Person
和A
之间的space
。对 Person B
和 Person C
做同样的事情
在先前提出的问题的帮助下,请参阅(
import mysql.connector
db = mysql.connector.connect(
host="localhost",
port="3307",
user="root",
passwd="#",
database="#"
)
mycursor = db.cursor()
mycursor.execute("CREATE TABLE trial157 (Name TEXT(13) NULL , A1 TEXT(13) NULL , A2 TEXT(13) NULL , A3 TEXT(13) NULL , A4 TEXT(13) NULL)")
sql = "INSERT INTO trial157(Name, A1, A2, A3, A4) VALUES (%s, %s, %s, %s, %s)"
val = [
('Person A', 'C', 'T', 'R', 'S'),
('Person B', 'M', 'V', 'R', 'S'),
('Person C', 'M', None , 'R', 'C')
]
mycursor.executemany(sql, val)
db.commit()
mycursor.execute("SELECT * From (select name_new, max(case when name = 'PersonA' then A end) as PersonA, max(case when name = 'PersonB' then A end) as PersonB, max(case when name = 'PersonC' then A end) as PersonC from (select name, 'A1' name_new, A1 A from trial157 union all select name, 'A2' name_new, A2 A from trial157 union all select name, 'A3' name_new, A3 A from trial157 union all select name, 'A4' name_new, A4 A from trial157 ) t group by name_new) t where PersonC is null")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
- OUTPUT
('A1', None, None, None)
('A2', None, None, None)
('A3', None, None, None)
('A4', None, None, None)
[Finished in 1.2s]
您使用了错误的名称。(在 case
中使用的表达式名称没有 space
。您应该按照 val
使用它完全相同)
替换
max(case when name = 'PersonA' then A end) as PersonA
有
max(case when name = 'Person A' then A end) as PersonA
参见name
中Person
和A
之间的space
。对 Person B
和 Person C