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;
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/16502971.html

浙公网安备 33010602011771号