ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My DB doesn't allow duplicates. But I need, rather than psedo-random numbers, to generate a 32-bit unique ID on Linux. I know someone has done this before. Does anyone know the link(s) of this software or of the similar kind?
Thanks
Jack
Last edited by lucky6969b; 03-25-2006 at 03:44 AM.
How unique does it have to be (i.e. unique within a single system or unique among a set of systems)?
The simplest method: associate a counter with your database and increment it whenever you read its value. If you modify this counter from several concurrent threads, you have to lock it on every modification.
Which database are you using? Most databases (RDBMS's like mySQL, pgSQL, Oracle and MS-SQL) have an "auto increment" attribute that will automatically generate a unique primary key value for you each time you insert a new record.
Otherwise, maybe the easiest technique is simply to store your "last index" somewhere (an external text file; maybe even a table in your database itself), and then increment "last index + 1" as part of your standard "insert".
'Hope that helps .. PSM
PS:
Please do consider building a small "project library" and please do consider the books I mentioned in reply to your post last night!
PPS:
A proprietary system I once worked on had a custom database called "MKF" ("Multi-Keyed Files"; it was designed and invented in the mid 80's, around the time relational databases were beginning to take off), and it had a table called "scalar_numbers". Which just stored the current value of things like "the current highest document ID". It served precisely the kind of purpose you're looking for...
Distribution: Slackware & Slamd64. What else is there?
Posts: 1,705
Rep:
Quote:
Originally Posted by lucky6969b
My DB doesn't allow duplicates. But I need, rather than psedo-random numbers, to generate a 32-bit unique ID on Linux. I know someone has done this before. Does anyone know the link(s) of this software or of the similar kind?
Thanks
Jack
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.