How to check if table exist? I would like to check if table exist then drop table. If not exist, create table.
My codes at this moment:
import MySQLdb, csv, sys
db = MySQLdb.connect("host","username","password","databasename" )
# prepare a cursor object using cursor() method
c = db.cursor()
# Drop table if it already exist using execute() method.
sql_drop = "DROP TABLE IF EXISTS Sentence"
c.execute(sql_drop)
# Create table as per requirement
sql = """CREATE TABLE Sentence(
Id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(Id),
Sentence TEXT(65535) NOT NULL)"""
c.execute(sql)
csv_data=csv.reader(file("textfile.txt"))
count = 0
for row in csv_data:
count = count + 1
#print count, row
c.execute("INSERT INTO (Sentence) VALUES (%s)", row)
# disconnect from server
db.close()
Any suggest?