I have an app with a TListView and I want to populate data from JSON inside its Items by using TJSONIterator.Next(). The code I use displays the results I want, except for the first one.
How can I parse these JSON objects correctly, what am I doing wrong?
Data: Data.json
{
"event":"subscribe-status",
"status":"ok",
"success":[
{
"symbol":"EUR/USD",
"exchange":"PHYSICAL CURRENCY",
"mic_code":"PHYSICAL CURRENCY",
"country":"",
"type":"Physical Currency"
},
{
"symbol":"USD/JPY",
"exchange":"PHYSICAL CURRENCY",
"mic_code":"PHYSICAL CURRENCY",
"country":"",
"type":"Physical Currency"
},
{
"symbol":"BTC/USD",
"exchange":"Coinbase Pro",
"mic_code":"Coinbase Pro",
"country":"",
"type":"Digital Currency"
},
{
"symbol":"ETH/BTC",
"exchange":"Huobi",
"mic_code":"Huobi",
"country":"",
"type":"Digital Currency"
}
],
"fails":null
}
Code app:
LStringReader := TStreamReader.Create('../../Data.json', TEncoding.UTF8, True);
LJsonTextReader := TJsonTextReader.Create(LStringReader);
LIterator := TJSONIterator.Create(LJsonTextReader);
NObjJSON := LIterator.AsInteger;
ListView1.Items.Clear;
ListView1.BeginUpdate;
try
while True do
begin
while LIterator.Next do
begin
if LIterator.&Type in [TJsonToken.StartObject, TJsonToken.StartArray] then
begin
LIterator.Recurse;
LIterator.Next;
oItem := ListView1.Items.Add;
for NObjJSON := 0 to ListView1.ItemCount -1 do
begin
oItem.Text := 'Object #' + NObjJSON.ToString + ' ' + LIterator.AsValue.ToString;
oItem.Detail := 'Key:' +LIterator.Key;
end
end;
end;
if LIterator.InRecurse then
LIterator.Return
else
Break;
end;
finally
ListView1.EndUpdate;
LIterator.Free;
LJsonTextReader.Free;
lStringReader.Free;
Memo1.Lines.Text := NObjJSON.ToString;
end;
Add this recurse / next at beginning of your loop to prepare to enter array :
while LIterator.Next do
begin
if LIterator.&Type = TJsonToken.StartArray then
begin
LIterator.Recurse;
LIterator.Next;
end;
You can check this exemple in the doc : https://docwiki.embarcadero.com/CodeExamples/Sydney/en/RTL.JSONIterator
The code below is easier to read :
procedure TFormX.LoadJSON;
const
cValue = 'symbol';
var
LValue: TJSONValue;
LArray: TJSONArray;
i: integer;
oItem: TListViewItem;
begin
LValue := TJSONObject.ParseJSONValue('{json}');
LArray := LValue.FindValue('success') as TJSONArray;
if Assigned(LArray) then
begin
for i := 0 to LArray.Count - 1 do
begin
oItem := ListView1.Items.Add;
oItem.Text := 'Object #' + i.ToString + ' ' + LArray.Items[i].GetValue<string>(cValue);
oItem.Detail := 'Key:' + cValue;
end;
end;
end;
After all, i found the correct solution:*
var
LIterator: TJSONIterator;
LJsonTextReader: TJsonTextReader;
LStringReader: TStreamReader;
NObjJSON: Integer;
begin
LStringReader := TStreamReader.Create('../../Data.json', TEncoding.UTF8, True);
LJsonTextReader := TJsonTextReader.Create(LStringReader);
LIterator := TJSONIterator.Create(LJsonTextReader);
NObjJSON := LIterator.AsInteger;
ListView1.Items.Clear;
ListView1.BeginUpdate;
try
while True do
begin
while LIterator.Next do
begin
if LIterator.&Type in [TJsonToken.StartObject, TJsonToken.StartArray] then
begin
Memo1.Lines.Add(LIterator.Key);
LIterator.Recurse;
end
else if LIterator.Path = 'success['+NObjJSON.ToString+'].symbol' then
begin
Memo1.Lines.Add(LIterator.AsValue.ToString);
oItem := ListView1.Items.Add;
for NObjJSON := 0 to ListView1.ItemCount -1 do
oItem.Text := 'Object #' + NObjJSON.ToString + ' ' + LIterator.AsValue.ToString;
end
end;
if LIterator.InRecurse then
LIterator.Return
else
Break;
end;
finally
ListView1.EndUpdate;
LIterator.Free;
LJsonTextReader.Free;
LStringReader.Free;
end;
end;
NObjJSON is used to count the number of objects inside array and it returns 4.
You can use a simple integer (I) and replace "for NObjJSON := 0 to ListView1.ItemCount -1 do" by for I := 0 to ListView1.ItemCount -1 do but the number of objects will return 0.
Related
I'm using this class (link) from Jens Borrisholt.
This class helps to monitor keyboard events.
but the problem is that all keystrokes is trapped inside my application and no characters appear on other programs!!!
FHook := THookInstance<TLowLevelKeyboardHook>.CreateHook(Self);
FHook.OnPreExecute := procedure(Hook: THook; var HookMsg: THookMessage)
var
LLKeyBoardHook: TLowLevelKeyboardHook;
ScanCode: integer;
begin
LLKeyBoardHook := TLowLevelKeyboardHook(Hook);
ScanCode := LLKeyBoardHook.KeyName.ScanCode;
Caption := 'Got ya! Key [' + LLKeyBoardHook.KeyName.KeyExtName + '] blocked.';
HookMsg.Result := LLKeyBoardHook.ThreadID;
end;
FHook.Active := true;
This works for me!
I set HookMsg.Result := 0; inside FHook.OnPostExecute
FHook := THookInstance<TLowLevelKeyboardHook>.CreateHook(Self);
FHook.OnPreExecute := procedure(Hook: THook; var HookMsg: THookMessage)
var
LLKeyBoardHook: TLowLevelKeyboardHook;
ScanCode: integer;
begin
LLKeyBoardHook := TLowLevelKeyboardHook(Hook);
if LLKeyBoardHook.LowLevelKeyStates.KeyState <> ksKeyDown then
exit;
ScanCode := LLKeyBoardHook.KeyName.ScanCode;
if not(ScanCode in [VK_NUMPAD0 .. VK_NUMPAD9, VK_0 .. VK_9]) then
begin
Caption := 'Got ya! Key [' + LLKeyBoardHook.KeyName.KeyExtName + '] blocked.';
end
else
Caption := '';
end;
FHook.OnPostExecute := procedure(Hook: THook; var HookMsg: THookMessage)
begin
HookMsg.Result := 0;
end;
Iam trying to insert data from a StringGrid to the Oracle DB table for that i tried like below.
function TfrmMapping.LoadtoTable: Boolean;
var
I, J: Integer;
lQuery, s: string;
lData: TArray<string>;
begin
for I := 0 to vTableColumns.count - 1 do
begin
if I <> vTableColumns.count - 1 then
begin
s := s + vTableColumns[I] + ',';
end
else
begin
s := s + vTableColumns[I];
end;
end;
for I := 1 to StrGrdLoadCSVData.RowCount - 1 do
begin
vSortedGrid.Add(StrGrdLoadCSVData.Rows[I].CommaText);
end;
for I := 0 to vSortedGrid.count - 1 do
begin
lQuery := 'Insert into ' + cmbBXDBTables.Text + '(' + s + ') values(' +
vSortedGrid[I] + ')';
DataModSample.FDQuery1.SQL.Clear;
DataModSample.FDQuery1.SQL.Add(lQuery);
DataModSample.FDQuery1.ExecSQL;
end;
Result := True;
end;
In the code , Iam adding all the data of StringGrid(StrGrdLoadCSVData) to a StringList(vSortedGrid), and now iam trying to loop through the StringList to add each row to the DB, But Iam not able to insert because my is taking the values like this
Insert into abc(sno,Name)values(1,welcome);
It is because there are no quotes to welcome it is giving an error.
it is error like this : [FireDAC][Phys][Ora]ORA-00984:column not allowed here
How i can modify my code to insert the data successfully to Db.
EDIT
My table Structure is :
Name Type
--------- ------------
SNO NUMBER(38)
NAME VARCHAR2(15)
my desired result in the table should be like this :
SNO NAME
---------- ----------
1 Hello
2 Welcome
The values in the table comming from the string List
It is because there are no quotes to welcome it is giving an error.
So from what you say :
for I := 0 to vSortedGrid.count - 1 do
begin
lQuery := 'Insert into ' + cmbBXDBTables.Text + '(' + s + ') values('+IntToStr(i+1)+',' +
QuotedStr(vSortedGrid[I]) + ')';
DataModSample.FDQuery1.SQL.Clear;
DataModSample.FDQuery1.SQL.Add(lQuery);
DataModSample.FDQuery1.ExecSQL;
end;
Result := True;
end;
Note: Better to use parameters.
Update:
Another option to insert from TStringGrid using TFDTable:
procedure TForm1.Button2Click(Sender: TObject);
Var I : Integer;
begin
for i := 1 to StringGrid1.RowCount-1 do
begin
try
FDTable1.Append;
FDTable1SNO.Value := StrToInt( StringGrid1.Cells[0,i] );
FDTable1SName.Value := StringGrid1.Cells[1,i];
FDTable1.Post;
except on E: Exception do
begin
MessageDlg(E.Message,mtError,[mbOK],0);
MessageBeep(MB_ICONERROR);
end;
end;
end;
Another option to insert from the TStringGrid using TFDQuery (avoid SQL Injection):
procedure TForm1.Button1Click(Sender: TObject);
Var I : Integer; TableName : String;
begin
TableName := 'Table1';
for i := 1 to StringGrid1.RowCount-1 do
begin
try
FDQuery1.SQL.Text := 'Insert Into '+TableName+' Values(:Val1 , :Val2)' ;
FDQuery1.Params.ParamByName('Val1').Value := StrToInt( StringGrid1.Cells[0,i] );
FDQuery1.Params.ParamByName('Val2').Value := StringGrid1.Cells[1,i];
FDQuery1.ExecSQL;
except on E: Exception do
begin
MessageDlg(E.Message,mtError,[mbOK],0);
MessageBeep(MB_ICONERROR);
end;
end;
You can also Create parameters as you need at Runtime for exemple:
FDQuery1.Params.CreateParam(ftString,'ParamName',ptInput) ;
Also you can use GetTableNames() to get all tables in the Database.
I Modified the Code like below
function TfrmMapping.LoadtoTable: Boolean;
var
I, J: Integer;
lQuery, s, lcolvalues: string;
begin
for I := 0 to vTableColumns.count - 1 do
begin
if I <> vTableColumns.count - 1 then
begin
s := s + vTableColumns[I] + ',';
end
else
begin
s := s + vTableColumns[I];
end;
end;
for I := 1 to StrGrdLoadCSVData.RowCount - 1 do
begin
for J := 0 to vTableColumns.count - 1 do
begin
if J <> vTableColumns.count - 1 then
begin
lcolvalues := lcolvalues +
QuotedStr(StrGrdLoadCSVData.Cells[J, I]) + ',';
end
else
begin
lcolvalues := lcolvalues + QuotedStr(StrGrdLoadCSVData.Cells[J, I]);
end;
end;
lQuery := 'Insert into ' + cmbBXDBTables.Text + '(' + s + ') values (' +
lcolvalues + ')';
DataModSample.FDQuery1.SQL.Clear;
DataModSample.FDQuery1.SQL.Add(lQuery);
DataModSample.FDQuery1.ExecSQL;
lcolvalues := '';
end;
Result := True;
end;
This is inserting values to Table from the string grid, I didn't use Parameter passing as of now. I have to try that also for ensuring more security.
Thank You #Sami , Buy using your concept of FDQuery I have got this idea...
I have a problem with MapiSendMail function of MAPI32.dll. Everything seems fine, message is completed, then I send it by winapi function, and i get an Access violation error, it happend in MAPISendMail. Here's the fragment of the code:
MAPIModule := LoadLibrary(PWideChar(MAPIDLL));
if MAPIModule = 0 then
Result := -1
else
try
#SM := GetProcAddress(MAPIModule, 'MAPISendMail');
if #SM <> nil then
begin
Result := SM(0, application.Handle, Msg, MAPI_DIALOG {or MAPI_LOGON_UI}, 0);
end
else
Result := 1;
finally
end;
Also I was trying to change GetProcAddres to MAPISendMailW or MAPISendMailHelper, but then #SM was nil.
#Edit1
function TMail._SendMAPIEmail(const aTo, aAtts: array of AnsiString; const body, subject, SenderName, SenderEmail: string; ShowError: Boolean = true): Integer;
var
SM: TFNMapiSendMail;
Msg: MapiMessage;
lpSender: MapiRecipDesc;
Recips: array of MapiRecipDesc;
Att: array of MapiFileDesc;
TempAttNames: array of pAnsiChar;
TempAttNamesAnsi: array of AnsiString;
TempAttPaths: array of pAnsiChar;
TempRecip: array of pAnsiChar;
p1, LenTo, LenAtts: Integer;
MAPIModule: HModule;
sError: String;
i: integer;
begin
try
FillChar(Msg, SizeOf(Msg), 0);
{ get the length of all arrays passed to this function }
LenTo := length(aTo);
if Trim(aAtts[0]) <> '' then
LenAtts := length(aAtts)
else
LenAtts := 0;
{ ... }
SetLength(Recips, LenTo);
SetLength(TempRecip, LenTo);
Setlength(Att, LenAtts);
SetLength(TempAttNames, LenAtts);
SetLength(TempAttPaths, LenAtts);
SetLength(TempAttNamesAnsi, LenAtts);
{ to }
for p1 := 0 to LenTo - 1 do
begin
FillChar(Recips[p1], SizeOf(Recips[p1]), 0);
Recips[p1].ulReserved := 0;
Recips[p1].ulRecipClass := MAPI_TO;
{ Upgrade }
Recips[p1].lpszName := '';
TempRecip[p1] := pAnsichar(aTo[p1]);
Recips[p1].lpszAddress := TempRecip[p1];
end;
{ atts }
for p1 := 0 to LenAtts - 1 do
begin
FillChar(Att[p1], SizeOf(Att[p1]), 0);
FillChar(TempAttPaths[p1], SizeOf(pAnsiChar), 0);
FillChar(TempAttNames[p1], SizeOf(pAnsiChar), 0);
FillChar(TempAttNamesAnsi[01], SizeOf(AnsiChar), 0);
Att[p1].ulReserved := 0;
Att[p1].flFlags := 0;
Att[p1].nPosition := Cardinal($FFFFFFFF);
{ Upgrade }
TempAttPaths[p1] := pAnsichar(aAtts[p1]);
Att[p1].lpszPathName := TempAttPaths[p1];
TempAttNamesAnsi[p1] := AnsiString((ExtractFileName(string(aAtts[p1]))));
TempAttNames[p1] := pAnsiChar(TempAttNamesAnsi[p1]);
Att[p1].lpszFileName := TempAttNames[p1];
end;
{ fill the message }
with Msg do
begin
ulReserved := 0;
if subject <> '' then
{ Upgrade }
lpszSubject := pAnsichar(AnsiString(subject));
if body <> '' then
{ Upgrade }
lpszNoteText := pAnsichar(AnsiString(body));
if SenderEmail <> '' then
begin
lpSender.ulRecipClass := MAPI_ORIG;
if SenderName = '' then
lpSender.lpszName := pAnsichar(AnsiString(SenderEmail))
else
lpSender.lpszName := pAnsichar(AnsiString(SenderName));
lpSender.lpszAddress := pAnsichar(AnsiString(SenderEmail));
lpSender.ulEIDSize := 0;
lpSender.lpEntryID := nil;
lpOriginator := #lpSender;
end
else
Msg.lpOriginator := nil;
Msg.lpszMessageType := nil;
Msg.lpszDateReceived := nil;
Msg.lpszConversationID := nil;
Msg.flFlags := 0;
Msg.nRecipCount := LenTo;
Msg.lpRecips := #Recips[0];
Msg.nFileCount := LenAtts;
Msg.lpFiles := #Att[0];
end;
MAPIModule := LoadLibrary(PWideChar(MAPIDLL));
if MAPIModule = 0 then
Result := -1
else
try
#SM := GetProcAddress(MAPIModule, 'MAPISendMail');
if #SM <> nil then
begin
//Result := MapiSendMail(0, application.Handle, Msg, MAPI_DIALOG, 0);
Result := SM(0, 0, Msg, MAPI_DIALOG {or MAPI_LOGON_UI}, 0);
end
else
Result := 1;
finally
if Assigned(Att) and (Msg.nFileCount > 0) then
begin
for i := 0 to Msg.nFileCount - 1 do
begin
if Assigned(Att[i].lpszPathName) then
Att[i].lpszPathName := nil;
if Assigned(Att[i].lpszFileName) then
Att[i].lpszFileName := nil;
//FreeMem(Att[i].lpszPathName);
//Dispose(Att[i].lpszPathname);
//StrDispose(Att[i].lpszPathName);
//Dispose(Att[i].lpszFileName);
//StrDispose(Att[i].lpszFileName);
end;
Att := nil;
end;
if Assigned(Recips) and (Msg.nRecipCount > 0) then
begin
for i := 0 to Msg.nRecipCount - 1 do
begin
if Assigned(Recips[i].lpszName) then
Recips[i].lpszName := nil;
if Assigned(Recips[i].lpszAddress) then
Recips[i].lpszAddress := nil;
//if Assigned(Recips[i].lpszName) then
//Dispose(Recips[i].lpszName);
//if Assigned(Recips[i].lpszAddress) then
//Dispose(Recips[i].lpszAddress);
end;
Recips := nil;
end;
end;
Under Win32
Under Win32 it should not be a problem. Just first try calling MapiSendMail with very simple MapiMessage and if it will work, add complexity little by little. Your code is just too complex to debug it visually. Did you call MapiSendMail with very simple MapiMessage, just for testing? Please try the following code, it works for sure:
procedure TestSendExA(const APath1, ACaption1, APath2, ACaption2: AnsiString);
var
R: Integer;
MSG: TMapiMessage;
F: Array [0..1] of TMapiFileDesc;
Recipients: array[0..1] of TMapiRecipDesc;
Originator : array[0..0] of TMapiRecipDesc;
begin
if not FileExists(APath1) or not FileExists(APath2) then raise Exception.Create('File not found');
FillChar(Msg, SizeOf(Msg), 0);
Msg.lpszSubject := 'testo';
Msg.lpszNoteText := 'Hi there!';
Msg.lpszDateReceived := '2015/01/25 12:34';
Msg.lpszConversationId := '1234.test#ritlabs.com';
Msg.flFlags := MAPI_RECEIPT_REQUESTED;
FillChar(Recipients, SizeOf(Recipients), 0);
with Recipients[0] do
begin
ulRecipClass := MAPI_TO;
lpszName := 'Maxim Masiutin';
lpszAddress := 'maxim.test#ritlabs.com';
end;
with Recipients[1] do
begin
ulRecipClass := MAPI_CC;
lpszName := 'Vasilii Pupkin';
lpszAddress := 'pupkin.test#ritlabs.com';
end;
FillChar(Originator, SizeOf(Originator), 0);
with Originator[0] do
begin
ulRecipClass := MAPI_TO;
lpszName := 'Maxim Masiutin';
lpszAddress := 'max#ritlabs.com';
end;
Msg.lpOriginator := #Originator;
Msg.nRecipCount := 2;
Msg.lpRecips := #Recipients;
Msg.nFileCount := 2;
Msg.lpFiles := #F;
FillChar(F, SizeOf(F), 0);
F[0].lpszPathName := PAnsiChar(APath1);
F[0].lpszFileName := PAnsiChar(ACaption1);
F[1].lpszPathName := PAnsiChar(APath2);
F[1].lpszFileName := PAnsiChar(ACaption2);
R := MAPISendMail(MapiSession, 0, Msg, 0, 0);
end;
The MapiSession in the above example is a handle to the session returned by MapiLogon.
This sample code requires that you pass two valid file paths to valid files in APath1 and APath2.
Under Win64
It is the record alignment of MapiMessage and other records that it is important when you work with Simple MAPI from Delphi: (1) make sure the records don't have "packed" prefix; and (2) make sure you have {$A8} compiler directive is explicitly specified before first record definition. This will work fine under both Win32 and Win64.
procedure TMainForm.btn1Click(Sender: TObject);
var
lHTTP: TIdHTTP;
begin
lHTTP := TIdHTTP.Create(nil);
try
mmo1.Text := lHTTP.Get('http://guessit.io/guess?filename=House.of.Cards.2013.S02E03.1080p.NF.WEBRip.DD5.1.x264-NTb.mkv');
finally
lHTTP.Free;
end;
end;
the result will be like that:
{
"series":"House of Cards",
"episodeNumber":3,
"releaseGroup":"NTb",
"format":"WEBRip",
"season":2,
"audioCodec":"DolbyDigital",
"year":2013,
"mimetype":"video/x-matroska",
"container":"mkv",
"videoCodec":"h264",
"other":[
"Netflix"
],
"audioChannels":"5.1",
"screenSize":"1080p",
"type":"episode"
}
how i can read the series ("series": "House of Cards") value for example.
i try to use
Function ExtractBetweenTags(Const Line, TagI, TagF: string): string;
var
i, f : integer;
begin
i := Pos(TagI, Line);
f := Pos(TagF, Copy(Line, i+length(TagI), MAXINT));
if (i > 0) and (f > 0) then
Result:= Copy(Line, i+length(TagI), f-1);
end;
mmo1.Text := StringReplace(ExtractBetweenTags(mmo1.Text,'"series": "','"'), ' ', '-',[rfReplaceAll, rfIgnoreCase]);
but i need to parsing all the values automatically
You might use DBXJSON contained in Delphi since Delphi 2010, an other option could be to use e.g. SuperObject.
Since your shown example is a simple JSON String without nesting you might access the cointained values by just interating over a TJSONObject acessing as TJSONArray.
This array consists of Pairs of "Name" and the value.
An simple example shown with the string you provided:
uses
DBXJSON;
procedure TForm7.Button1Click(Sender: TObject);
var
S:String;
I:Integer;
JObj:TJSONObject;
Pair : TJSONPair;
begin
s := '{"series": "House of Cards", "episodeNumber": 3, "releaseGroup": "NTb", "format": "WEBRip", "season": 2'
+', "audioCodec": "DolbyDigital", "year": 2013, "mimetype": "video/x-matroska", "container": "mkv", "videoCodec": "h264", "other": ["Netflix"], "audioChannels":
"5.1", "screenSize": "1080p", "type": "episode"}';
JObj := TJSONObject.ParseJSONValue(s) as TJSONObject;
for I := 0 to TJSONArray(JObj).Size - 1 do
begin
Pair := TJSONPair(TJSONArray(JObj).Get(i));
Memo1.Lines.Add( Pair.JsonString.Value + '=' + Pair.JsonValue.ToString)
end;
end;
You can parse the object with class like this:
uses System.JSON, Rest.JSON;
type
TGuess = class(TObject)
public
fseries: string;
fepisodeNumber: integer;
freleaseGroup: string;
fformat: string;
fseason: integer;
faudioCodec: string;
fyear: integer;
fmimetype: string;
fcontainer: string;
fvideoCodec: string;
fother: TArray<string>;
faudioChannels: string;
fscreenSize: string;
ftype: string;
end;
var
g: TGuess;
s: string; // your input string
// g object will be created by following call and will contain parsed values
g := TJson.JsonToObject<TGuess>(s);
// do something with g
// release g after it is no longer needed
g.Free;
Thanks for all the answers but here what i finally figure it out
procedure TMainForm.btn1Click(Sender: TObject);
var
S : String;
NS , S1,S2,series,season,episodeNumber : String;
L , I , LN : integer;
MM :TStringList;
begin
try
MM := TStringList.Create;
NS := '';
S := '{"series": "House of Cards", "episodeNumber": 3, "releaseGroup": "NTb", "format":'+
'"WEBRip", "season": 2, "audioCodec": "DolbyDigital", "year": 2013, "mimetype": "video/x-matroska", "container": "mkv",'+
'"videoCodec": "h264", "other": ["Netflix"], "audioChannels": "5.1", "screenSize": "1080p", "type": "episode"}';
{if AnsiPos('title',s) <> 0 then
IsMovie := True
else
IsMovie := False;}
L := Length(S);
for I := 1 to L do
Begin
If (Ord(S[I]) <> 34)
And (Ord(S[I]) <> 123)
And (Ord(S[I]) <> 125)
Then NS := NS + Copy(S,I,1);
End;
S := NS;
NS := '';
L := Length(S);
for I := 1 to L do
Begin
If (Ord(S[I]) = 44)
Or (Ord(S[I]) = 58)
Then Begin
NS := NS + #13#10;
MM.Add(Trim(NS));
NS := '';
End
Else NS := NS + Copy(S,I,1);
End;
MM.Add(Trim(NS));
// --------------------------------------------------------------
LN := MM.Count-1;
for I := 0 to LN Do
Begin
If MM[I] = Trim('series')
Then series := Trim(MM[I + 1]);
If MM[I] = Trim('season')
Then season := Trim(MM[I + 1]);
If MM[I] = Trim('episodeNumber')
Then episodeNumber := Trim(MM[I + 1]);
End;
ShowMessage(series+'.S'+season+'.E'+episodeNumber);
finally
MM.Free
end;
end;
I have a DB grid which is sorted (the user clicked a few radio buttons and checkboxes to influence the display).
I would like to export all of the data (not just what is visible in the grid), sorted identically, to CSV - how do I do so? The data - not the user settings, just to clarify.
Thanks in advance for any help
[Update] I build sqlQuery bit by bit, depending on the user's settings of checkboxes & radio groups, then, when one of them changes, I
ActivityADQuery.SQL.Clear();
ActivityADQuery.SQL.Add(sqlQuery);
ActivityADQuery.Open(sqlQuery);
That is to say that there isn't a hard coded query, it varies and I want to export the current settings.
I don't know enough if I want to export from the grid or the dataset (I am just not a db guy, this is my first DBgrid), but I suspect that I want the grid, because it has a subset of fields of he dataset.
I guess that TJvDBGridCSVExport is a Jedi component(?) I have tried to avoid them so far, great as they sound, because I prefer discreet, stand-alone, components to installing a huge collection. That may not be the cleverest thing to do, but it's how I feel - ymmv (and prolly does)
Another solution, works also with (multi)selected rows:
procedure TReportsForm.ExportToCSV(const aGrid : TDBGrid; const FileName : String);
Var
I, J : Integer;
SavePlace : TBookmark;
Table : TStrings;
HeadTable : String;
LineTable : String;
First : Boolean;
Begin
HeadTable := '';
LineTable := '';
Table := TStringList.Create;
First := True;
Try
For I := 0 To Pred(aGrid.Columns.Count) Do
If aGrid.Columns[I].Visible Then
If First Then
Begin
// Use the text from the grid, in case it has been set programatically
// E.g., we prefer to show "Date/time" than "from_unixtime(activity.time_stamp, "%D %b %Y %l:%i:%S")"
// HeadTable := HeadTable + aGrid.Columns[I].FieldName;
HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ','; // Previous separated wth semi-colon, not comma! (global)
First := False;
End
Else
begin
// HeadTable := HeadTable + ';' + aGrid.Columns[I].FieldName;
HeadTable := HeadTable + ActivityReportStringGrid.Columns[i].Title.Caption + ',';
end;
Delete(HeadTable, Length(HeadTable), 1); // Remove the superfluous trailing comma
Table.Add(HeadTable);
First := True;
// with selection of rows
If aGrid.SelectedRows.Count > 0 Then
Begin
For i := 0 To aGrid.SelectedRows.Count - 1 Do
Begin
aGrid.DataSource.Dataset.GotoBookmark(pointer(aGrid.SelectedRows.Items[i]));
For j := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[J].Visible Then
If First Then
Begin
lineTable := lineTable + aGrid.Fields[J].AsString;
First := False;
End
Else
lineTable := lineTable + ',' + aGrid.Fields[J].AsString;
Delete(LineTable, Length(LineTable), 1); // Remove the superfluous trailing comma
Table.Add(LineTable);
LineTable := '';
First := True;
End;
End
Else
//no selection
Begin
SavePlace := aGrid.DataSource.Dataset.GetBookmark;
aGrid.DataSource.Dataset.First;
Try
While Not aGrid.DataSource.Dataset.Eof Do
Begin
For I := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[I].Visible Then
If First Then
Begin
lineTable := lineTable + aGrid.Fields[I].AsString;
First := False;
End
Else
lineTable := lineTable + ',' + aGrid.Fields[I].AsString;
Delete(LineTable, Length(LineTable), 1); // Remove the superfluous trailing comma
Table.Add(LineTable);
LineTable := '';
aGrid.DataSource.Dataset.Next;
First := True;
End;
aGrid.DataSource.Dataset.GotoBookmark(SavePlace);
Finally
aGrid.DataSource.Dataset.FreeBookmark(SavePlace);
End;
End;
Table.SaveToFile(FileName);
Finally
Table.Free;
End;
End; // ExportToCSV()
You could use a own tiny procedure wich could be adapted to your needs
Procedure Dataset2SeparatedFile(ads: TDataset; const fn: String; const Separator: String = ';');
var
sl: TStringList;
s: String;
i: Integer;
bm: TBookmark;
Procedure ClipIt;
begin
s := Copy(s, 1, Length(s) - Length(Separator));
sl.Add(s);
s := '';
end;
Function FixIt(const s: String): String;
begin
// maybe changed
Result := StringReplace(StringReplace(StringReplace(s, Separator, '', [rfReplaceAll]), #13, '', [rfReplaceAll]), #10, '', [rfReplaceAll]);
// additional changes could be Quoting Strings
end;
begin
sl := TStringList.Create;
try
s := '';
For i := 0 to ads.FieldCount - 1 do
begin
if ads.Fields[i].Visible then
s := s + FixIt(ads.Fields[i].DisplayLabel) + Separator;
end;
ClipIt;
bm := ads.GetBookmark;
ads.DisableControls;
try
ads.First;
while not ads.Eof do
begin
For i := 0 to ads.FieldCount - 1 do
begin
if ads.Fields[i].Visible then
s := s + FixIt(ads.Fields[i].DisplayText) + Separator;
end;
ClipIt;
ads.Next;
end;
ads.GotoBookmark(bm);
finally
ads.EnableControls;
ads.FreeBookmark(bm);
end;
sl.SaveToFile(fn);
finally
sl.Free;
end;
end;