Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of ParamByName with TQuery 3

Status
Not open for further replies.

luistsousa

Technical User
May 12, 2003
66
PT
Hello

I have a very simple question to make (I think). I want create a new field in a mysql table.
I use a mysql database and when i run the code below appear a error message with "general sql error [Mysql].....:

Who can I solve the problem?


Query1.Close();
Query1.SQL.Clear();
Query1.SQL.Add("ALTER TABLE alter_exp, ADD :name CHAR(25)");
Query1.Params.ParamByName("name").AsString := "Deg";
Query1.ExecSQL();


Many thanks
Luis
 
have you tried running the query without using parameters?
i.e.

Query1.Close();
Query1.SQL.Clear();
Query1.SQL.Add('ALTER TABLE alter_exp, ADD Deg CHAR(25)');
Query1.ExecSQL();

Does it still fail? Just want to acertain if it is the bind param that is causing the problem. If not, you could always use the format function each time to build the sql statement:

Query1.SQL.Add(Format('ALTER TABLE alter_exp, ADD %s CHAR(25)', ['Deg']));

Any more info on the exception you are getting? as opposed to: "general sql error [Mysql].....:" - care to post the entire exception message?
 
hi

The parameter cann't be used in this sort of ways. A better way is to use a format:


Function AddFieldToDatabase(const sTable, sField: string);
const
CQuery = "ALTER TABLE %s ADD %s CHAR(25)";
var
s: string;
begin
s := Format(CQuery, [sTable, sField]);

Query1.Close();
Query1.SQL.Clear();
Query1.SQL.Add(s);
Query1.ExecSQL();

end;

This function will add char fields of length 25 to a database. It is also possible to give the fieldtype and the length as a parameter.


Function AddFieldToDatabase(const sTable, sField, sType: string);
const
CQuery = "ALTER TABLE %s ADD %s %s";
var
s: string;
begin
s := Format(CQuery, [sTable, sField, sType]);

Query1.Close();
Query1.SQL.Clear();
Query1.SQL.Add(s);
Query1.ExecSQL();

end;

In another function you could use:

AddFieldToDatabase('alter_exp', 'Deg' , 'CHAR(25)');


Steph [Bigglasses]
 
Hi

I used the format option and works very well.

Many thanks
Luis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top