1

i develop application mobile where i use framework phonegap, and i use to insert data into local data base my question is how do to generate a uniqueidentifier for sqlite with javascript?

    CREATE TABLE foobar (id uniqueidentifier, foo text, bar text, PRIMARY 
    KEY (id)); 

    insert into foobar values (newid(), "Aaa", "Bbb"); 



    id                                        foo      bar 
    ___________________________________________________________ 

    {00000109-0000-0010-8000-00AA006D2EA4}    "Aaa"    "Bbb" 
B.M.A
  • 175
  • 1
  • 1
  • 13
  • What's the deal with that 8-4-4-4-12 GUID format? Seen it a couple of times already, looks pretty arbitrary (edit -> Got that: RFC 4122) – Kos Dec 12 '12 at 09:59
  • 2
    Check http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript out to generate UID – Jonathan de M. Dec 12 '12 at 09:59
  • @JonathandeM. big thks, but i'll insert into database, then you think we can not even have the key confusion? – B.M.A Dec 12 '12 at 10:03
  • quasi-impossible, there is one chance on 340,282,366,920,938,463,463,374,607,431,768,211,456 to create twice the same UID. – Jonathan de M. Dec 13 '12 at 01:41
  • Generating a unique id on the client side and then using it in your database doesn't sound very safe =/ – Ja͢ck Dec 13 '12 at 02:12

1 Answers1

0

You have differents choices:

  1. Take the compacted version by Broofa
  2. Take this version which add uuid method to Math object. Then just call Math.uuid() to generate a random UUID, also by Broofa.
  3. A complete library for multiple operation on the uuid, for exemple you could use the version 4 (random-numbers-based) as well as the version 1(time-based).

To finish, as said in the comment there are 340,282,366,920,938,463,463,374,607,431,768,211,456 possible UUIDs, so don't worry about generating twice the same, you would have more chance trying to win lottery 1 billion times.

Community
  • 1
  • 1
Jonathan de M.
  • 9,721
  • 8
  • 47
  • 72