Python-mysql for checking table exist or not

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 8
Reputation: laspal is an unknown quantity at this point 
Solved Threads: 0
laspal laspal is offline Offline
Newbie Poster

Python-mysql for checking table exist or not

 
0
  #1
Jan 5th, 2008
Hi
I wanted to know how can I make sure that table exist in my database.
  1. class Record:
  2. def __init__(self):
  3. self.conn = MySQLdb.connect( host = 'localhost', user = 'root', passwd = 'abcd', db = 'justfun')
  4. self.cursor = self.conn.cursor()
  5. self.cursor.execute("DROP TABLE IF EXISTS book")
  6. self.cursor.execute( """CREATE TABLE book (name char(40), lastname char(40), petname char (40)) """)
  7.  
  8. main()
  9. 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 .
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,056
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 298
woooee woooee is offline Offline
Veteran Poster

Re: Python-mysql for checking table exist or not

 
0
  #2
Jan 5th, 2008
The standard way is
  1. if not os.path.isfile(full_file_name):
  2. CREATE Table
Last edited by woooee; Jan 5th, 2008 at 3:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Python-mysql for checking table exist or not

 
0
  #3
Jan 7th, 2008
Originally Posted by laspal View Post
Hi
I wanted to know how can I make sure that table exist in my database.

  1. class Record:
  2. def __init__(self):
  3. self.conn = MySQLdb.connect( host = 'localhost', user = 'root', passwd = 'abcd', db = 'justfun')
  4. self.cursor = self.conn.cursor()
  5. self.cursor.execute("DROP TABLE IF EXISTS book")
  6. self.cursor.execute( """CREATE TABLE book (name char(40), lastname char(40), petname char (40)) """)
  7.  
  8. main()
  9. 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 .
hi,

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.

  1. class Record:
  2. def __init__(self):
  3. try:
  4. self.conn = MySQLdb.connect( host = 'localhost', user = 'root', passwd = 'abcd', db = 'justfun')
  5. self.cursor = self.conn.cursor()
  6. self.cursor.execute("DROP TABLE IF EXISTS book")
  7.  
  8. # try catching connection refuse and other exceptions. Sorry i dont remember exception to catch
  9. #except :
  10. #...
  11.  
  12. # code within finally block will execute irrespective of exception
  13. finally:
  14. self.cursor.execute( """CREATE TABLE book (name char(40), lastname char(40), petname char (40)) """)
  15.  
  16. main()
  17. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC