Board index » delphi » 'Key violation'

'Key violation'

Hi

    I have this function I created:

procedure TFS_Starting_Form.mLoadFiles(Tablename: TTable; num:
Integer; s1, s2,
        s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13: string);
var
    s: string;
    done: boolean;
begin
    if num = 1 then
    begin
        AssignFile(f1, s1);
        Reset(f1);
        while not eof(f1) do
        begin
            done := false;
            Tablename.Insert;
            Readln(f1, s);
            Tablename.FieldByName(s2).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s3).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s4).Asstring := s;
            while not done do
            begin
                Readln(f1, s);
                if s = '' then
                begin
                    Readln(f1, s);
                    if s = '' then
                    begin
                        Readln(f1, s);
                        if s = '' then
                        begin
                            Readln(f1, s);
                            if s = '' then
                            begin
                                Readln(f1, s);
                                if s = '' then
                                begin
                                    while not done do
                                    begin
                                        Readln(f1, s);
                                        if s <> '' then
                                        begin

Tablename.FieldByName(s5).Asstring := s;
                                            Readln(f1, s);

Tablename.FieldByName(s6).Asstring := s;
                                            Readln(f1, s);

Tablename.FieldByName(s7).Asstring := s;
                                            Readln(f1, s);

Tablename.FieldByName(s8).Asstring := s;
                                            Readln(f1, s);

Tablename.FieldByName(s9).Asstring := s;
                                            if s10 <> 'safety' then
                                            begin
                                                Readln(f1, s);

Tablename.FieldByName(s10).Asstring := s;
                                                Readln(f1, s);

Tablename.FieldByName(s11).Asstring := s;
                                                Readln(f1, s);

Tablename.FieldByName(s12).Asstring := s;
                                                Readln(f1, s);

Tablename.FieldByName(s13).Asstring := s;
                                            end;
           'This is where it crashes' ----> Tablename.Post;
                                            done := true;
                                        end;
                                    end;
                                end
                                else
                                    Tablename.FieldByName(s4).Asstring
:= Tablename.FieldByName(s4).Asstring + CRLF + s;
                            end
                            else
                                Tablename.FieldByName(s4).Asstring :=
Tablename.FieldByName(s4).Asstring + CRLF + s;
                        end
                        else
                            Tablename.FieldByName(s4).Asstring :=
Tablename.FieldByName(s4).Asstring + CRLF + s;
                    end
                    else
                        Tablename.FieldByName(s4).Asstring :=
Tablename.FieldByName(s4).Asstring + CRLF + s;
                end
                else
                    Tablename.FieldByName(s4).Asstring :=
Tablename.FieldByName(s4).Asstring + CRLF + s;
            end;
        end;
    end
    else
    begin
        AssignFile(f1, s1);
        Reset(f1);
        while not eof(f1) do
        begin
            Tablename.Insert;
            Readln(f1, s);
            Tablename.FieldByName(s2).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s3).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s4).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s5).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s6).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s7).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s8).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s9).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s10).Asstring := s;
            Readln(f1, s);
            Tablename.FieldByName(s11).Asstring := s;
            if s12 <> 'safety' then
            begin
                Readln(f1, s);
                Tablename.FieldByName(s12).Asstring := s;
            end;
            Tablename.Post;
        end;
    end;
    closefile(f1);
end;

and I call like so:

mLoadFiles(Table1, 1, 'Student_Info.txt', 'Student', 'Phone',
'Address', 'Discipline',
        'CFSA_Number', 'Club', 'Club_Number', 'Last_Freestyle',
'Last_Skills',
        'Last_dance', 'Last_Artistic', 'DOB');

Now my function does what it is supposed to do 99% of the time, but if
my program crashes for some reason, when I reset it and try to run it
again, it generates a EDBEngineError with message 'Key Violation' when
it tries to post.  Everytime I try to run it afterwards, it keeps
generating the error.  I have to shut down all open delphi programs
and open everything again.  Can anybody figure out what I am doing
wrong here or a way that I can fix the key violation?

Thanks in Advance
Ghost

PS. I'm using a paradox 7 table

 

Re:'Key violation'


Quote
"Ghost" <jeremy_gilbe...@hotmail.com> wrote in message

news:55f559cc.0306231010.7e87a0a3@posting.google.com...

Quote
> Now my function does what it is supposed to do 99% of the time, but if
> my program crashes for some reason, when I reset it and try to run it
> again, it generates a EDBEngineError with message 'Key Violation' when
> it tries to post.  Everytime I try to run it afterwards, it keeps
> generating the error.  I have to shut down all open delphi programs
> and open everything again.  Can anybody figure out what I am doing
> wrong here or a way that I can fix the key violation?

A KeyViolation error is raised by the BDE when one attempts to Post a record
that has the same key as some other record in an index which is marked as
unique - the primary index is always unique. I would guess that the problem
arises when one attempts to re-post records from a partially processed input
file.

Re:'Key violation'


Quote
"Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message <news:w4IJa.51$CK.2965@news20.bellglobal.com>...
> "Ghost" <jeremy_gilbe...@hotmail.com> wrote in message
> news:55f559cc.0306231010.7e87a0a3@posting.google.com...

> > Now my function does what it is supposed to do 99% of the time, but if
> > my program crashes for some reason, when I reset it and try to run it
> > again, it generates a EDBEngineError with message 'Key Violation' when
> > it tries to post.  Everytime I try to run it afterwards, it keeps
> > generating the error.  I have to shut down all open delphi programs
> > and open everything again.  Can anybody figure out what I am doing
> > wrong here or a way that I can fix the key violation?

> A KeyViolation error is raised by the BDE when one attempts to Post a record
> that has the same key as some other record in an index which is marked as
> unique - the primary index is always unique. I would guess that the problem
> arises when one attempts to re-post records from a partially processed input
> file.

That sounds like it is what it is doing, but how can I fix that
problem?  When my program crashes I have to shut down delphi cause it
won't let me run again.  How can reset the database to allow me to run
again?  or what commands would reset the database like shutting delphi
down does?

Ghost

Re:'Key violation'


Quote
"Ghost" <jeremy_gilbe...@hotmail.com> wrote in message

news:55f559cc.0306240419.4f066407@posting.google.com...
Quote
> "Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message

<news:w4IJa.51$CK.2965@news20.bellglobal.com>...

Quote
> That sounds like it is what it is doing, but how can I fix that
> problem?  When my program crashes I have to shut down delphi cause it
> won't let me run again.  How can reset the database to allow me to run
> again?  or what commands would reset the database like shutting delphi
> down does?

No simple answers to these questions.

I would suggest that you alter your code so that it checks to see if the
record already exists. I usually code stuff like that something like

if aTable.Locate ('PrimaryKeyField1, . . .', VarArrayOf
([inputValueForPrimaryKeyField1, . . .]), [])
then aTable.Edit
else aTable.Insert;
// move all the data
aTable.Post;

This takes care of the problem with duplicate primary keys. However if you
have secondary indexes that are also unique you are going to have to do more
checking.

I've never had a problem with a db program crashing and forcing me to close
Delphi in order to re-run the program. Are you opening tables with Exclusive
true?

Re:'Key violation'


Quote
"Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message <news:TllKa.3950$Fe3.635868@news20.bellglobal.com>...
> "Ghost" <jeremy_gilbe...@hotmail.com> wrote in message
> news:55f559cc.0306240419.4f066407@posting.google.com...
> > "Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message
> <news:w4IJa.51$CK.2965@news20.bellglobal.com>...

> > That sounds like it is what it is doing, but how can I fix that
> > problem?  When my program crashes I have to shut down delphi cause it
> > won't let me run again.  How can reset the database to allow me to run
> > again?  or what commands would reset the database like shutting delphi
> > down does?

> No simple answers to these questions.

> I would suggest that you alter your code so that it checks to see if the
> record already exists. I usually code stuff like that something like

> if aTable.Locate ('PrimaryKeyField1, . . .', VarArrayOf
> ([inputValueForPrimaryKeyField1, . . .]), [])
> then aTable.Edit
> else aTable.Insert;
> // move all the data
> aTable.Post;

I'll give this a try.

Quote
> This takes care of the problem with duplicate primary keys. However if you
> have secondary indexes that are also unique you are going to have to do more
> checking.

Can I still just use aTable.Locate to locate the secondary indexes
too?  And just use a nested Locate?

Quote
> I've never had a problem with a db program crashing and forcing me to close
> Delphi in order to re-run the program. Are you opening tables with Exclusive
> true?

I keep having these kinda problems with db programs...I guess I am
jinxed...:(
The Exclusive property is set to false, so that isn't causing it.

Re:'Key violation'


Quote
"Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message <news:TllKa.3950$Fe3.635868@news20.bellglobal.com>...
> "Ghost" <jeremy_gilbe...@hotmail.com> wrote in message
> news:55f559cc.0306240419.4f066407@posting.google.com...
> > "Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message
> <news:w4IJa.51$CK.2965@news20.bellglobal.com>...

> > That sounds like it is what it is doing, but how can I fix that
> > problem?  When my program crashes I have to shut down delphi cause it
> > won't let me run again.  How can reset the database to allow me to run
> > again?  or what commands would reset the database like shutting delphi
> > down does?

> No simple answers to these questions.

> I would suggest that you alter your code so that it checks to see if the
> record already exists. I usually code stuff like that something like

> if aTable.Locate ('PrimaryKeyField1, . . .', VarArrayOf
> ([inputValueForPrimaryKeyField1, . . .]), [])
> then aTable.Edit
> else aTable.Insert;
> // move all the data
> aTable.Post;

> This takes care of the problem with duplicate primary keys. However if you
> have secondary indexes that are also unique you are going to have to do more
> checking.

> I've never had a problem with a db program crashing and forcing me to close
> Delphi in order to re-run the program. Are you opening tables with Exclusive
> true?

I found a suitable loop hole that I can use.  Since I have to load my
database records in from a file everytime because the database
spontaneously empties itself for no apparent reason, I am emptying the
database when I exit the program.  To get around the 'Key Violation'
error, I empty the database again before I try to load in the records.
 Works nicely.  I added in the code when my program did crash and
generated the error and the program started up again normally.

Thanks for the help
Ghost

PS. Would you know why a database would empty itself periodically for
no reason?  I can't seem to get it to do it to try and stop it, but it
does it from time to time and I certaninly can't have that happening
as I'm sure you know.

Re:'Key violation'


Quote
"Ghost" <jeremy_gilbe...@hotmail.com> wrote in message

news:55f559cc.0306260627.1f4364c@posting.google.com...

Quote
> PS. Would you know why a database would empty itself periodically for
> no reason?  I can't seem to get it to do it to try and stop it, but it
> does it from time to time and I certaninly can't have that happening
> as I'm sure you know.

If you are using Paradox tables and Local Share is not set to true then the
BDE caches an awful lot of data when a program is running. This data can be
lost if the db is not closed properly, i.e. a program blows up on a BDE
error. Set Local Share (see BDE Administrator in the Control Panel) to True.

Re your question about nested locates. Sure, make sure that Complete Boolean
Evaluation is OFF and do something like

if aTable.Locate ('fields in primary index', . . .) or
  aTable.Locate ('fields in 1st unique secondary index', . . .) or . . .
then aTable.Edit
else aTable.Insert;

Re:'Key violation'


Quote
"Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message <news:QNGKa.300$CK.11928@news20.bellglobal.com>...
> "Ghost" <jeremy_gilbe...@hotmail.com> wrote in message
> news:55f559cc.0306260627.1f4364c@posting.google.com...

> > PS. Would you know why a database would empty itself periodically for
> > no reason?  I can't seem to get it to do it to try and stop it, but it
> > does it from time to time and I certaninly can't have that happening
> > as I'm sure you know.

> If you are using Paradox tables and Local Share is not set to true then the
> BDE caches an awful lot of data when a program is running. This data can be
> lost if the db is not closed properly, i.e. a program blows up on a BDE
> error. Set Local Share (see BDE Administrator in the Control Panel) to True.

By setting it to true, what does that do?  Does it prevent the BDE
from caching all the information?  So when (and it will happen) I blow
up my program again, what's from stopping it from screwing up my
tables?  The info on it only says that if I need to access local data
between a BDE and a non-BDE application to set it to true.

Quote
> Re your question about nested locates. Sure, make sure that Complete Boolean
> Evaluation is OFF and do something like

> if aTable.Locate ('fields in primary index', . . .) or
>   aTable.Locate ('fields in 1st unique secondary index', . . .) or . . .
> then aTable.Edit
> else aTable.Insert;

Where is Complete Boolean Evaluation?

Re:'Key violation'


"Ghost" <jeremy_gilbe...@hotmail.com> skrev i melding
news:55f559cc.0306270433.7696848a@posting.google.com...
Quote
> "Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message

<news:QNGKa.300$CK.11928@news20.bellglobal.com>...

Quote

> > Re your question about nested locates. Sure, make sure that Complete
Boolean
> > Evaluation is OFF and do something like

> > if aTable.Locate ('fields in primary index', . . .) or
> >   aTable.Locate ('fields in 1st unique secondary index', . . .) or . . .
> > then aTable.Edit
> > else aTable.Insert;

> Where is Complete Boolean Evaluation?

Project | Options | Compiler

--
Regards,

Bj?rge S?ther
bjorge@haha_itte.no
-------------------------------------
I'll not spend any money on American Software products
until armed forces are out of Iraq.

Re:'Key violation'


"Ghost" <jeremy_gilbe...@hotmail.com> skrev i melding
news:55f559cc.0306260627.1f4364c@posting.google.com...

Quote
> PS. Would you know why a database would empty itself periodically for
> no reason?  I can't seem to get it to do it to try and stop it, but it
> does it from time to time and I certaninly can't have that happening
> as I'm sure you know.

Have you turned "Periodical deletes" off ?

;-)

I would have a look at indexes. If a table that had plenty of records
yesterday suddenly seems empty, I would suspect primary key index to be
corrupt. If you open the database file in a hex vewer, you'll probably see
that data is still there.

--
Regards,

Bj?rge S?ther
bjorge@haha_itte.no
-------------------------------------
I'll not spend any money on American Software products
until armed forces are out of Iraq.

Re:'Key violation'


The F1 key and Help menus are your friends.

Re:'Key violation'


Quote
"Bj?rge S?ther" <bjorge@hahaha_itte.no> wrote in message <news:OiXKa.9472$Hb.164598@news4.e.nsc.no>...
> "Ghost" <jeremy_gilbe...@hotmail.com> skrev i melding
> news:55f559cc.0306260627.1f4364c@posting.google.com...
> > PS. Would you know why a database would empty itself periodically for
> > no reason?  I can't seem to get it to do it to try and stop it, but it
> > does it from time to time and I certaninly can't have that happening
> > as I'm sure you know.

> Have you turned "Periodical deletes" off ?
> ;-)

Cute answer...

Quote

> I would have a look at indexes. If a table that had plenty of records
> yesterday suddenly seems empty, I would suspect primary key index to be
> corrupt. If you open the database file in a hex vewer, you'll probably see
> that data is still there.

But if that is the case, and the data is still technically there in
the hex viewer, what good does that do me if it's not in the database
which in turn means that it is not in my program?

Re:'Key violation'


Quote
Ghost wrote:
> But if that is the case, and the data is still technically there in
> the hex viewer, what good does that do me if it's not in the database
> which in turn means that it is not in my program?

It means it's time to break out the table repair utility!

--
jc

Other Threads