Jump to content

Python SQLite how to insert values if table is empty

I have code that creates a table and then inserts data into the table. I've set the table to only be created if it doesn't already exist, but how can I do something like that for the insertion of values? For example, my insertion code is this:

c.execute("INSERT INTO ip VALUES(?, ?, ?)", (domainA, ip, lastIP))

So how could I modify that to only insert if the table is empty?

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, Lunar Evolution said:

I have code that creates a table and then inserts data into the table. I've set the table to only be created if it doesn't already exist, but how can I do something like that for the insertion of values? For example, my insertion code is this:


c.execute("INSERT INTO ip VALUES(?, ?, ?)", (domainA, ip, lastIP))

So how could I modify that to only insert if the table is empty?

select * from ip

if row count == 0 then

insert into table

 

I only know how to do this in php but that code should help with the logic. Im mot completely sure why you only want to insert if the tale is empty.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

select * from ip

if row count == 0 then

insert into table

 

I only know how to do this in php but that code should help with the logic. Im mot completely sure why you only want to insert if the tale is empty.

For the same reason I only want to create one if it doesn't exist. It's essentially supposed to say "the first time this runs make a database and table and input values" and the "each subsequent time, check new values against those in the database." 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Lunar Evolution said:

For the same reason I only want to create one if it doesn't exist. It's essentially supposed to say "the first time this runs make a database and table and input values" and the "each subsequent time, check new values against those in the database." 

the sql will just fail if it doesn't exist. 

 

have a look inti try and catch. In c# you can go

 

try

{insert sql}

catch

{some why ti report an error}

 

but really you should always create tables then insert data and always abort the script if the creation fails so there is never a chance for your code to try and put data inti a non existent table

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×