sqlite newbie question - how to know if a table exists?

Discussion in 'Python' started by kf9150@gmail.com, Jun 22, 2007.

  1. Guest

    Hello,

    Another newbie question: How do I know if there is a table with
    certain name in a sqlite database? What i'm doing now is just create
    the table with that name, if exception occurs, that means the table is
    already created. Am i correct? Any better way? Thank you.

    kelie
     
    , Jun 22, 2007
    #1
    1. Advertising

  2. wrote:
    > Hello,
    >
    > Another newbie question: How do I know if there is a table with
    > certain name in a sqlite database? What i'm doing now is just create
    > the table with that name, if exception occurs, that means the table is
    > already created. Am i correct? Any better way? Thank you.


    That approach is ok. If your SQLite library is recent enough (I don't
    know the exact version), you can use "create table if not exists ...".

    For older SQLite releases, you can check like this:

    len(con.execute("pragma table_info(?)", ("tablename",)).fetchall()) > 0

    or

    con.execute("select count(*) from sqlite_master where name=?",
    ("tablename" ,)).fetchone()

    -- Gerhard
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jun 22, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 22, 12:07 am, Gerhard Häring <> wrote:
    > That approach is ok. If your SQLite library is recent enough (I don't
    > know the exact version), you can use "create table if not exists ...".


    >
    > -- Gerhard



    Thanks Gerhard. I'm using sqlite3 that came with Python2.5
    installation. So "create table if not exists" works.
     
    , Jun 22, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Totan
    Replies:
    0
    Views:
    1,032
    Totan
    Apr 17, 2006
  2. Ulf Meinhardt
    Replies:
    8
    Views:
    6,225
  3. Carl Youngblood
    Replies:
    1
    Views:
    235
    Carl Youngblood
    Apr 9, 2005
  4. Replies:
    4
    Views:
    356
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    253
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page