473,625 Members | 3,201 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 2502
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
3260
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 db in a shared source (for now, a directory.. ???) to be able to generate reports on the fly. What they want to do is to be able to migrate that data to a MySQL db instance that currently exists on a different server. What would be the best...
0
5339
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, into the MySQL database, which is a DATETIME datatype. I must keep it DATETIME so it can be accessed via Microsoft Access. Here are two versions of the program. The first one fails, with the "Row cannot be located for updating. Some values may...
2
3444
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 alone). The second is marked as TEXT, the third is MEDIUMTEXT, and the fourth is DATE. This is just an example though, the problem exists in no matter what manner I create the form. Now here is the issue. In the TEXT fields nothing is saved...
6
3181
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 name 'CoreLab' could not be found (are you missing a using directive or an assembly reference?)" I downloaded the add on for connecting to MySQL using ADO.NET and installed it, but am still getting this error. Anybody ever run into this before?
5
7681
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, the compiler believes an SQL db is being accessed ("An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does...
2
9380
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 have installed IIS as my server. I have installed Mysql 5.0 and mysql ODBC driver 3.51.12 in their default locations. Database is up and running okay, as I've been able to create databases and tables and access them via the command prompt and...
5
3093
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 database connection and re-use it throughout the life of the application (simple Web application - low traffic), or is it better to open a connection, use it and close it as needed?
3
2696
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. http://msdn2.microsoft.com/en-us/library/system.data.odbc.odbcconnectionstringbuilder.dsn(VS.80).aspx i looked through this url to see how could i connect to a mysql database...where does the dsn figure out? why do i need to give it? i cant see any dsn specified anywhere on the database control panel. i am a...
221
367340
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 needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
0
8253
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8354
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.