Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   sqlite newbie question - how to know if a table exists? (http://www.velocityreviews.com/forums/t516576-sqlite-newbie-question-how-to-know-if-a-table-exists.html)

kf9150@gmail.com 06-22-2007 07:52 AM

sqlite newbie question - how to know if a table exists?
 
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


=?ISO-8859-1?Q?Gerhard_H=E4ring?= 06-22-2007 10:07 AM

Re: sqlite newbie question - how to know if a table exists?
 
kf9150@gmail.com 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

kf9150@gmail.com 06-22-2007 04:47 PM

Re: sqlite newbie question - how to know if a table exists?
 
On Jun 22, 12:07 am, Gerhard Häring <g...@ghaering.de> 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.



All times are GMT. The time now is 03:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57