Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

SQLite – Auto-Increment / Auto Generate GUID

Posted on December 19, 2014May 13, 2016 By David Kittell

Recently was asked if it’s possible to create an auto-incrementing GUID in SQLite.

Here is one approach:

In Firefox there is an add-on called SQLite Manager (also available at https://code.google.com/p/sqlite-manager/)

  1. Open Firefox, in the “Tools” menu select “SQLite Manager”
  2. With SQLite Manager open click on the “Database” menu and select “New Database”
  3. Type in the database name and click the “OK” button
  4. Select the folder you want the database to go in and then click the “Select Folder” button
  5. Click on the “Execute SQL” tab
  6. Paste the code below in the “Enter SQL” window and click on the “Run SQL” button.
    CREATE TABLE tblUsers (
    
    	UserAccountID CHAR(36) PRIMARY KEY,
    	firstname VARCHAR(100),
    	lastname VARCHAR(100)
    	);
    
  7. Clear the “Enter SQL” window and then paste the code below and click on the “Run SQL” button.
    CREATE TRIGGER AutoGenerateGUID
    AFTER INSERT ON tblUsers
    FOR EACH ROW
    WHEN (NEW.UserAccountID IS NULL)
    BEGIN
       UPDATE tblUsers SET UserAccountID = (select hex( randomblob(4)) || '-' || hex( randomblob(2))
                 || '-' || '4' || substr( hex( randomblob(2)), 2) || '-'
                 || substr('AB89', 1 + (abs(random()) % 4) , 1)  ||
                 substr(hex(randomblob(2)), 2) || '-' || hex(randomblob(6)) ) WHERE rowid = NEW.rowid;
    END;
    
  8. Clear the “Enter SQL” window and then paste the code below and click on the “Run SQL” button. Now when you do an insert like below it will generate a GUID automatically
    INSERT INTO tblUsers (
    	UserAccountID,
    	firstname,
    	lastname
    	)
    VALUES (
    	NULL,
    	"David",
    	"Kittell"
    	)
    
    

Resulting INSERT looks like this:
UserAccountID: E2D37368-7E05-40DC-9BA3-04B2156CA598
firstname: David
lastname: Kittell

Originally Posted on December 19, 2014
Last Updated on May 13, 2016
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code SQL SQLite SQLite

Post navigation

Previous post
Next post

Related Posts

PHP – Friendly File Size

Posted on August 15, 2013October 10, 2019

function GetFriendlySize($s) { if ($s <= 1024) return $s.’ bytes’; else if ($s <= 1048576) $s = ($s/1024).’ kilobytes’; else if ($s <= 11559501824) $s = ($s/1048576).’ megabytes’; else if ($s <= 11836929867776) $s = ($s/11559501824).’ gigabytes’; else if ($s <= 12121016184602624) $s = ($s/11836929867776).’ terabytes’; else if ($s <=…

Read More

PowerShell – Ping Subnet

Posted on November 9, 2015

#requires -Version 2.0 Function Ping-Subnet { #.Synopsis # Ping a subnet returning all alive hosts. #.Example # Ping-Subnet -IP 192.168.1.0 -Netmask /24 #.Example # Ping-Subnet -IP 192.168.1.128 -Netmask 255.255.255.128 Param( [string] $IP, [string] $netmask ) Begin { $IPs = New-Object System.Collections.ArrayList $Jobs = New-Object System.Collections.ArrayList $max = 50 Function Get-NetworkAddress…

Read More

PowerShell – Create System Path

Posted on March 4, 2016March 4, 2016

Possibly the easiest manual way is in Windows 10 and Windows 2012 R2 (likely previous versions too but don’t have them handy while writing this): From the Desktop, right click on the Windows icon (Start Menu) in the bottom left of the screen Click on “System” Click the “Advanced system…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - IIS Remove Site
  • SQLite - Auto-Increment / Auto Generate GUID
  • PowerShell - FTP Upload Directory With Sub-Directories
  • Raspberry Pi - Remove Default Apps
  • PowerShell - Change Windows CD/DVD Drive Letter
©2025 David Kittell | WordPress Theme by SuperbThemes