LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2011, 01:31 PM   #1
drj000
Member
 
Registered: Sep 2004
Location: Houston, TX
Distribution: Fedora
Posts: 261

Rep: Reputation: 33
Question sqlite triggers after insert problem


I have an sqlite database with two tables that have autoincrement primary keys. I wanted those keys to be unique to the database, and not just the table.
When you use autoincrement keys in sqlite, a system table called sqlite_sequence is created with two columns "CREATE TALBE sqlite_sequence (name,seq)" where name is the table name and seq is the value of the highest key used for the table. Whenever a new row is inserted, seq is updated.

I found a way to keep that value sort of in sync with this query: "UPDATE sqlite_sequence SET seq=(SELECT MAX(seq) FROM sqlite_sequence)". That works, so I decided to add a trigger for each table, like this:
Code:
CREATE TRIGGER update_sequence
   AFTER 
   INSERT ON table_name
BEGIN
    UPDATE sqlite_sequence SET seq=(SELECT MAX(seq) FROM sqlite_sequence);
END
Unfortunately, the trigger occurs after the table is updated, but before sqlite_sequence is updated. The result is that if table_a was just updated, and the new seq is 138, table_b has a seq of 137.

I tried to create an update trigger on sqlite_sequence, but an error was thrown telling me I couldn't create a trigger on a system table.

Does anybody have an idea of how I might accomplish this? I've really been wracking my brain trying to figure this out.

Last edited by drj000; 05-16-2011 at 02:34 PM. Reason: typed in the trigger wrong
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Sqlite-Commander - A ncurses based tool to display the records and tables of a sqlite database LXer Syndicated Linux News 0 01-02-2011 08:11 AM
insert lines into a file after calculating where to insert xonar Programming 13 12-24-2009 04:37 AM
Strange PHP/SQLite problem RattleSn@ke *BSD 1 06-25-2009 04:51 PM
How to use variables in SQL insert statement (SQLite) montylee Programming 2 06-13-2008 07:06 AM
Mouse triggers? carlosinfl Linux - Hardware 0 01-31-2005 02:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration