function getjsontoWheresql(const ajsonarray: TJSONArray): string;
var
i: Integer;
ajsonobject: TJSONObject;
afieldname,afieldvalue:string;
sqlwherestring:string;
afieldoperator:string;
begin
sqlwherestring := ' ';
for i := 0 to ajsonarray.Size - 1 do
begin
ajsonobject := ajsonarray.Get(i) as TJSONObject;
afieldname := ajsonobject.Pairs[0].JsonString.ToString;
afieldname := ReplaceStr(afieldname,'"','');
afieldvalue := ajsonobject.Pairs[0].JsonValue.Value;
afieldoperator := ajsonobject.Pairs[1].JsonValue.Value;
sqlwherestring := sqlwherestring + ' ' + afieldname + ' ' + afieldoperator +
' ' + QuotedStr(afieldvalue) + ' and ';
end;
Result := ' where ' + LeftStr(sqlwherestring,
Length(sqlwherestring) - 5);
end;
///////////////
function getjsontoInsertsql(const ajsonarray:TJSONArray; tablename: string): string;
var
i,j: Integer;
ajsonobject: TJSONObject;
sqlstring:string;
afieldname,afieldvalue:string;
begin
sqlstring := '';
for i := 0 to ajsonarray.Size - 1 do
begin
sqlstring := sqlstring + ' insert into ' + tablename + '(';
ajsonobject := ajsonarray.Get(i) as TJSONObject;
for j := 0 to ajsonobject.Size - 1 do
begin
afieldname := ajsonobject.Pairs[j].JsonString.ToString;
sqlstring := sqlstring + ReplaceStr(afieldname,'') + ',';
end;
sqlstring := LeftStr(sqlstring,Length(sqlstring) - 1);
sqlstring := sqlstring + ')values(';
for j := 0 to ajsonobject.Size - 1 do
begin
afieldvalue := ajsonobject.Pairs[j].JsonValue.Value;
sqlstring := sqlstring + QuotedStr(afieldvalue) + ',Length(sqlstring) - 1);
sqlstring := sqlstring + ')';
end;
Result := sqlstring;
end;
///////////
function TForm1.getjsontoSelectsql(const ajsonwherearray: TJSONArray;
tablename: string;const thesql:string): string;
var
sqlwherestring:string;
begin
sqlwherestring := getjsontoWheresql(ajsonwherearray);
result := thesql + ' from ' + tablename + sqlwherestring;
end;
/////////
function getjsontoUpdadtesql(const ajsonUpdatearray,ajsonWherearray: TJSONArray;
tablename: string): string;
var
i,afieldvalue:string;
setvaluesql,sqlwherestring:string;
afieldoperator:string;
begin
sqlstring := '';
for i := 0 to ajsonUpdatearray.Size - 1 do
begin
sqlstring := sqlstring + ' update ' + tablename + ' set ';
ajsonobject := ajsonUpdatearray.Get(i) as TJSONObject;
setvaluesql := '';
for j := 0 to ajsonobject.Size - 1 do
begin
afieldname := ajsonobject.Pairs[j].JsonString.ToString;
afieldname := ReplaceStr(afieldname,'');
afieldvalue := ajsonobject.Pairs[j].JsonValue.Value;
setvaluesql := setvaluesql + afieldname + ' = ' + QuotedStr(afieldvalue) + ',';
end;
setvaluesql := LeftStr(setvaluesql,Length(setvaluesql) - 1);
//////
sqlwherestring := getjsontoWheresql(ajsonWherearray);
sqlstring := sqlstring + setvaluesql + sqlwherestring;
end;
Result := sqlstring;
end;
////////////////
function getjsontoDeletesql(const ajsonWherearray: TJSONArray; tablename: string): string;
var
i: Integer;
ajsonobject: TJSONObject;
sqlstring:string;
afieldname,afieldvalue:string;
sqlwherestring:string;
afieldoperator:string;
begin
sqlwherestring := ' ';
for i := 0 to ajsonWherearray.Size - 1 do
begin
ajsonobject := ajsonWherearray.Get(i) as TJSONObject;
afieldname := ajsonobject.Pairs[0].JsonString.ToString;
afieldname := ReplaceStr(afieldname,'');
afieldvalue := ajsonobject.Pairs[0].JsonValue.Value;
afieldoperator := ajsonobject.Pairs[1].JsonValue.Value;
sqlwherestring := sqlwherestring + ' ' + afieldname + ' ' + afieldoperator +
' ' + QuotedStr(afieldvalue) + ' and ';
end;
sqlwherestring := ' where ' + LeftStr(sqlwherestring,Length(sqlwherestring) - 5);
sqlstring := ' delete from ' + tablename + sqlwherestring;
Result := sqlstring;
end;
////////生成Jsonarray
procedure TForm1.Button1Click(Sender: TObject);var i: Integer; JSONCars: TJSONArray; Car,Price: TJSONObject;begin JSONCars := TJSONArray.Create; try for i := low(Cars) to high(Cars) do begin Car := TJSONObject.Create; JSONCars.AddElement(Car); Car.AddPair('manufacturer',Cars[i][TCarInfo.Manufacturer]); Car.AddPair('name',Cars[i][TCarInfo.Name]); Price := TJSONObject.Create; Car.AddPair('price',Cars[i][TCarInfo.Name]); // Price.AddPair('value',TJSONNumber.Create(Cars[i][TCarInfo.Price].ToInteger)); // Price.AddPair('currency',Cars[i][TCarInfo.Currency]); end; JSON := JSONCars.ToString; memo3.Text := json; finally JSONCars.Free; end;end;