Postgresql: UUID的使用

方法1:使用create extension命令

win7需要通过管理员权限打开pgAdmin,输入命令:

create extension "uuid-ossp"

安装扩展成功以后,就可以通过uuid_generate_v4()或uuid_generate_v1()查询



方法2:

默认安装的 Postgresql 是不带 UUID 函数的,为了生成一个 UUID,我们不得不在客户端生成。其实在 Postgresql 的安装目录下已经存在这样的函数定义,我们只需将其 import 进来即可。

 

在安装目录的 share/contrib 下面,可以找到 uuid-ossp.sql,使用以下命令:

[c-sharp]  view plain copy print ?
  1. psql -d pisces -U postgres -f D:/Service/PostgreSQL/9.0/share/contrib/uuid-ossp.sql  

 

 

参数说明:

-d: 数据库名

-U: 用户名

-f: 要import的文件

 

以下截屏说明导入正确:

 

然后我们可以尝试用导入的函数生成一个UUID:

[c-sharp]  view plain copy print ?
  1. select uuid_generate_v4();  

 

 

更多uuid函数,请在postgresql文档中搜索:

uuid-ossp

在C#中使用uuid类型的例子:

 

[c-sharp]  view plain copy print ?
  1. //---------------------------------------------------------------------------  
  2. // <copyright file="Program.cs" company="Xinsoft.org">  
  3. //     Copyright (c) Yaping Xin. All rights reserved.  
  4. // </copyright>  
  5. // <Description>Entry class for the testing.</Description>  
  6. //---------------------------------------------------------------------------  
  7. namespace Org.Xinsoft.Demo.Postgresql  
  8. {  
  9.     using System;  
  10.     using System.Data;  
  11.     using Npgsql;  
  12.     /// <summary>  
  13.     /// Entry class for the testing.  
  14.     /// </summary>  
  15.     internal class TestUuidProgram  
  16.     {  
  17.         /// <summary>  
  18.         /// Entry point for the testing.  
  19.         /// </summary>  
  20.         /// <param name="args">console arguments</param>  
  21.         internal static void Main(string[] args)  
  22.         {  
  23.             using (DatabaseOperation db = new DatabaseOperation())  
  24.             {  
  25.                 db.OpenConnection();  
  26.                 Console.WriteLine(db.ConnState);  
  27.                 string sql = "insert into demotable(id, name) values(@id, @name);";  
  28.                 using (Npgsql.NpgsqlCommand command = db.Connection.CreateCommand())  
  29.                 {  
  30.                     command.CommandText = sql;  
  31.                     command.CommandType = CommandType.Text;  
  32.                     command.Parameters.Add("@id", NpgsqlTypes.NpgsqlDbType.Uuid);  
  33.                     command.Parameters.Add("@name", NpgsqlTypes.NpgsqlDbType.Varchar, 25);  
  34.                     command.Prepare();  
  35.                     for (int i = 0; i < 10; i++)  
  36.                     {  
  37.                         command.Parameters[0].Value = Guid.NewGuid();  
  38.                         command.Parameters[1].Value = string.Format("{0} {1}", i, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));  
  39.                         int effects = command.ExecuteNonQuery();  
  40.                         Console.WriteLine(string.Format("INSERT: {0} rows effected.", effects));  
  41.                     }  
  42.                 }  
  43.                 sql = "select id, name from demotable;";  
  44.                 using (Npgsql.NpgsqlCommand command = db.Connection.CreateCommand())  
  45.                 {  
  46.                     command.CommandText = sql;  
  47.                     command.CommandType = CommandType.Text;  
  48.                     command.Prepare();  
  49.                     DataTable result = DatabaseHelper.GetDataTable(command.ExecuteReader());  
  50.                     int rows = result.Rows.Count;  
  51.                     int cols = result.Columns.Count;  
  52.                     Console.WriteLine("Rows: {0}", rows);  
  53.                     for (int i = 0; i < cols; i++)  
  54.                     {  
  55.                         Console.WriteLine("Column[{0}]: {1}/t| {2}", i, result.Columns[i].ColumnName, result.Columns[i].DataType.Name);  
  56.                     }  
  57.                     Console.WriteLine("Query result:");  
  58.                     for (int i = 0; i < rows; i++)  
  59.                     {  
  60.                         DataRow row = result.Rows[i];  
  61.                         for (int j = 0; j < cols; j++)  
  62.                         {  
  63.                             Console.Write("{0}/t", row[j].ToString());  
  64.                         }  
  65.                         Console.WriteLine(string.Empty);  
  66.                     }  
  67.                 }  
  68.                 sql = "delete from demotable;";  
  69.                 using (Npgsql.NpgsqlCommand command = db.Connection.CreateCommand())  
  70.                 {  
  71.                     command.CommandText = sql;  
  72.                     command.CommandType = CommandType.Text;  
  73.                     int effects = command.ExecuteNonQuery();  
  74.                     Console.WriteLine(string.Format("DELETE: {0} rows effected.", effects));  
  75.                 }  
  76.                 db.CloseConnection();  
  77.             }  
  78.         }  
  79.     }  
  80. }  

 

上述例子用到的表结构如下:

 

[c-sharp]  view plain copy print ?
  1. CREATE TABLE demotable  
  2. (  
  3.   id uuid NOT NULL,  
  4.   "name" character varying(25),  
  5.   CONSTRAINT pk_demotable PRIMARY KEY (id)  
  6. )  
  7. WITH (  
  8.   OIDS=FALSE  
  9. );  
  10. ALTER TABLE demotable OWNER TO agentusr;  

方法2引用:http://blog.csdn.net/yapingxin/article/details/6365055

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值