Autoinc primary key not refreshed after post

Forum related to SQLite

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
rnoe
Fresh Boarder
Fresh Boarder
Posts: 1
Joined: 02.06.2008, 04:19

Autoinc primary key not refreshed after post

Post by rnoe »

I'm having trouble with TZUpdateSQL on Delphi 7 + Zeos 6.6.2 RC2 + SQLite 3.5.9.

I have an autoincrement primary key field on the table I want to update. But I don't know what kind of RefreshSQL I should give to the ZUpdateSQL so the primary key field automatically retrieved after a post (and the record pointer stays on the record I have just posted).

Here is the reproduction of my situation:

the table
---------
CREATE TABLE [mytable] (
[id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[name] VARCHAR(20));

the ZQuery
----------
object ZQuery1: TZQuery
Connection = ZConnection1
UpdateObject = ZUpdateSQL1
SQL.Strings = (
'select * from mytable'
'order by name')
Params = <>
SequenceField = 'id'
Left = 64
Top = 16
end

the ZUpdateSQL
--------------
object ZUpdateSQL1: TZUpdateSQL
DeleteSQL.Strings = (
'DELETE FROM mytable'
'WHERE'
' ((id IS NULL AND :OLD_id IS NULL) OR (id = :OLD_id))')
InsertSQL.Strings = (
'INSERT INTO mytable'
' (name)'
'VALUES'
' (:name)')
ModifySQL.Strings = (
'UPDATE mytable SET'
' mytable.name = :name'
'WHERE'
' ((id IS NULL AND :OLD_id IS NULL) OR (id = :OLD_id))')
UseSequenceFieldForRefreshSQL = True
Left = 160
Top = 16
ParamData = <
item
DataType = ftUnknown
Name = 'name'
ParamType = ptUnknown
end
item
DataType = ftUnknown
Name = 'OLD_id'
ParamType = ptUnknown
end>
end


I have tried to fill the RefreshSQL with these lines but all failed:
- select * from mytable where id = :id
- select * from mytable where id = :OLD_id
- select * from mytable where id = :NEW_id

I hope someone could help me. Thank you.
wilstyx
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: 23.07.2008, 16:53

Post by wilstyx »

Well i had a similar problem and if you read the Sqlite3 documentation its states you must write a NULL to the INTEGER PRIMARY KEY for it to update. Also i think it might be in the way you are creating that field it just need to read INTEGER PRIMARY KEY the NOT NULL and AUTOINCREMENT may be throwing it off.
Hope this solves your problem it did mine. I still trying to learn this and there are a lot of things that seem like you have to fight more today than in the past.
pttmail
Fresh Boarder
Fresh Boarder
Posts: 1
Joined: 25.02.2009, 23:16

workarround

Post by pttmail »

For refresh on insert try this in your refreshSQL:

select * from my_table where id = :id or id is null order by id desc

(id is your INTEGER PRIMARY KEY AUTOINCREMENT field)
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Hi,

This is still on the TODO list. I have the impression this could be realised the way it's done for mysql. There the last_insert_id is fetched after every inserted record with an API call. I saw this API call is also available in the sqlite APi.

Somebody willing to give it a try? I know I could do it myself, but as I'm not using sqlite a lot this wouldn't be a first priority job for me.

Using refresh_sql you'll have to be smarter, I'm afraid. No way you can get the id of an inserted record unless you can write a unique where-clause without using the id field (eg by including all other fields which combination must be unique in your dataset).

Mark
Image
yagolnik
Fresh Boarder
Fresh Boarder
Posts: 1
Joined: 22.10.2009, 21:15
Location: St. Petersburg

Post by yagolnik »

Hi,

You can use in refreshSQL:

SELECT * FROM mytable
WHERE
( (:OLD_ID is not null) AND (ID = :OLD_ID))
OR
( (:OLD_ID is null) AND (RowID = (select max(rowid) from mytable)) )


Alex
DestinyR
Fresh Boarder
Fresh Boarder
Posts: 9
Joined: 10.03.2010, 16:59

Post by DestinyR »

Hi!!!
Try this RefreshSQL:
select * from mytable where id = (select @@identity)

Edited:
Most correctly:

Code: Select all

select * from mytable 
where
((:OLD_id IS NULL AND mytable.id = (select @@identity)) OR (mytable.id = :OLD_id))
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Hi,

Is a TZUpdateSQL component really necessary in your case? When updating a TZQuery without it, the new autoincrement field should be updated after the post immediately.
Since rev 762 (on january 8.) it should work even better. The native last_increment API function is now used instead of a select statement.

Mark
Image
Post Reply