Saving settings to database - insert vs update
I want to use an Sqlite database to save certain program settings. I want the program to load the settings on startup & set the controls accordingly and then save the settings on program exit. This will basically setup the program with the last used settings when opened. I feel like I need to use an update statement to save the settings but that won't work on the first run of the program, because there won't be any settings in the table yet to update. Do I need to check first if the settings exist in the table and if they don't exist then do an insert & if they do exist then do an update? That seems like an inefficient way to do it. There's got to be a better way. Any suggestions?
Re: Saving settings to database - insert vs update
The easiest way is to include the initial default settings.
If not, then you would certainly need to check if a setting exists before an update and if it doesn't then do an add. I wouldn't worry about inefficiency, unless your talking about 100,000 settings records.
Re: Saving settings to database - insert vs update
Quote:
Originally Posted by
wes4dbt
The easiest way is to include the initial default settings.
I actually did think about doing that way. Since I am creating the database & tables on first start of the program, it would pretty easy to just insert some default values for the settings. I just wanted to see how others might tackle the problem. I'm sure there are other ways to do it also. Thanks...
Re: Saving settings to database - insert vs update
Quote:
Originally Posted by
nbrege
I just wanted to see how others might tackle the problem. I'm sure there are other ways to do it also. Thanks...
a) Either by doing an UPSERT if possible, or if not, b) then by writing a default record with default values...
Since your'e creating the db... seems like b would be the easier/better choice.
-tg
Re: Saving settings to database - insert vs update
I would also tend to insert the default values at startup, when creating the database. Are you using default values for these settings when your app runs the first time? If so, that's even more reason to insert default values.