如何使用python 语句来检查sql table 是否存在。?
How to use python statement to check the sql table is exist or not.?
我想知道如何写python语句来检查sql table是否存在。
如果 table 存在,那么我将向 table 插入数据,否则,我将创建 table.
table 名字是 "resulttable",
db = MySQLdb.connect("localhost","root","123","test")
cursor = db.cursor()
sql="""CREATE TABLE resulttable ( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, writerName CHAR(20) NOT NULL, matchWords LONGTEXT, matchMagazine LONGTEXT, matchNews LONGTEXT )"""
cursor.execute(sql)
db.close()
检查部分怎么做?
我添加了这个语句,但是我得到了一个错误:
sql="""CREATE TABLE IF NOT EXISTS resulttable ( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, writerName CHAR(20) NOT NULL, matchWords LONGTEXT, matchMagazine LONGTEXT, matchNews LONGTEXT )"""
cursor.execute(sql)
db.close()
错误是:
警告:Table 'resulttable' 已经存在
cursor.execute(sql)
使用"TABLES" 信息模式视图。 http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
SELECT * FROM information_schema.tables
WHERE table_name = 'YOUR TABLE'
您可以通过执行以下操作将此视图应用于您的代码:
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
db = MySQLdb.connect("localhost","root","123","test")
cursor = db.cursor()
sql="""CREATE TABLE IF NOT EXISTS resulttable ( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, writerName CHAR(20) NOT NULL, matchWords LONGTEXT, matchMagazine LONGTEXT, matchNews LONGTEXT )"""
cursor.execute(sql)
# add insert staff
# insert_sql = "inset ..."
# cursor.execute(sql)
cursor.close()
db.close()
如果你想检查 table 如果存在的话,检查这个 python - how to check if table exists?
我想知道如何写python语句来检查sql table是否存在。
如果 table 存在,那么我将向 table 插入数据,否则,我将创建 table.
table 名字是 "resulttable",
db = MySQLdb.connect("localhost","root","123","test")
cursor = db.cursor()
sql="""CREATE TABLE resulttable ( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, writerName CHAR(20) NOT NULL, matchWords LONGTEXT, matchMagazine LONGTEXT, matchNews LONGTEXT )"""
cursor.execute(sql)
db.close()
检查部分怎么做?
我添加了这个语句,但是我得到了一个错误:
sql="""CREATE TABLE IF NOT EXISTS resulttable ( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, writerName CHAR(20) NOT NULL, matchWords LONGTEXT, matchMagazine LONGTEXT, matchNews LONGTEXT )"""
cursor.execute(sql)
db.close()
错误是: 警告:Table 'resulttable' 已经存在 cursor.execute(sql)
使用"TABLES" 信息模式视图。 http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
SELECT * FROM information_schema.tables
WHERE table_name = 'YOUR TABLE'
您可以通过执行以下操作将此视图应用于您的代码:
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
db = MySQLdb.connect("localhost","root","123","test")
cursor = db.cursor()
sql="""CREATE TABLE IF NOT EXISTS resulttable ( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, writerName CHAR(20) NOT NULL, matchWords LONGTEXT, matchMagazine LONGTEXT, matchNews LONGTEXT )"""
cursor.execute(sql)
# add insert staff
# insert_sql = "inset ..."
# cursor.execute(sql)
cursor.close()
db.close()
如果你想检查 table 如果存在的话,检查这个 python - how to check if table exists?