[Solved] Database Errror handling

Post

Posted
Rating:
#1 (In Topic #1387)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 Hi All you experts,

Could someone please point me in the correct direction so I can sort my issue out.

My Issue is I am not sure how I handle errors coming from the database (example would be duplicate record inserts)

At the moment i am importing some data from a customer into a MySQL database and I have a few duplicated files but I can not figure out
how I can send the duplicated barcode number to another file and then move onto the next import

I known it is a duplicate as MySQL database has the primary set on the barcode and this is also set to not allow duplicate information

I would like to know if it is possible to capture this error and handle it gracefully and not have my entire app crash.

Kind regards to you all

Andy
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
This will depend on how many records you process. I've made the subroutine as brief as possible but it will have an effect on performance, but essentially:
<LIST>
  • <LI>Read each record in</LI>
</LIST>
<LIST>
  • <LI>Check it for an existing duplicate. (with the provided subroutine)</LI>
</LIST>
<LIST>
  • <LI>On Exit then:
    <LIST>
    • <LI>Update Duplicate Counter</LI>
    </LIST>
    <LIST>
    • <LI>Write the duplicate record to a log file.</LI>
    </LIST></LI>
</LIST>
<LIST>
  • <LI>NOT exit then add new record</LI>
</LIST>

Code (gambas)

  1. Public Function CheckDuplicateId(InId as string) as Integer
  2. dim RetCount as Integer = 0
  3. $Rec = MyConnection.Exec("SELECT BarcodeFieldName FROM MySQLTable WHERE(BarcodeFieldName = '" & InId & "')")
  4. RetCount = $Rec.Count
  5. Return RetCount
  6.  

The program flow would flow would be to check for a duplicate record by passing the input barcode to the subroutine.
If it returns zero then update you new record.
If it is non-zero then increment your duplicate counter and write to a log file to review later as required.
Process the next record or end.

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

Quincunxian said

This will depend on how many records you process. I've made the subroutine as brief as possible but it will have an effect on performance, but essentially:
<LIST>
  • <LI>Read each record in</LI>
</LIST>
<LIST>
  • <LI>Check it for an existing duplicate. (with the provided subroutine)</LI>
</LIST>
<LIST>
  • <LI>On Exit then:
    <LIST>
    • <LI>Update Duplicate Counter</LI>
    </LIST>
    <LIST>
    • <LI>Write the duplicate record to a log file.</LI>
    </LIST></LI>
</LIST>
<LIST>
  • <LI>NOT exit then add new record</LI>
</LIST>

Code (gambas)

  1. Public Function CheckDuplicateId(InId as string) as Integer
  2. dim RetCount as Integer = 0
  3. $Rec = MyConnection.Exec("SELECT BarcodeFieldName FROM MySQLTable WHERE(BarcodeFieldName = '" & InId & "')")
  4. RetCount = $Rec.Count
  5. Return RetCount
  6.  

The program flow would flow would be to check for a duplicate record by passing the input barcode to the subroutine.
If it returns zero then update you new record.
If it is non-zero then increment your duplicate counter and write to a log file to review later as required.
Process the next record or end.

Hi Quincunxian,
Thank For this subroutine I have added it to my import functions and it has worked a treat.
Online now: No Back to the top
1 guest and 0 members have just viewed this.