We help IT Professionals succeed at work.

TTable / Save datas / Get Datas

ItsMe
ItsMe asked
on
hello! i want to use ttable to get text datas from my mysql table and to write data to it. i don't know how to use ttable. could you post me some examples please ?

imagine a table like

counter|name|company
1|me|its
2|you|test
.
.
.

how can i add new lines, delete e.g. entry where counter = 2 or change the name of counter 1 line from me to "testit" ???

kind regards
ItsMe

ps_ i got a ttable component which works pretty fine with mysql.
Comment
Watch Question

table1.Add;
table1.Insert;
table1.Edit;
table1.Delete;

After changes you must write;
Table1.Post;

Table1.Cancel;

.........


Author

Commented:
and how do i define the values i want to write to the table ? how do i select the entry i want to change ?

kind regards
ItsMe
Mohammed NasmanSoftware Developer
BRONZE EXPERT

Commented:
Hello

  Button1 will add record to the table with data is specific, but button2 will get the data from the edit boxes

hoep that what you need
procedure TForm1.Button1Click(Sender: TObject);
begin
  Table1.Append;
  Table1.FieldByName('Counter').AsInteger := 1;
  Table1.FieldByName('Name').AsString := 'Me';
  Table1.FieldByName('Company').AsString := 'Its';
  Table1.Post;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Table1.Append;
  Table1.FieldByName('Counter').AsInteger := StrToInt(Edit1.Text);
  Table1.FieldByName('Name').AsString := Edit2.Text;
  Table1.FieldByName('Company').AsString := Edit3.Text;
  Table1.Post;
end;

Author

Commented:
this seems to be very easy. but what when i want to change an existing line ? how do i select a specific dataset ? i think this is required to do a delete command and so on, too ?
Mohammed NasmanSoftware Developer
BRONZE EXPERT

Commented:
also if you want update or delete sepcific record

// to update record
procedure TForm1.Button3Click(Sender: TObject);
begin
  Table1.Locate('Counte',2,[]);
  Table1.Edit;
  Table1.FieldByName('Company').AsString := 'New Company';
  Table1.Post;
end;

// to delete record
procedure TForm1.Button4Click(Sender: TObject);
begin
  if Table1.Locate('Counte',2,[]) then
    Table1.Delete
  else
    ShowMessage('Record is not exist');
end;

also you can use the query component to update or delete more than record with online
procedure TForm1.Button5Click(Sender: TObject);
begin
  Query1.Close;
  Query1.SQL.Text := 'Update test set Company = "New Company" where counter > 5';
// to delete look at this line
//  Query1.SQL.Text := 'delete from test where counter > 5';
  Query1.ExecSQL;
end;
Mohammed NasmanSoftware Developer
BRONZE EXPERT

Commented:
if you want to change the table name

  Table1.Close;
  Table1.TableName := 'Customer';
  Table1.Open;

Author

Commented:
is there also a way to "go through" the table, e.g. to generate statistics ?

Mohammed NasmanSoftware Developer
BRONZE EXPERT

Commented:
this simple code to get the sum of counter field
this procedure will go through ur table record by record, so u can do what you want with the records

procedure TForm1.Button8Click(Sender: TObject);
 var
   sum : Integer;
begin
  Sum := 0;
  Table1.DisableControls;
  while not Table1.Eof do
  begin
    Sum := Sum + Table1.FieldByName('counter').AsInteger;
    Table1.Next;
  end;
  Table1.EnableControls;
  ShowMessage('Sum of the counter = ' + IntToStr(Sum));
end;

but it's will be easier if you work with sql statments

Query1.Close;
Query1.Sql.Text := 'Select sum(counter) from test';
Query1.Open;

also there's some statics functions in the sql, like sum, count, avarege and so on
if you want to more info about the database look at these pages

http://delphi.about.com/cs/database/
http://www.delphi-dolphin.com/chapter.php?chapter=6

Author

Commented:
hi mnasman!
how do you get the result returned by the select sum sql command ? i thought of executing sql directly, too but i didn't know how to get the result.

kind regards
ItsMe
Mohammed NasmanSoftware Developer
BRONZE EXPERT

Commented:
Hello ItsMe

it's easy, you can assign name for the result as following

Query1.Close;
Query1.Sql.Text := 'Select Sum(Counter) as SumCounter from test';
Query1.Open;
Edit1.Text := Query1.FieldByName('SumCounter').asString;


or u can abbreviate the last line with
Edit1.Text := Query1['SumCounter'];

Best regards
Mohammed Nasman

Author

Commented:
hi mnasman!
could you explain me what happens when i execute the last commands ? is SumCounter a new field in my DB ? Must I delete it after Reading, or is it only a kind of var ?

kind regards
ItsMe
Software Developer
BRONZE EXPERT
Commented:
hi ItsMe

  it's not new field, it's just alias for ur field, you can also use it to view the field with names u choose
like

select counter as MyCounter, sal as Salary from table

so it's not phisycal field in the database, it's just an alias for ur field or expression that u can use it as field
Mohammed NasmanSoftware Developer
BRONZE EXPERT

Commented:
was that clear to you?, if not just say that :o)
ItsMe:

I have found that you have a great many questions open. Some of them are quite old. Please address them immediately:

http://www.experts-exchange.com/jsp/qShow.jsp?qid=20080809
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20081517
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20110611
http://www.experts-exchange.com/jsp/qShow.jsp?qid=11412679
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20170559
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20172193
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20177907
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20178750
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20179401
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20181503
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20187389
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20187432
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20188151
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20243899
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20250630
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20254819

To assist you in your cleanup, I'm providing the following guidelines:

1.  Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.

2.  Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.

3.  When grading, be sure to read:
http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.

4.  Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS:  http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.

5.  If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.

Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!


thanks!
amp
community support moderator

Author

Commented:
sorry i forgot