Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a .txt file where data present in below format:
[customer] customer_id=121 # this is primary key. customer_name=abc customer_age=25 [item] item_id=222 # this is primary key. sugar=100 stock=yes
Here 'customer' and 'item' are table names, this is external .txt file. I have to read this file and write the update statement to update the customer and update the corresponding values of table, the same applicable to to 'item' table as well and this I have to do based on primary key I have mentioned in the .txt file. what is best approach to do this.

Replies are listed 'Best First'.
Re: reading txt and update into database table
by GrandFather (Saint) on Jul 07, 2015 at 11:49 UTC

    You may find Databases made easy helps get you up to speed enough with database management using Perl to figure this stuff out.

    When you have some code that you need some help with come back and show us where the problems are. When you show you've put some effort in yourself you'll find the monks are very forthcoming with help, but we won't generally do your homework for you.

    Premature optimization is the root of all job security
Re: reading txt and update into database table
by CountZero (Bishop) on Jul 07, 2015 at 12:16 UTC
    This text file looks very much like an ".INI" configuration file.

    Config::INI or Config::Tiny can read (and write) this type of files easily and transform the contents into a Perl data-structure.

    Such data-structure can then be translated into an SQL statement very easily which DBI will use to update your database.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: reading txt and update into database table
by kcott (Archbishop) on Jul 07, 2015 at 12:19 UTC

    You've provided no indication of your level of Perl expertise or the context of the project you're working on. Accordingly, I'll assume the lowest common denominators for both: minimal Perl knowledge and a small (personal/hobby/learning) project.

    Read "perlintro: Perl introduction for beginners". Each section is fairly basic but also provides links to more detailed information and advanced topics: follow these as required.

    For "I have to read this file", you'll want to focus on the "Files and I/O" section in that document.

    For your database work, look at DBI. This isn't a core (builtin) module so you'll need to install it: you can use the builtin cpan utility for that task.

    You'll also need a database driver module (and you'll need to install that as well). These modules have names like DBD::xxx, where xxx will be something like Oracle. You provided no information about the database system you're using; search CPAN for an appropriate "DBD:: module".

    At some later point, having done all this, you may require further assistance. To get the best and quickest responses, follow the guidelines in "How do I post a question effectively?".

    -- Ken

Re: reading txt and update into database table
by 1nickt (Canon) on Jul 07, 2015 at 11:46 UTC

    Please show the code you have tried, and why it doesn't work.

    Please disclose if this is a homework assignment.

    Look into SQL placeholders. You want to be doing something like:

    #!/usr/bin/env/perl use strict; use warnings; use DBI; use File::Slurp::Tiny 'read_file'; # pseudo code, obvi $dbh = get_database_handle_using_DBI() make_SQL_Statement_with_placeholders() read_conf_file_Using_File::Slurp::Tiny() parse_conf_file_and_put_data_into_a_hash() foreach (table_you_got_from_the_file) { do_SQL_statement_with_the_hash_data() } # Don't forget to use strict and warnings, and check all your return v +alues!
    Remember: Ne dederis in spiritu molere illegitimi!
Re: reading txt and update into database table
by erix (Prior) on Jul 07, 2015 at 11:41 UTC

    what is best approach to do this.

    what DBMS. is this homework.