![]() |
|
|||||||
| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
| Questions & Help Needed Post any question regarding Basic4ppc. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
If field1 is set as the PRIMARY KEY or a UNIQUE column, then it will not accept duplicate values. Personally I think it is best practice to test for duplicates before attempting an insert, so that you can nicely control the action. This is easily done with a small select:
cmd.CommandText = "SELECT ROWID from My_table WHERE Name = '" & txtName.Text & "'" reader.Value = cmd.ExecuteReader havedup = reader.ReadNextRow reader.Close If havedup Then Msgbox(txtName.Text & " already exists!" & crlf & "Must be unique.",,cMsgBoxHand) Return End If Geoff. |
|
|||
|
I'll try to explain (sorry about the delay - been busy with other things).
A column may be defined as either the PRIMARY KEY or as UNIQUE. This is usually done when you create the table, either by an SQL command from B4ppc or with an external utility. This column will then not accept duplicate values. If you try to insert record where the column value already exist, an "error" message will be produced - something like "Abort due to constraint violation column field1 is not unique". In my opinion, it is best to handle this event before the error occurs, by testing for the value before attempting any insert - as in the sample code. Your program can then take the appropriate action. This test could be done regardless of how the column is defined. Hope that helps, Geoff. |
|
|||
|
Thanks Gianni, an excellent example.
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 2 sql database | gjoisa | Questions & Help Needed | 1 | 07-01-2008 10:06 AM |
| About SQL Database | gjoisa | Questions & Help Needed | 12 | 02-23-2008 06:27 AM |
| how to create a database ? | gjoisa | Questions & Help Needed | 27 | 02-14-2008 05:38 PM |
| UTF-8 Database | EdQas | Questions & Help Needed | 5 | 10-16-2007 05:11 PM |
| How to convert an Access database to a sqlite database | Peter | Code Samples & Tips | 5 | 07-24-2007 03:20 PM |