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

Robert Helmer rhelmer at rhelmer.org
Wed Apr 6 04:19:11 CEST 2011


On Tue, Apr 5, 2011 at 6:59 PM, michael scott <jigenbakuda at yahoo.com> wrote:
> 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

See http://www.sqlite.org/faq.html#q7

For example:

select * from sqlite_master where name='test';

Alternatively, if the only purpose in checking to see if the table
exists is so that it can be created, you can simple do:

create table if not exists test (...);


More information about the Tutor mailing list