当前位置:Gxlcms > 数据库问题 > xorm使用pgsql的例子

xorm使用pgsql的例子

时间:2021-07-01 10:21:17 帮助过:12人阅读

Navicat Premium Data Transfer Source Server : localhost Source Server Type : PostgreSQL Source Server Version : 90401 Source Host : localhost Source Database : mmc Source Schema : public Target Server Type : PostgreSQL Target Server Version : 90401 File Encoding : utf-8 Date: 08/15/2015 20:24:56 PM */ -- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS "public"."student"; CREATE TABLE "public"."student" ( "id" int4 NOT NULL DEFAULT nextval(student_id_seq::regclass), "name" varchar(255) NOT NULL DEFAULT ‘‘::character varying COLLATE "default", "age" int4 NOT NULL DEFAULT 0 ) WITH (OIDS=FALSE); ALTER TABLE "public"."student" OWNER TO "mmc"; -- ---------------------------- -- Records of student -- ---------------------------- BEGIN; INSERT INTO "public"."student" VALUES (1, tom, 25); COMMIT; -- ---------------------------- -- Primary key structure for table student -- ---------------------------- ALTER TABLE "public"."student" ADD PRIMARY KEY ("id") NOT DEFERRABLE INITIALLY IMMEDIATE;

 

直接贴代码

  1. <span style="color: #000000;">package main
  2. import (
  3. </span><span style="color: #800000;">"</span><span style="color: #800000;">fmt</span><span style="color: #800000;">"</span>
  4. <span style="color: #800000;">"</span><span style="color: #800000;">github.com/go-xorm/xorm</span><span style="color: #800000;">"</span><span style="color: #000000;">
  5. _ </span><span style="color: #800000;">"</span><span style="color: #800000;">github.com/lib/pq</span><span style="color: #800000;">"</span>
  6. <span style="color: #800000;">"</span><span style="color: #800000;">reflect</span><span style="color: #800000;">"</span>
  7. <span style="color: #800000;">"</span><span style="color: #800000;">unsafe</span><span style="color: #800000;">"</span><span style="color: #000000;">
  8. )
  9. </span><span style="color: #0000ff;">var</span> engine *<span style="color: #000000;">xorm.Engine
  10. func BytesToString(b []</span><span style="color: #0000ff;">byte</span>) <span style="color: #0000ff;">string</span><span style="color: #000000;"> {
  11. bh :</span>= (*reflect.SliceHeader)(<span style="color: #0000ff;">unsafe</span>.Pointer(&<span style="color: #000000;">b))
  12. sh :</span>=<span style="color: #000000;"> reflect.StringHeader{bh.Data, bh.Len}
  13. </span><span style="color: #0000ff;">return</span> *(*<span style="color: #0000ff;">string</span>)(<span style="color: #0000ff;">unsafe</span>.Pointer(&<span style="color: #000000;">sh))
  14. }
  15. func StringToBytes(s </span><span style="color: #0000ff;">string</span>) []<span style="color: #0000ff;">byte</span><span style="color: #000000;"> {
  16. sh :</span>= (*reflect.StringHeader)(<span style="color: #0000ff;">unsafe</span>.Pointer(&<span style="color: #000000;">s))
  17. bh :</span>= reflect.SliceHeader{sh.Data, sh.Len, <span style="color: #800080;">0</span><span style="color: #000000;">}
  18. </span><span style="color: #0000ff;">return</span> *(*[]<span style="color: #0000ff;">byte</span>)(<span style="color: #0000ff;">unsafe</span>.Pointer(&<span style="color: #000000;">bh))
  19. }
  20. func main() {
  21. engine, _ </span>= xorm.NewEngine(<span style="color: #800000;">"</span><span style="color: #800000;">postgres</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">user=mmc password=mmc dbname=mmc host=127.0.0.1 port=5432 sslmode=disable</span><span style="color: #800000;">"</span><span style="color: #000000;">)
  22. sql :</span>= <span style="color: #800000;">"</span><span style="color: #800000;">select * from student</span><span style="color: #800000;">"</span><span style="color: #000000;">
  23. rowArray, _ :</span>=<span style="color: #000000;"> engine.Query(sql)
  24. </span><span style="color: #0000ff;">for</span> _, row :=<span style="color: #000000;"> range rowArray {
  25. </span><span style="color: #0000ff;">for</span> colName, colValue :=<span style="color: #000000;"> range row {
  26. value :</span>=<span style="color: #000000;"> BytesToString(colValue)
  27. fmt.Println(colName, value)
  28. }
  29. }
  30. }</span>

 

xorm使用pgsql的例子

标签:

人气教程排行