go和delphi对数据库查询数据基于结构的遍历
go和delphi对数据库查询数据基于结构的遍历
在结构面前,go的rows,delphi的Tdataset,都是和谐的。特意整理了2篇GO和DELPHI的对比,试图提示跨语言、平台 数据标准的真像。
GO没有Tdataset,但有rows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 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; |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/16502971.html
分类:
google protobuf
, go语言
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-07-21 google protobuf经验
2014-07-21 数据同步存储过程