Skip to main content

How to Create UUID in SQLite

Complete guide for SQLite 3.38+ and older versions

How to Create UUID in SQLite. Use randomblob() and hex() to generate a SQLite UUID. Older servers need an extension before any generation function works, and a postgresql uuid page lists those alternatives.

Generate UUID for SQLite

SELECT uuid();
550e8400-e29b-41d4-a716-446655440000

Unlike PostgreSQL or SQL Server, SQLite has no built-in UUID generation function — there's no UUID() or gen_random_uuid() waiting for you in the core engine. That's not really a problem: you either generate the UUID in your application code before inserting it as TEXT, or build one directly with a well-known SQL expression using randomblob() and hex(). Below you'll find copy-paste ready examples for creating a SQLite UUID, storing it efficiently, and the schema-level alternatives SQLite offers instead.

How to Generate a UUID in SQLite

SQLite core ships with no UUID() or gen_random_uuid() equivalent, so you have two honest options: generate the UUID in application code before inserting it, or build one directly with a pure-SQL expression. Oracle has no dedicated type, so the oracle uuid guide uses a 16-byte raw column instead.

sqlite3
# Recommended: generate the UUID in application code, then insert it as TEXT.
# Python's sqlite3 and uuid modules are both in the standard library --
# no extra packages required.
import sqlite3
import uuid

conn = sqlite3.connect('app.db')
conn.execute(
    'INSERT INTO users (id, email) VALUES (?, ?)',
    (str(uuid.uuid4()), 'jane@example.com')
)
conn.commit()
sqlite3
-- Pure-SQL alternative: a well-known community pattern that builds a
-- v4-shaped UUID directly with randomblob() and hex(), no app code needed
SELECT lower(
    hex(randomblob(4)) || '-' ||
    hex(randomblob(2)) || '-4' ||
    substr(hex(randomblob(2)), 2) || '-' ||
    substr('89ab', abs(random()) % 4 + 1, 1) ||
    substr(hex(randomblob(2)), 2) || '-' ||
    hex(randomblob(6))
);
-- Output: 7e57ac1d-9b3f-4a2e-8f1c-6d4b8a9e0f12

Explanation

Convert a String to a UUID in SQLite

SQLite is dynamically typed with no dedicated UUID column type, so a UUID received as a string — from an API request or another system — is simply stored as TEXT, or converted to BLOB when storage efficiency matters more than readability. When two values must be compared by content rather than by reference, a java uuid snippet parses a hyphenated string into an object first.

sqlite3
-- Simplest: store the UUID as TEXT, no conversion needed
CREATE TABLE users (
    id TEXT PRIMARY KEY,
    email TEXT NOT NULL
);

INSERT INTO users (id, email)
VALUES ('550e8400-e29b-41d4-a716-446655440000', 'jane@example.com');

SELECT * FROM users WHERE id = '550e8400-e29b-41d4-a716-446655440000';
sqlite3
-- Storage-efficient alternative: convert to 16 raw bytes and store as BLOB
CREATE TABLE users_compact (
    id BLOB PRIMARY KEY,
    email TEXT NOT NULL
);

-- unhex() is available in SQLite 3.41+; strip the hyphens first
INSERT INTO users_compact (id, email)
VALUES (
    unhex(replace('550e8400-e29b-41d4-a716-446655440000', '-', '')),
    'jane@example.com'
);

-- Convert back to the readable string form on read
SELECT lower(hex(id)) FROM users_compact;

Explanation

Other Options for Generating UUIDs in SQLite

Beyond application-side generation and the pure-SQL v4 expression, SQLite supports a column default that produces a random value automatically on every INSERT, without touching application code at all.

sqlite3
-- SQLite 3.38+: a simple random-value default, generated on every INSERT
CREATE TABLE sessions (
    id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
    user_id TEXT NOT NULL,
    expires_at DATETIME
);

-- Insert without specifying id -- SQLite fills it in automatically
INSERT INTO sessions (user_id, expires_at)
VALUES ('u_042', datetime('now', '+1 day'));

SELECT id FROM sessions LIMIT 1;
-- Output: 3f9a7c21e8b4419fa02d6c7e1b5f8a90

Explanation

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the UUID generators, validator, decoder and the language and database UUID guides on UUID Generator.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Every generator is checked against the RFC 4122 and RFC 9562 layouts and every code example is run before it goes live.
  • Published Updated

Vinitha Pu on Behance · Vinitha Pu on LinkedIn

Comments & Feedback

Share your experience or ask questions about this tool

Copied!