473,396 Members | 1,775 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.

How to generate SQL statements using Perl?

Hi people,

Suppose, my data is as below: (CSV format)

Expand|Select|Wrap|Line Numbers
  1.    'asd,asd,asd','123','123'
  2.    'asd','456','123'
Suppose my code is this:

Expand|Select|Wrap|Line Numbers
  1. $csv = "test.csv";
  2. open(DAT, $csv) || die("Cannot Open File");
  3.  
  4. while (<DAT>) {
  5.  
  6.     my @new  = ();
  7.  
  8.     push(@new, $+) while $_ =~ m{
  9.         "([^\"\\]*(?:\\.[^\"\\]*)*)",?
  10.  
  11.            |  ([^,]+),?
  12.  
  13.            | ,
  14.         }gx;
  15.  
  16.        push(@new, undef) if substr($_, -1,1) eq ',';
  17.        print "$_\n" foreach(@new);   
  18. }
The output result is:

Expand|Select|Wrap|Line Numbers
  1. asd,asd,asd
  2. 123
  3. 123
  4.  
  5. asd
  6. 456
  7. 123
How do i actually modify the code to allow my output to become:

" Insert into person_info VALUES ("asd,asd,asd",123,123) "
" Insert into person_info VALUES ("asd",456,123) "

Please help...
Aug 4 '08 #1
6 2252
eWish
971 Expert 512MB
This can be fine tuned to work exactly as you want, but it will at least give you a start.

Expand|Select|Wrap|Line Numbers
  1. my @new = qw(asd,asd,asd 123 234 asd asd 345 456);
  2. my $string = format_string(@new);
  3. my $query = qq{Insert into person_info VALUES ($string)};
  4.  
  5. print $query, "\n";
  6.  
  7. sub format_string {
  8.  
  9.     my $string;
  10.  
  11.     foreach (@_) {
  12.         if  (/[^\d]/) {
  13.             $string .= qq{"$_",};
  14.         } else {
  15.             $string .= qq{$_,};
  16.         }
  17.     }
  18.     return substr($string, 0, -1);    
  19. }
Output:
Expand|Select|Wrap|Line Numbers
  1. Insert into person_info VALUES ("asd,asd,asd",123,234,"asd","asd",345,456) 
--Kevin
Aug 4 '08 #2
Thank you Kevin. I finally able to finish my application. Thanks a million!!!
Aug 4 '08 #3
Hi all, please help a Perl newbie here...

Supposed my data (CSV format) is:
Expand|Select|Wrap|Line Numbers
  1. a,123,123
  2. b,234,234
  3. c,345,345
Supposed my code is:

Expand|Select|Wrap|Line Numbers
  1. $csv = "test.csv";
  2. open(DAT, $csv) || die("Cannot Open File");
  3.  
  4. while (<DAT>) {
  5.     my @new  = ();
  6.  
  7.     push(@new, $+) while $_ =~ m{
  8.         "([^\"\\]*(?:\\.[^\"\\]*)*)",?
  9.            |  ([^,]+),?
  10.            | ,
  11.         }gx;
  12.  
  13.     push(@new, undef) if substr($_, -1,1) eq ',';
  14.  
  15.     my $string = format_string(@new);
  16.     my $query = qq{Insert into person_info VALUES ($string)};
  17.     print $query, "\n"; 
  18. }  
  19.  
  20.  sub format_string {  
  21.         my $string;
  22.         foreach (@_) {
  23.             if  (/[^\d]/) {
  24.                 $string .= qq{"$_",};
  25.             } else {
  26.                 $string .= qq{$_,};
  27.             }
  28.         }
  29.         return substr($string, 0, -1); 
  30.     }
My result will be:
Expand|Select|Wrap|Line Numbers
  1. Insert into person_info VALUES ("a",123,"123")
  2. Insert into person_info VALUES ("b",234,"234")
  3. Insert into person_info VALUES ("c",345,"345")
This is exactly how i 1 it. BUT

if my data is:
Expand|Select|Wrap|Line Numbers
  1. a,1"2"3,123
  2. b,234,234
  3. c,345,3"4"5
my result will be:(which is disaster)
Expand|Select|Wrap|Line Numbers
  1. Insert into person_info VALUES ("a",1,2,3,"123")
  2. Insert into person_info VALUES ("b",234,"234")
  3. Insert into person_info VALUES ("c",345,3,4,5,"")
How can I modify to allow a result:
Expand|Select|Wrap|Line Numbers
  1. Insert into person_info VALUES ("a",1"2"3,"123")
  2. Insert into person_info VALUES ("b",234,"234")
  3. Insert into person_info VALUES ("c",345,"3"45"")
Please help me!!!
Aug 4 '08 #4
eWish
971 Expert 512MB
NOTSomebody,

I have merged your two threads are on the same subject. Please do not create new threads when you already have a thread that covers the same issues.

Also, please use the [CODE][/CODE] tags when posting code and sample data.

I have edited your posts for your and please do use the code tags in the future.

Thank You!

Kevin
Aug 4 '08 #5
eWish
971 Expert 512MB
You will just need to modify the code I gave you to do what you need. Give it a try and if you can't then post the sample code that you have tried and we will help you. What I gave you is a good foundation and would not need to be modified that much to achieve your desired result.

--Kevin
Aug 4 '08 #6
okay...i will try... thanks alot... sry coz im quite new to this forum so i dunno hw 2 use the code tag etc. sry, i will try 2 use all that in future. thanks alot.
Aug 4 '08 #7

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

Similar topics

15
by: Max | last post by:
Hi, I'm a perl programmer and am trying to learn PHP. So far I have figured out most of the differences, but have not been able to find out how to do the following: When running through a...
3
by: Xah Lee | last post by:
20050226 exercise: generate all possible pairings given a list that is a set partitioned into subsets, generate a list of all possible pairings of elements in any two subset. Example: ...
0
by: David.Tymon | last post by:
>Description: MySQL v4.1.0-alpha only allows a client to prepare a maximum of 254 statements. On the 255th mysql_prepare() call, a failure is returned with no information returned by...
1
by: Terri | last post by:
I'd I have a problem I'd like to post CREATE TABLE and INSERT statements that will create my table and insert data into the table. I can use the scripting feature in Enterprise Manager to...
6
by: jpetrang | last post by:
We use VS6.0 to support our automated build environment, and we'd like to move to something that is currently supported. One aspect of VS6.0 is part of the "Generate Makefile" feature, which it...
10
by: subhadip | last post by:
Hi, I want to generate New mail alert for any mail client at client side . I want to check if any new mail has arrived in my inbox or not . the mail client be anything . I want to do this for...
2
by: sainiamit25 | last post by:
Hi All, I have writte a perl code. The perl code simple do some calculation and based on that, it takes a specific action for a device. But i do not have any logging mechanism for this. By this i...
2
by: wweiy | last post by:
Hi all, I am now using C++ and perl in my project. Thus i require perl library fro doing so. Where can i download the source code to generate perl library like libperl.a, libIO.so etc Thanks for...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
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...

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.