Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > sqlite newbie question - how to know if a table exists?

Reply
Thread Tools

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

 
 
kf9150@gmail.com
Guest
Posts: n/a
 
      06-22-2007
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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Gerhard_H=E4ring?=
Guest
Posts: n/a
 
      06-22-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
kf9150@gmail.com
Guest
Posts: n/a
 
      06-22-2007
On Jun 22, 12:07 am, Gerhard Häring <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Ruby sqlite/gem error: Could not load sqlite adapter jhs408@gmail.com Ruby 4 04-18-2009 12:53 AM
Installing sqlite-ruby when sqlite is in non-standard location Carl Youngblood Ruby 1 04-09-2005 03:32 AM
Table/table rows/table data tag question? Rio HTML 4 11-05-2004 08:11 AM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM



Advertisments