On 22/03/2011, at 9:04 AM, Erich93063 wrote:

> I am trying to create a SQLite database if it doesn't exist, which I
> know I can use 'CREATE TABLE IF NOT EXISTS", but more importantly, I
> need to initially populate the database with seed data if it doesn't
> exist. If I use CREATE TABLE IF NOT EXISTS, it will obviously create
> the table if it doesn't exist, but if I follow that up with insert
> statements, those would ALWAYS get ran. I only want to enter the seed
> data if the database does not exist. ???

Others have suggested solutions that require you to step back and forth from 
SQL to application code (except Max's solution), but you can do this in pure 
SQL. Something like:

begin immediate;
create temp table if not exists "Variables" (Name text unique collate nocase, 
"Value");
insert or replace into "Variables" select 'MyTable exists', 1 in (select Name 
from SQLite_Master where type = 'table');
create table if not exists "My Table" (ID integer primary key not null, Name 
text unique); -- or whatever your definition
insert into "My Table" (Name) select 'First row' where (select "Value" from 
"Variables" where Name = 'MyTable exists');
insert into "My Table" (Name) select 'Second row' where (select "Value" from 
"Variables" where Name = 'MyTable exists');
insert into "My Table" (Name) select 'Third row' where (select "Value" from 
"Variables" where Name = 'MyTable exists');
commit;


Thanks,
Tom
BareFeetWare

 --
Comparison of SQLite GUI tools:
http://www.barefeetware.com/sqlite/compare/?ml



_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to