| | |
Python-mysql for checking table exist or not
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2007
Posts: 8
Reputation:
Solved Threads: 0
Hi
I wanted to know how can I make sure that table exist in my database.
The problem here is how can i check if my table exist or not.
I dont want to create a table if its already exists.
Thanks .
I wanted to know how can I make sure that table exist in my database.
Python Syntax (Toggle Plain Text)
class Record: def __init__(self): self.conn = MySQLdb.connect( host = 'localhost', user = 'root', passwd = 'abcd', db = 'justfun') self.cursor = self.conn.cursor() self.cursor.execute("DROP TABLE IF EXISTS book") self.cursor.execute( """CREATE TABLE book (name char(40), lastname char(40), petname char (40)) """) main() Rec = Record()
The problem here is how can i check if my table exist or not.
I dont want to create a table if its already exists.
Thanks .
•
•
Join Date: Dec 2006
Posts: 1,056
Reputation:
Solved Threads: 298
The standard way is
Python Syntax (Toggle Plain Text)
if not os.path.isfile(full_file_name): CREATE Table
Last edited by woooee; Jan 5th, 2008 at 3:39 pm.
•
•
•
•
Hi
I wanted to know how can I make sure that table exist in my database.
Python Syntax (Toggle Plain Text)
class Record: def __init__(self): self.conn = MySQLdb.connect( host = 'localhost', user = 'root', passwd = 'abcd', db = 'justfun') self.cursor = self.conn.cursor() self.cursor.execute("DROP TABLE IF EXISTS book") self.cursor.execute( """CREATE TABLE book (name char(40), lastname char(40), petname char (40)) """) main() Rec = Record()
The problem here is how can i check if my table exist or not.
I dont want to create a table if its already exists.
Thanks .
Do you want to drop and create table every time you instatiate your above class? If so, you can keep drop statement within
try ... finally block. By doing so, if the table does not exists then you get exception saying UNKNOWN TABLE NAME(means, table does not present). And the code within finally is what you always expect that is create table. Python Syntax (Toggle Plain Text)
class Record: def __init__(self): try: self.conn = MySQLdb.connect( host = 'localhost', user = 'root', passwd = 'abcd', db = 'justfun') self.cursor = self.conn.cursor() self.cursor.execute("DROP TABLE IF EXISTS book") # try catching connection refuse and other exceptions. Sorry i dont remember exception to catch #except : #... # code within finally block will execute irrespective of exception finally: self.cursor.execute( """CREATE TABLE book (name char(40), lastname char(40), petname char (40)) """) main() Rec = Record()
OR
If you want to create table only if it is not already present in the database, then you can do so with query statement itself.
# To create a table by checking if the table already exists in MySQL.
CREATE TABLE IF NOT EXISTS book (name char(40), lastname char(40), petname char (40))It is good practice to place your code within
try...except block, which helps you catching exact problems. Sorry for the incomplete code. I hope this helps to some extent.kath.
Last edited by katharnakh; Jan 7th, 2008 at 6:27 am.
![]() |
Other Threads in the Python Forum
- Previous Thread: gregorian to julian in python
- Next Thread: Where to place modules?
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code copy customdialog decimals dictionary directory drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyglet pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






