Just a thought. I want to insert unique code to the database. So let's say, my code would be from 0 to 9; 2-digit code. (just an example) And set the code field to unique.
I have here a loop flow.
This approach:
loop
digit_code = generate_random_code()
bool = insert_to_database(digit_code) // insert to db; returns boolean
if bool // if successfully inserted
break
endloop
OR this:
rows = get_all_code() // retrieves all data from db
digit_code = generate_random_code()
loop rows
if digit_code is not in rows
insert_to_database(digit_code)
break
endloop
So basically, the task is simple, generate unique code for each transaction. Now what would be a better approach? Repeat insert until success or go to db once (to get all data) then do an insert. Or if you have any great suggestion. I'm happy to take them. Just wondering. Thanks.