我正在使用这个功能:
def checker(name,s)
MY_T = "SELECT count(*) FROM `"+session.SessionInfo.Name where EventName='"+name+"'"
我想检查表是否存在,我该怎么做?
我看到了一些使用 XXXX.execute()
的示例。什么意思?
这是我看到的:
query = cursor.execute("""SELECT count(*) FROM scan WHERE prefix = %s and code_id = %s and answer = %s and station_id = %s""",
(prefix, code_id, answer, station,))
if query != 1:
例如,我尝试打印 MY_T 以查看它是否返回 -1,但它只打印 "select count (*)...... "
如何检查?
最佳答案
使用“TABLES”信息模态视图。 http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
SELECT * FROM information_schema.tables
WHERE table_name = 'YOUR TABLE'
您可以通过执行以下操作将此 View 应用到您的代码中:
def checkTableExists(dbcon, tablename):
dbcur = dbcon.cursor()
dbcur.execute("""
SELECT COUNT(*)
FROM information_schema.tables
WHERE table_name = '{0}'
""".format(tablename.replace('\'', '\'\'')))
if dbcur.fetchone()[0] == 1:
dbcur.close()
return True
dbcur.close()
return False
关于python - 如何检查表是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17044259/