constraint failed
Posted: Mon 05 Sep 2011 09:07
I use the following code to insert 7 records for a user in a table.
But the function gives me a 'coinstraint failed' when the record exists already. The reason for running the code more than once is to be absolute sure that all 7 records exist.
I have a function that creates 365 records for a year in another table and there it is the same problem.
How can I avoid this error and get my records created?
Delphi XE, UniDAC 3.70.0.19, SQLite (the error also is on MS Access and MySQL)
FormatSettings.ISOLongDayNames is an extension to FormatSettings.LongDayNames that gives me localized daynames
DefaultDay is a Double with value 7.4
UserProfile.ID is the current logged on user
procedure CalendarWeekCreate;
var
Query: TUniQuery;
DayNumber: integer;
begin
Query := TUniQuery.Create(Nil);
try
Query.Connection := frmDBConn.conDBserver;
Query.SQL.Clear;
Query.SQL.Add('INSERT INTO ' + TableCalendarWeek);
Query.SQL.Add('(flddaynumber, flddaytext, flddaynormal, flduser)');
Query.SQL.Add('values (:flddaynumber, :flddaytext, :flddaynormal, :flduser)');
try
for DayNumber := 1 to 7 do
begin
Query.ParamByName('flddaynumber').AsInteger := DayNumber;
Query.ParamByName('flddaytext').AsString := FormatSettings.ISOLongDayNames(DayNumber);
if DayNumber < 6 then
Query.ParamByName('flddaynormal').AsFloat := DefaultDay
else
Query.ParamByName('flddaynormal').AsFloat := 0;
Query.ParamByName('flduser').AsString := UserProfile.ID;
Query.Execute;
end;
except
on E: exception do
begin
Logfile.Error('CalendarWeekCreate: ' + E.Message);
end
end;
finally
Query.Free;
end;
end;
But the function gives me a 'coinstraint failed' when the record exists already. The reason for running the code more than once is to be absolute sure that all 7 records exist.
I have a function that creates 365 records for a year in another table and there it is the same problem.
How can I avoid this error and get my records created?
Delphi XE, UniDAC 3.70.0.19, SQLite (the error also is on MS Access and MySQL)
FormatSettings.ISOLongDayNames is an extension to FormatSettings.LongDayNames that gives me localized daynames
DefaultDay is a Double with value 7.4
UserProfile.ID is the current logged on user
procedure CalendarWeekCreate;
var
Query: TUniQuery;
DayNumber: integer;
begin
Query := TUniQuery.Create(Nil);
try
Query.Connection := frmDBConn.conDBserver;
Query.SQL.Clear;
Query.SQL.Add('INSERT INTO ' + TableCalendarWeek);
Query.SQL.Add('(flddaynumber, flddaytext, flddaynormal, flduser)');
Query.SQL.Add('values (:flddaynumber, :flddaytext, :flddaynormal, :flduser)');
try
for DayNumber := 1 to 7 do
begin
Query.ParamByName('flddaynumber').AsInteger := DayNumber;
Query.ParamByName('flddaytext').AsString := FormatSettings.ISOLongDayNames(DayNumber);
if DayNumber < 6 then
Query.ParamByName('flddaynormal').AsFloat := DefaultDay
else
Query.ParamByName('flddaynormal').AsFloat := 0;
Query.ParamByName('flduser').AsString := UserProfile.ID;
Query.Execute;
end;
except
on E: exception do
begin
Logfile.Error('CalendarWeekCreate: ' + E.Message);
end
end;
finally
Query.Free;
end;
end;