我试图确定python中的mysql查询中的元组中是否存在值。到目前为止,这是我的代码:
query = ("select distinct(table_schema) FROM information_schema.tables where engine = 'MyISAM' AND table_schema NOT IN ('information_schema', 'mysql', 'performance_schema');")
cursor.execute(query)
for row in cursor.fetchall():
print ('\t %s' % row[0])
f.write('%s \n' % row[0])
databaseName = input('Which Database would you like to convert? ')
databaseInput = any(databaseName in row for row in query)
if databaseInput == False:
print('You have entered an incorrect database name!')
else:
do something...
我在语法方面遇到了一些麻烦。 if语句似乎无法正常工作。
任何有关确定元组元组中是否存在值的正确语法和逻辑的帮助都将受到赞赏。感谢。
答案 0 :(得分:0)
我想出的不一定是我的确切问题的答案,而是完全正如我原来的帖子时所做的那样。问题是使用mysql.connector将值返回到元组元组中的游标。我改为从元组元组中取出每个元素并将其附加到我创建的列表中。以下是我的代码现在的样子:
query = ("select distinct(table_schema) FROM information_schema.tables where engine = 'MyISAM' AND table_schema NOT IN ('information_schema', 'mysql', 'performance_schema');")
cursor.execute(query)
databaseList = []
for row in cursor.fetchall():
print ('\t %s' % row[0])
databaseList.append(row[0])
databaseName = input('Which Database would you like to convert? ')
while databaseName not in databaseList:
print('You have entered an invalid database name!')
databaseName = input('Which Database would you like to convert? ')
else:
continue doing stuff....
希望这可以帮助将来可能会遇到此问题的人。感谢。