[Tutor] (sqlite3) Testing if a table has been created.

michael scott jigenbakuda at yahoo.com
Wed Apr 6 03:59:17 CEST 2011


Hello guys,

Since sqlite gives an error if you try to create a table that's already there, 
how do I test if a table is already present?


for example in

def database(info):
    import sqlite3

    connection = sqlite3.connect("test.db")
    cursor = connection.cursor()
    if table not in test.db: #with this being my test that I'm not sure how to 
implement
        cursor.execute(""" CREATE TABLE stuff (id INTEGER PRIMARY KEY, name 
TEXT)""")
    cursor.execute("""INSERT INTO stuff VALUES (null, ?)""",(info))
    cursor.commit()
    cursor.close()
    connection.close()


How would I properly test  if table not in test.db: ? Is it something as simple 
as having a method of cursor check it for me, or should I just create my table 
in the beginning of my code(outside of this function) and leave it out of the 
function all together, so I can just have my function focusing on inserting 
data?

Uhm, in the immediate example I'm using, this is a function, thats inside of a 
function used by a tkinter button, a save button if you will. So I want it to 
save whatever is in the entry widget to the database. Keeping this in mind, am I 
going about it the wrong way? Should I be trying to save a different way?






----
What is it about you... that intrigues me so?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110405/589a8971/attachment.html>


More information about the Tutor mailing list