• go和delphi对数据库查询数据基于结构的遍历


    go和delphi对数据库查询数据基于结构的遍历

    在结构面前,go的rows,delphi的Tdataset,都是和谐的。特意整理了2篇GO和DELPHI的对比,试图提示跨语言、平台 数据标准的真像。

    GO没有Tdataset,但有rows

    func UnitsQryPB(w http.ResponseWriter, r *http.Request) {
    	// /pb/{service}/units/qry/{dbid}
    	url := strings.Split(r.URL.Path, "/")
    	dbid := url[5]
    	db := getDB(dbid)
    	if db == nil {
    		return
    	}
    	sql := "select * from tunit"
    	rows, err := db.Query(sql)
    
    	defer rows.Close()
    	defer db.Close()
    
    	if err != nil {
    		public.Log(err)
    		return
    	}
    	var arr Unitss
    	for rows.Next() {
    		var dw Units
    		rows.Scan(&dw.Unitid, &dw.Unitname)
    		arr.UnitsArr = append(arr.UnitsArr, &dw)
    	}
    	data, _ := proto.Marshal(&arr)
    	if public.UseGzip {
    		data, _ = public.GZIPEn(data)
    	}
    	w.Write(data)
    }
    

      DELPHI没有rows,但有Tdataset

    function select(url: string; body: TBytes): TBytes;
    var
      db: tdb;
      pool: tdbpool;
      arr: TArray<string>;
      serial: TgoProtocolBuffer;
      rows: TtunitArray;
      i: integer;
      res: TRes;
    begin
      serial := TgoProtocolBuffer.Create;
      try
        try
          arr := url.Split(['/']);
          pool := GetDBPool(arr[4]);
          db := pool.Lock;
          db.qry.Close;
          db.qry.SQL.Clear;
          var where: string := '';
          if high(arr) >= 5 then
            where := ' where ' + TNetEncoding.URL.Decode(arr[5]);
          db.qry.SQL.Text := 'select * from tunit' + where;
          db.qry.Open;
          if db.qry.isempty then
          begin
            rows.status := 0;
            rows.exception := 'No found any data.';
            result := serial.Serialize<TtunitArray>(rows);
            exit;
          end;
          SetLength(rows.tunits, db.qry.RecordCount);
          db.qry.First;
          i := 0;
          while not db.qry.Eof do
          begin
            rows.tunits[i].unitid := db.qry.fieldbyname('unitid').asstring;
            rows.tunits[i].unitname := db.qry.fieldbyname('unitname').asstring;
            rows.status := 1;
            rows.message := 'success';
            inc(i);
            db.qry.Next;
          end;
          result := serial.Serialize<TtunitArray>(rows);
        except
          on E: Exception do
          begin
            res.status := 0;
            res.exception := E.message;
            result := serial.Serialize<TRes>(res);
          end;
        end;
      finally
        pool.Unlock(db);
        serial.Free;
      end;
    end;
    

      

  • 相关阅读:
    Linux PHP连接MSSQL
    Curl参数一览
    [android开发必备] Android开发者社区汇总
    android定时器
    一个mysql小技巧
    php empty问题
    周报_2012第16周(2012/04/152012/04/21)
    周报_2012第17周(2012/04/222012/04/28)
    周报_2013第04周(2013/01/202013/01/26)
    周报_2013第01周(2012/12/302012/01/05)
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/16502971.html
Copyright © 2020-2023  润新知