Page 1 of 1

Adding a New Record

Posted: Thu 04 May 2006 19:10
by ashlar64
Hello,

I am evaluating these components with Builder 6.0 and I am trying to add a new record onto a table.

The table Site_ID has these fields.

ID which is a int, the primary flag and is set to auto increment
Name varchar(20)
Notes text
Num int


I was trying to use some of the methods in TMyTable like
Table1->Insert();
Table1->FieldVaues["Name"] = Edit1->Text;
Table1->FieldVaues["Notes"] = Edit2->Text;
Table1->FieldVaues["Num"] = 5;
Table1->Post();

But I seem to get errors when I try it this way.
Am I missing some steps? Or do I need to do it with SQL statements in a TMyQuery component?

Also how do I ensure that ID will be given the next number? I was under the impression MySQL does that automatically?


Thanks in Advance


---Dave

Posted: Thu 04 May 2006 21:30
by GEswin
You should at least post the error message you're getting. You'll know the last inserted ID with mytable.LastInsertID.

Posted: Fri 05 May 2006 09:26
by Antaeus
> Am I missing some steps?
It looks like correct operators sequence. Please describe the problem more detailed.

> Or do I need to do it with SQL statements in a TMyQuery component?
You can use TMyQuery but this isn't necessary. You should keep in mind that TMyQuery component provides more functionality than TMyTable.

> Also how do I ensure that ID will be given the next number?
As GEswin said, you can use LastInsertID property. Furthermore, after Post you can also check field value for AUTO_INCREMENT field.

> I was under the impression MySQL does that automatically?
MySQL server automatically generates values for fields defined as AUTO_INCREMENT, but the server also allows to insert values into AUTO_INCREMENT fields manually.

Posted: Fri 12 May 2006 12:42
by ashlar64
Actually my code was correct....the mysql server was going a little nutty and just needed to be reset.