473,396 Members | 1,900 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,396 software developers and data experts.

Perl/mysql: retrieve id and name for record already in the database or create newname

Hi mysql and perl experts,

I am new to both perl and mysql, and I am trying to populate a database that I had previously created in mysql. I am stuck on a step and don't really know how to move on and would REALLY appreciate suggestions...
I need to insert new data on individuals which refers to another table (called "location") through a foreign key, but I first need to check whether this location has already been entered: if it has already been entered then take the location_id and use this to enter the individual data which refers to this location, otherwise create a new location_id. Then, if there is no location_name, then enter an invented one from the last_inserted id (which differentiates the ones that already had a name from the ones that did not).

This is what the mysql tables look like:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----Table_1-----------
LOCATION_ID (AUTO-INCREMENT) LOCATION SITE NAME

1 1234 4 syd
2 1235 4 brux
3 1236 5 temp_1

-----Table_2-----------
ASSIGN_ID (AUTO-INCREMENT) LOCATION_ID (FOREIGN KEY) ASSIGN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
And this is where I got stuck and what I am trying to do with the perl script:

I am looping through my data file and creating these variables for the columns in Table 1 and Table 2:
my $location = shift(@fields);
my $site = shift(@fields);
my $name = shift(@fields);
my $assign = shift(@fields);


#see if record exists
my($id, $location_name) = $dbh->selectrow_array(qq{SELECT location_id, location_name FROM location WHERE location = $location && site = $site });

## If there is already an entry, then use the location_id and name entered in Table 1 (and if the name is not entered already in the database, then enter this name) --- In this step I do not need to do anything in table_1, but I need to select this location_id (and name if there is one), maybe using a “selectrow_array”?

## If there is not an entry, then insert the new data into Table_1 using the “name” if there is one, and if there is no name entering the new data in Table_1 using a temporary name such as “temp_1”.
# I DO NOT KNOW HOW TO DO THIS....MAYBE SOMETHING LIKE THIS? ...
if(!$id){
my $sth = $dbh->do(qq{
INSERT location
SET location = $location, site = $site, location_name = '$location_name',
});
}
elsif(!$id){
my $sth = $dbh->do(qq{
UPDATE location
SET location_name = '$location_name'
WHERE location_id = $id
});
}

$dbh->do(q{INSERT INTO table_1 (location, site, name) VALUES (?, ?, ?)}, undef, $location, $site, $name);
# get the primary key value of the inserted record of Table 1:
my $location_id = $dbh->selectrow_array (qq (select last_insert_id() ));

##And if there is no name, then maybe do:
update location where location_id = last_insert location_name = "temp_$id"

## Then enter the new data for Table2, using either the newly entered location_id or the location_id that was fetched in step 3…

$dbh->do (qq (insert into table_2 (location_id, assign) values (?,?) ), undef, $location_id, $assign);


Thank you so much for any help!! I really appreciate any suggestions...
-f
Mar 8 '11 #1
2 2130
code green
1,726 Expert 1GB
Rather than all this code could you make use of
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO table (..) VALUES (..)
  2.   ON DUPLICATE KEY UPDATE ....
Mar 8 '11 #2
Hi code green,

Thank you for your reply.
I am not sure about the benefits of using the "on duplicate key update", and since I am quite confused I thought to work an "insert" even if it is not the most practical way...But I will check it out!
I could make the code work by creating the query using "selectrow_array" and then repeating all the conditions if there is an entry, and again if there is no entry...

-f
Mar 8 '11 #3

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

Similar topics

1
by: Matt | last post by:
how to get the last record from database without looping? Whenever the user need to insert a new record to the database, it just increment the id field by one from the last record. I tried...
4
by: Mmm_moo_cows | last post by:
Hi, I'm new to the world of mysql and i'm having alot of trouble with it. All i want to do is create a page with a response form, e.g. name etc and some radio buttons (part of a uni project,...
4
by: Jim in Arizona | last post by:
I'm wanting to do a simple controlled voting page. I too our webserver off anonymous and everyone who accesses the website is a domain authenticated user. I've already done some control structure...
4
by: Akinyemi | last post by:
I am currently using Visual Basic 6 to write a Payroll Program. I created a Database with one Table in Microsoft Access that ships with Office 2000. The name of the Database is "Payroll", and the...
11
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result =...
2
josie23
by: josie23 | last post by:
Egad, I'm not a coder/programmer by nature or occupation but understand things like html and css and a small amount of perl. So, basically, I'm a perl/mysql imbecile. But, I've been trying to...
1
by: raaaz | last post by:
Hi dears, I am adding a record in Access database using AddNew method of recordset. here is the code of the page .. <% Set RsLibBuild = Server.CreateObject ("ADODB.Recordset")...
3
by: Surya | last post by:
Dear All, I have problem on inserting a record to database..Although it looked easy.. i have caught up with following issue .. please go ahead and help me to find solution I Need to insert...
1
by: Carmine [www.thetotalsite.it] | last post by:
Hi, I am using ASP.Net 2.0 with the MySQL Connector .Net 5.1.2.2. I added these lines to the web.config to make the connector.net able to handle the SqlConnection object (and so to bind it to a...
10
by: puT3 | last post by:
I want to get the value from Product_Price field which it will display it record on a text box based on a product that is selected from a list box. Product Table Product_ID Product_Name...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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,...

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.