Jump to content

SQLite insert techique for unique key


Recommended Posts

SQLite Experts,

What is a common table insert practice, test for the record and insert if it does not exist or insert and test for duplicate return code?

This seems like a "it depends..." kind of situation.

Searches on Google show it done both ways.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Don't forget the SQlite help itself.

http://www.sqlite.org/lang.html

Seems that REPLACE might be the one your looking for.

(also take a look at ON CONFLICT)

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

It all depends on your schema and its underlying rules. Logical use of either a (hidden) rowid or an integer primary key can make a huge difference, whether you handle the id yourself or leave SQLite create one for you.

Never forget that INSERT OR REPLACE will first delete one or more existing row(s) then insert one anew. If you have FK on the deleted row(s) it may not be what you need!

AFAICT there is no definitive "rule", every situation calls for its own logic.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thank you, gentelmen.

@iEvKI3gv9Wrkd41u - I have read that doc and others and use it for reference. I have a local copy of it. I wish that it had examplem, however. Do you mind if I ask you how you came up with that screen name and what it means?

@jhcd - Yes, I thought a much. Unfortunately I do not yet understand enough about DB management to make any intelligent decisions regarding DB organization. I am planning the schema (is that the word that describes the DB organization?) now. It seems like a trivial event, but I am tickled pink that I actually have a DB loaded. I read much of what you posted and learned enough to get something going. I have a mountain of questions but this forum is hardly the place for DBA type discussions. I'll keep digging till I get it.

I used the transaction batching technique that you recently advised on another post. The difference on my computer is at least 300% (probably due to minimized I/O activity).

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Do you mind if I ask you how you came up with that screen name and what it means?

I did not and it means nothing. Its just a random generated (password) string. (Its my actual login name on this forum)

... kinda boring, but thats the way it is ...

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

You'll certainly benefit from freely digging into the SQLite mailing list archive which is the premium mine of information both theoritical and practical.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Hi,

normally, you do know whether the key already exists. (Programmatical logic)

Guess what: If you don't know then there are several ways to get it done.

1 ) Check for key (via SQL) e.g. by return code

a ) if it exsis, then update

b ) if not, then insert

2 ) Firstly make a delete then an insert

3 ) Using a special command (depends on the used database)

E.g. in DB2 >= V9 you can try using Merge.

You can also create your own function which provides the logic/code.

Mega

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Yes, that or much more simply, declare a ON CONFLICT IGNORE on the unique column(s) and forget about applicative code complexity.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...