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.
4 2481 Kelicula 176
Recognized Expert New Member
The "connect" method maintains a constant connection, until the "disconnect" method is called.
You can simply: -
-
use DBI;
-
use strict;
-
-
my $dbh = DBI->connect("DBI:mysql:shema_name:port", "username", "pass")|| die "Error connecting to database: $DBI::errstr";
-
-
my $sth = $dbh->prepare(qq~ INSERT INTO temp_table (number, time) values(?,?)~);
-
-
-
for(1..2000){
-
$sth->execute($_, time)|| die "Could not update table: $sth->errstr";
-
}
-
-
$sth->finish;
-
$dbh->disconnect;
-
-
That will insert into a table named "temp_table", 2000 values, with the current time they were inserted.
Kelicula 176
Recognized Expert New Member
Or you can create a module that connects and shares it's database handle with another program. -
package External;
-
-
use DBI;
-
use strict;
-
-
our $dbh = DBI->connect("DBI:mysql:schema_name:port", "username", "pass")|| die "Can not connect to DB: $DBI::errstr";
-
-
1;
-
Then in another file: -
-
use strict;
-
use External;
-
-
my $dbh = $External::dbh;
-
-
for(1..2000){
-
do stuff
-
}
-
-
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.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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,...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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.
...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |