Difference between revisions of "Talk:Lazarus Database Overview"
From Lazarus wiki
Jump to navigationJump to searchm |
|||
Line 21: | Line 21: | ||
Well to cut a long story short, I would like the code to be changed into: | Well to cut a long story short, I would like the code to be changed into: | ||
− | procedure TFormTryMySQL.QueryButtonClick(Sender: TObject); | + | procedure TFormTryMySQL.QueryButtonClick(Sender: TObject); |
− | var | + | var |
− | + | tableBuffer: PMYSQL_RES; | |
− | + | recordBuffer: TMYSQL_ROW; | |
− | + | fieldList: TStringList; | |
− | + | I: Integer; | |
− | + | stMessage: String; | |
− | begin | + | begin |
− | + | // Show how we can get records from a table | |
− | + | StatusBar1.SimpleText := 'Executing query: ' + CommandEdit.Text; | |
− | + | ShowString('Executing query: ' + CommandEdit.Text); | |
− | + | if (mysql_query(mySQLSock, PChar(CommandEdit.Text)) < 0) then begin | |
− | + | ShowString('Query failed '+ StrPas(mysql_error(mySQLSock))); | |
− | + | end else begin | |
− | + | tableBuffer := mysql_store_result(mySQLSock); | |
− | + | try | |
− | + | if tableBuffer = nil then begin | |
− | + | ShowString('Query didnot return any results.'); | |
− | + | end else begin | |
− | + | fieldList := TStringList.Create; | |
− | + | try | |
− | + | ShowString('Number of records returned : ' + IntToStr(mysql_num_rows(tableBuffer))); | |
− | + | ShowString('Number of fields per record : ' + IntToStr(mysql_num_fields(tableBuffer))); | |
− | + | // Lets get a list a returned fields. | |
− | + | for I := 0 to pred(mysql_num_fields(tableBuffer)) do begin | |
− | + | fieldList.Add(mysql_fetch_field_direct(tableBuffer, I).name); | |
− | + | end; | |
− | + | recordBuffer := mysql_fetch_row(tableBuffer); | |
− | + | while (recordBuffer <> nil) do begin | |
− | + | stMessage := ''; | |
− | + | for I := 0 to pred(mysql_num_fields(tableBuffer)) do begin | |
− | + | stMessage := stMessage + fieldList[I] + ': ' + recordBuffer[I] + ', '; | |
− | + | end; | |
− | + | Delete(stMessage, Length(stMessage) - 2, 2); | |
− | + | ShowString(stMessage); | |
− | + | recordBuffer := mysql_fetch_row(tableBuffer); | |
− | + | end; | |
− | + | finally | |
− | + | fieldList.Free; | |
− | + | end; | |
− | + | end; | |
− | + | finally | |
− | + | // Free the results, thus saving resources. | |
− | + | mysql_free_result(tableBuffer); | |
− | + | end; | |
− | + | end; | |
− | end; | + | end; |
There are some more changes, in fact I expanded it with a second form to show data in a ListView. So if there is any interest in that I'm willing to contribute it to the documentation project. | There are some more changes, in fact I expanded it with a second form to show data in a ListView. So if there is any interest in that I'm willing to contribute it to the documentation project. |
Revision as of 15:22, 4 April 2005
Hi,
I would like to change some code given in the MySQL example for Lazarus. I think there are some fundamental errors here. For instance
procedure ShowString (S : string); (* display a string in a Memo box *) begin trymysqlForm1.ResultsMemo.Lines.Add (S) end;
should be done in a methode of the form like:
procedure TtrymysqlForm1.ShowString (S : string); (* display a string in a Memo box *) begin ResultsMemo.Lines.Add (S) end;
It makes the use of the trymysqlForm1 variable obsolete, which might be the cause of runtime errors. Well to cut a long story short, I would like the code to be changed into:
procedure TFormTryMySQL.QueryButtonClick(Sender: TObject); var tableBuffer: PMYSQL_RES; recordBuffer: TMYSQL_ROW; fieldList: TStringList; I: Integer; stMessage: String; begin // Show how we can get records from a table StatusBar1.SimpleText := 'Executing query: ' + CommandEdit.Text; ShowString('Executing query: ' + CommandEdit.Text); if (mysql_query(mySQLSock, PChar(CommandEdit.Text)) < 0) then begin ShowString('Query failed '+ StrPas(mysql_error(mySQLSock))); end else begin tableBuffer := mysql_store_result(mySQLSock); try if tableBuffer = nil then begin ShowString('Query didnot return any results.'); end else begin fieldList := TStringList.Create; try ShowString('Number of records returned : ' + IntToStr(mysql_num_rows(tableBuffer))); ShowString('Number of fields per record : ' + IntToStr(mysql_num_fields(tableBuffer))); // Lets get a list a returned fields. for I := 0 to pred(mysql_num_fields(tableBuffer)) do begin fieldList.Add(mysql_fetch_field_direct(tableBuffer, I).name); end; recordBuffer := mysql_fetch_row(tableBuffer); while (recordBuffer <> nil) do begin stMessage := ; for I := 0 to pred(mysql_num_fields(tableBuffer)) do begin stMessage := stMessage + fieldList[I] + ': ' + recordBuffer[I] + ', '; end; Delete(stMessage, Length(stMessage) - 2, 2); ShowString(stMessage); recordBuffer := mysql_fetch_row(tableBuffer); end; finally fieldList.Free; end; end; finally // Free the results, thus saving resources. mysql_free_result(tableBuffer); end; end; end;
There are some more changes, in fact I expanded it with a second form to show data in a ListView. So if there is any interest in that I'm willing to contribute it to the documentation project.