473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to send variable number of arguments to mysql execute()

fungazid
Help help help please

I’m using DBD::mysql, and I want to insert a record into clients table (id, address, and phone-number of a client):


Expand|Select|Wrap|Line Numbers
  1. my $str= “?,?,?,,,”; 
  2. my @args1=('Zalman','Zalmanovich',12121,'New York');
  3.  
  4. $q=$dbh->prepare("INSERT INTO $table VALUES($str)") 
  5.          or die "Couldn't prepare statement: " . $dbh->errstr; 
  6.  
  7. $q->execute($args1[0],$args1[1],$args1[2],…) 
  8.         or die "Couldn't execute statement: " . $q->errstr; 
  9.  
THE PROBLEM:
If I want to change the number of arguments sent to execute(), I must change the source code inside execute() brackets.
I would like to be able to change it on run-time with something like:

Expand|Select|Wrap|Line Numbers
  1. $q->execute(f(@args1)) 
  2.       or die "Couldn't execute statement: " . $q->errstr; 
  3.  
where f() formats the input to the function, so @args1 is allowed to have a variable size.
This may add invaluable flexibility to my program, because my program works with different kinds of clients tables, but how to do it ????.


thanks for your advices
Dec 18 '07 #1
2 1988
eWish
971 Expert 512MB
If I understand you correctly, you are using placeholders and you want to have more or less arguments and you want your code to adapt to it automatically which placeholder dosen't do real well. Is that correct?

--Kevin
Dec 19 '07 #2
eWish
971 Expert 512MB
You can do something like this, then fine tune it for your needs.
Expand|Select|Wrap|Line Numbers
  1. my @array = ();
  2.  
  3. my $id = $q->param('id') || undef;
  4. my $address = $q->param('address') || undef;
  5. my $phone = $q->param('phone') || undef;
  6.  
  7.    push(@array, $id,$address,$phone);
  8.  
  9. my $count = @array;
  10. my $placeholders = join(',', (split//, '?' x $count));
  11.  
  12. my $insert_statement = $dbh->prepare(qq{INSERT INTO $table_name(id,address,phone) VALUES($placeholders)});
  13.    $insert_statement->execute(@array);
  14.    $insert_statement->finish();
--Kevin
Dec 20 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Suzanne Vogel | last post by:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers of parameters to functions. The example of its use given in the Dinkumware documentation *seems* to imply that the 'va_arg'...
2
by: news.cablevision.qc.ca | last post by:
Hi! I want, using a form, enter a number and then, read that number of records in a database and store information in variables. The only way I see to do this is to do a FOR...NEXT to the...
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call...
10
by: CJM | last post by:
I'm trying to call a package/procedure in oracle (from an ASP page) which requires a number of parameters. I have got this working using OO40 but unfortunately the transaction rollback function...
4
by: baccaril | last post by:
Hello, I have a form the post to a php script, the variable is passed to the php script successfully. I then query MySQL and output 8 rows at a time. So the problem I'm having is the first 8...
2
by: Alan | last post by:
I have a couple of questions about using a variable number of arguments in a function call (...). The context is that I have some mathematical functions I created. I currently pass them a pair of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.