473,499 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using an open connection to mysql database.

14 New Member
Hello,
Sorry in advance if this is a trivial problem.
I have a script which is executed about 2000 times in a row, and requires a connection to the mysql database each time it is executed.

I am wondering if there is a way to have a seperate program which connects to mysql and then keeps the connection open so that I can save some time and not have to connect 2000 times to the same thing.

I tried just making a script that connects to the database and then is put in an infinite loop, im just not sure if i can reference that connection with my other script. When i tried to execute it, it didn't know that a connection was already open. In other words I would like to reference the line:
$dbh = DBI->connect($dsn, $user, $pass);
in the script that is run 2000 times.
Any help is appreciated.
Nov 19 '08 #1
4 2481
Kelicula
176 Recognized Expert New Member
The "connect" method maintains a constant connection, until the "disconnect" method is called.

You can simply:
Expand|Select|Wrap|Line Numbers
  1.  
  2. use DBI;
  3. use strict;
  4.  
  5. my $dbh = DBI->connect("DBI:mysql:shema_name:port", "username", "pass")|| die "Error connecting to database: $DBI::errstr";
  6.  
  7. my $sth = $dbh->prepare(qq~ INSERT INTO temp_table (number, time) values(?,?)~);
  8.  
  9.  
  10. for(1..2000){
  11. $sth->execute($_, time)|| die "Could not update table: $sth->errstr";
  12. }
  13.  
  14. $sth->finish;
  15. $dbh->disconnect;
  16.  
  17.  
That will insert into a table named "temp_table", 2000 values, with the current time they were inserted.
Nov 19 '08 #2
Kelicula
176 Recognized Expert New Member
Or you can create a module that connects and shares it's database handle with another program.

Expand|Select|Wrap|Line Numbers
  1. package External;
  2.  
  3. use DBI;
  4. use strict;
  5.  
  6. our $dbh = DBI->connect("DBI:mysql:schema_name:port", "username", "pass")|| die "Can not connect to DB: $DBI::errstr";
  7.  
  8. 1;
  9.  

Then in another file:

Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3. use External;
  4.  
  5. my $dbh = $External::dbh;
  6.  
  7. for(1..2000){
  8. do stuff
  9. }
  10.  
  11.  
But I doubt it would work like you want.

You should proable move the file into a "block" within the file that connects, and then just call on it 2000 times.

Like a subroutine, instead of calling another file 2000 times, just call a block 200 times.
Nov 19 '08 #3
Icecrack
174 Recognized Expert New Member
Can you post some your script or mainly how you are connecting to mysql, so we could improve on how you would do it.
Nov 19 '08 #4
minimatrix
5 New Member
you could always store the values of the sql query in a hash.
then you only need to connect to the database once to populate the hash.

Once you have done this you can walk through the hash and make any changes you require by adding or removing elements form the hash.
and printing out SQL commands.

You can then dump it to an sql file and use source 'filename.sql' to make the changes to the database. hope this makes sense.

MINIMATRIX
Nov 20 '08 #5

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

Similar topics

5
3241
by: Phil Powell | last post by:
I've read some online resources that utilize various MySQL command-line actions to migrate data from Access to MySQL. The situation is this: a group of co-workers of mine will be using an Access...
0
5323
by: Jim S. | last post by:
I'm having a horrible time simply inserting the date in a MySQL database through a Visual Basic program. I have a table, called "ORDERS"; "ID" is the primary key. I'm trying the insert the date,...
2
3436
by: Sam White | last post by:
I have set up a MySQL db on one server, IIS 6.0 on another. Using Frontpage I created some forms to input data. On a test page I made, I have 4 fields. First is the ID which is autonumber (I leave...
6
3172
by: Erik H. | last post by:
Trying to connect to MySQL db on localhost, and populate datagrid from a dataset using code inline method. Getting the following compile error: Error Message: "CS0246: The type or namespace...
5
7671
by: msch-prv | last post by:
Hi, I am trying to tie a SQLDataSource control to MySQL without success. The connection string works ok with an ObjectDataSource. (Native asp.net 2.0 MySQL dll loaded in /bin) For some reason,...
2
9368
by: 111mike | last post by:
Hello, Here's my problem. I cannot connect to mysql database using odbc string connections or dns. I keep getting a "cannot connect to mysql server localhost." I'm running windows XP Pro and...
5
3076
by: john | last post by:
All: I have a MS background and am having fun in the open source world of PHP and MySQL --- and I have a question. With PHP and MySQL, is it considered better practice to open a single...
3
2688
by: Paradox Synthesist | last post by:
hi, i am a newbie to asp.net and have started learning mainly from websites. i have a question which IS very silly and trivial. ...
221
366942
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
7134
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
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
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,...
1
6905
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
7395
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
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.