I'm using this function :
我正在使用这个功能:
def checker(name,s)
MY_T = "SELECT count(*) FROM `"+session.SessionInfo.Name where EventName='"+name+"'"
I want to check if the table exists, how can I do it ? I saw some examples using : XXXX.execute()
what does it mean?
我想检查表是否存在,我该怎么办?我看到一些使用的例子:XXXX.execute()是什么意思?
Here is what I saw :
这是我看到的:
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:
I tried printing MY_T to see if it returns -1 for example but it just prints "select count (*)...... "
我尝试打印MY_T以查看它是否返回-1,但它只是打印“select count(*)......”
How can I check it? Any help would be very appreciated.
我怎么检查呢?任何帮助将非常感激。
3 个解决方案
#1
6
Use the "TABLES" information schema view. http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
使用“TABLES”信息架构视图。 http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
SELECT * FROM information_schema.tables
WHERE table_name = 'YOUR TABLE'
You can apply this view to your code by doing something like the following:
您可以通过执行以下操作将此视图应用于您的代码:
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
#2
9
If you are using Python-MySQL (MySQLdb) -> http://mysql-python.sourceforge.net/MySQLdb.html
如果您使用的是Python-MySQL(MySQLdb) - > http://mysql-python.sourceforge.net/MySQLdb.html
cursor.execute() is the method to run queries with MySQLdb, Python MySQL driver. You can pass two arguments, like:
cursor.execute()是使用MySQLdb,Python MySQL驱动程序运行查询的方法。您可以传递两个参数,例如:
cursor.execute(statement, parameters)
And will execute "statement" parsing "parameters" to the statement. You need to have opened a database connection and also open a cursor
并将执行“语句”解析“参数”到语句。您需要打开数据库连接并打开游标
I think you can use MySQL's statement: SHOW TABLES LIKE 'tablename';
我想你可以使用MySQL的声明:SHOW TABLES LIKE'tablename';
stmt = "SHOW TABLES LIKE 'tableName'"
cursor.execute(stmt)
result = cursor.fetchone()
if result:
# there is a table named "tableName"
else:
# there are no tables named "tableName"
EDIT: there will other Python drivers with similar behaviour. Look for yours :)
编辑:将有其他类似行为的Python驱动程序。找你的:)
#3
0
I found that this works well with Python 3.6 and MySql 5.7:
我发现这适用于Python 3.6和MySql 5.7:
table = 'myTable'
_SQL = """SHOW TABLES"""
cursor.execute(_SQL)
results = cursor.fetchall()
print('All existing tables:', results) # Returned as a list of tuples
results_list = [item[0] for item in results] # Conversion to list of str
if table in results_list:
print(table, 'was found!')
else:
print(table, 'was NOT found!')
#1
6
Use the "TABLES" information schema view. http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
使用“TABLES”信息架构视图。 http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
SELECT * FROM information_schema.tables
WHERE table_name = 'YOUR TABLE'
You can apply this view to your code by doing something like the following:
您可以通过执行以下操作将此视图应用于您的代码:
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
#2
9
If you are using Python-MySQL (MySQLdb) -> http://mysql-python.sourceforge.net/MySQLdb.html
如果您使用的是Python-MySQL(MySQLdb) - > http://mysql-python.sourceforge.net/MySQLdb.html
cursor.execute() is the method to run queries with MySQLdb, Python MySQL driver. You can pass two arguments, like:
cursor.execute()是使用MySQLdb,Python MySQL驱动程序运行查询的方法。您可以传递两个参数,例如:
cursor.execute(statement, parameters)
And will execute "statement" parsing "parameters" to the statement. You need to have opened a database connection and also open a cursor
并将执行“语句”解析“参数”到语句。您需要打开数据库连接并打开游标
I think you can use MySQL's statement: SHOW TABLES LIKE 'tablename';
我想你可以使用MySQL的声明:SHOW TABLES LIKE'tablename';
stmt = "SHOW TABLES LIKE 'tableName'"
cursor.execute(stmt)
result = cursor.fetchone()
if result:
# there is a table named "tableName"
else:
# there are no tables named "tableName"
EDIT: there will other Python drivers with similar behaviour. Look for yours :)
编辑:将有其他类似行为的Python驱动程序。找你的:)
#3
0
I found that this works well with Python 3.6 and MySql 5.7:
我发现这适用于Python 3.6和MySql 5.7:
table = 'myTable'
_SQL = """SHOW TABLES"""
cursor.execute(_SQL)
results = cursor.fetchall()
print('All existing tables:', results) # Returned as a list of tuples
results_list = [item[0] for item in results] # Conversion to list of str
if table in results_list:
print(table, 'was found!')
else:
print(table, 'was NOT found!')