如果是,则SQLITE中是否存在UID数据类型,然后如何为其生成值 [英] Is there UID datatype in SQLITE if Yes then how to generate value for that

查看:153
本文介绍了如果是,则SQLITE中是否存在UID数据类型,然后如何为其生成值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建这样的表:

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

如何在表foobar中为id字段插入或生成值?

How to insert or generate value for id field in table foobar?

推荐答案

您可以说SQLite根本不支持数据 types .例如,在SQLite3中,您可以执行此操作.

You can argue that SQLite doesn't support data types at all. In SQLite3, you can do this, for example.

sqlite> create table test (id wibblewibble primary key);

SQLite将愉快地创建一个带有数据类型" wibblewibble的列. SQLite还将愉快地使用uuid,guid和SuperChicken等数据类型"创建列.

SQLite will happily create a column with the "data type" wibblewibble. SQLite will also happily create columns with the "data types" uuid, guid, and SuperChicken.

对您来说,关键点可能是如何自动生成uid. SQLite在那里帮不了您什么.

The crucial point for you is probably how to automatically generate a uid. SQLite can't help you much there.

您可以完全将其保留给客户端程序.如果您使用python编程,请使用 uuid模块.在ruby中,您具有 SecureRandom.uuid函数.其他语言也具有类似的功能或解决方法.

You can leave it entirely up to the client program. If you're programming in python, use the uuid module. In ruby, you have the SecureRandom.uuid function. Other languages have similar features or workarounds.

您可以在C语言中编写自己的uid生成函数.(请参见创建或重新定义SQL函数.)我将此称为相对极端的方法.

You can write your own uid-generating function in C. (See Create or Redefine SQL Functions.) I'd call this a relatively extreme approach.

您可以将其存储在二进制文件

You can store it in either binary or text format.

在线上的其他对话表明,对于UUID 是什么存在广泛的误解. UUID不仅仅是一个128位随机数. UUID具有结构和规则.参见 RFC 4122 .

Other conversations online suggest that there's a widespread misunderstanding about what a UUID is. A UUID is not simply a 128-bit random number. A UUID has structure and rules. See RFC 4122.

这篇关于如果是,则SQLITE中是否存在UID数据类型,然后如何为其生成值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆