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

putting data from file to mysql...

14
Helo,

I found at forum somebodys post:
I'm trying to parse an input file and then take data from that file and enter it into a MySQL database. I'm separating the fields with tabs '\t' and ending the records with a newline '\n'. I'm using the LOAD DATA function (see the code snippet below).

Expand|Select|Wrap|Line Numbers
  1. # Load the data into the history table
  2. my $sql_statmnt2 = "LOAD DATA LOCAL INFILE \'$history_file\'
  3. INTO TABLE History";
  4. print "$sql_statmnt2 \n";
  5. $sth2 = $dbh->prepare($sql_statmnt2);
  6. $sth2->execute();
  7. $sth2->finish();
However, I only get the first record not all of them. I've looked at the file with an editor that shows invisible characters and the tabs and newlines show up.

Thanks!

I have the same problem...

my file is temp1.txt , doses it mean I put it in a script as a variable?just like above? does any one know how to solve it?
when I try this script it doesn't work at all
what am I doing wrong please help!I would be grateful very very much...

my script is...(basing on the above):

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use CGI qw(:standard);
  4. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  5. use DBI;
  6.  
  7. # BD connection----------------------------------------
  8. my $dbh = DBI->connect('dbi:mysql:monitoring','root','passwd')
  9. or die "Connection Error: $DBI::errstr\n";
  10.  
  11. my $sql_statmnt2 = "LOAD DATA LOCAL INFILE \'$temp1.txt\'
  12. INTO TABLE room1_temperature";
  13. print "$sql_statmnt2 \n";
  14. $sth2 = $dbh->prepare($sql_statmnt2);
  15. $sth2->execute();
  16. $sth2->finish();
----------------------------------------------

\'$temp1.txt\' or \'$temp1\' without a file type?
Dec 4 '07 #1
14 1775
Megi
14
aaaaaaaa what more:

I put the script in /usr/lib/cgi-bin/ as all my perl and cgi scripts...so where to put a file temp1.txt ???I have put it in the same directory as script...

my errors form command line:

Expand|Select|Wrap|Line Numbers
  1. madzia@madzia-laptop:/usr/lib/cgi-bin$ perl loader.pl
  2. [Tue Dec  4 14:51:15 2007] loader.pl: Name "main::temp1" used only once: possible typo at loader.pl line 13.
  3. [Tue Dec  4 14:51:15 2007] loader.pl: Use of uninitialized value in concatenation (.) or string at loader.pl line 13.
  4. LOAD DATA LOCAL INFILE '.txt'
  5. INTO TABLE room1_temperature 
  6. [Tue Dec  4 14:51:15 2007] loader.pl: DBD::mysql::st execute failed: File '.txt' not found (Errcode: 2) at loader.pl line 17.
  7.  
would be grateful for help, thanks!!!!
Dec 4 '07 #2
numberwhun
3,509 Expert Mod 2GB
aaaaaaaa what more:

I put the script in /usr/lib/cgi-bin/ as all my perl and cgi scripts...so where to put a file temp1.txt ???I have put it in the same directory as script...

my errors form command line:

Expand|Select|Wrap|Line Numbers
  1. madzia@madzia-laptop:/usr/lib/cgi-bin$ perl loader.pl
  2. [Tue Dec  4 14:51:15 2007] loader.pl: Name "main::temp1" used only once: possible typo at loader.pl line 13.
  3. [Tue Dec  4 14:51:15 2007] loader.pl: Use of uninitialized value in concatenation (.) or string at loader.pl line 13.
  4. LOAD DATA LOCAL INFILE '.txt'
  5. INTO TABLE room1_temperature 
  6. [Tue Dec  4 14:51:15 2007] loader.pl: DBD::mysql::st execute failed: File '.txt' not found (Errcode: 2) at loader.pl line 17.
  7.  
would be grateful for help, thanks!!!!
Ok, because you are using the -w switch (which is essentially the same as saying "use warnings;", Perl becomes rather picky with regards to syntax and other check points. You will need to correct these issues before your script will move on.

For instance, on line 11, you define $temp1.txt, yet nowhere before this point is $temp1 defined anywhere. This is more than likely causing line 13 to error out since it couldn't completely set the variable from line 11 due to that variable not existing.

You will need to correct these small error(s) before proceeding.

Regards,

Jeff
Dec 4 '07 #3
Megi
14
Helo Jeff,

You are completly right! I have corrected the errors and it works like almost like I wanted to..

here is the script:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use CGI qw(:standard);
  3. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  4. use DBI;
  5. $foo = new CGI;
  6.  
  7. my @row;
  8. print $foo->header;
  9. $temp1 = 'temp1.txt';
  10.  
  11. # BD connection----------------------------------------
  12. my $dbh = DBI->connect('dbi:mysql:monitoring','root','passwd')
  13. or die "Connection Error: $DBI::errstr\n";
  14. my $sql = "select * from room1_temperature";
  15. my $sth = $dbh->prepare($sql);
  16. $sth->execute
  17. or die "SQL Error: $DBI::errstr\n";
  18.  
  19.  $sql_statmnt2 = "LOAD DATA LOCAL INFILE \'$temp1\'
  20. INTO TABLE room1_temperature (temperature)";
  21. print "$sql_statmnt2 \n";
  22. $sth2 = $dbh->prepare($sql_statmnt2);
  23. $sth2->execute();
  24. $sth2->finish();
  25.  
it works great...the only defect is that it rewrites records when I reload the script..and I need to have it refresh all the time becouse it should work like a real time...
How to avoid rewriting the same data to mysql? Is any command that will do it?
Maybe have any idea?????
Thanks a lot...
Dec 4 '07 #4
numberwhun
3,509 Expert Mod 2GB
Helo Jeff,

You are completly right! I have corrected the errors and it works like almost like I wanted to..

here is the script:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use CGI qw(:standard);
  3. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  4. use DBI;
  5. $foo = new CGI;
  6.  
  7. my @row;
  8. print $foo->header;
  9. $temp1 = 'temp1.txt';
  10.  
  11. # BD connection----------------------------------------
  12. my $dbh = DBI->connect('dbi:mysql:monitoring','root','passwd')
  13. or die "Connection Error: $DBI::errstr\n";
  14. my $sql = "select * from room1_temperature";
  15. my $sth = $dbh->prepare($sql);
  16. $sth->execute
  17. or die "SQL Error: $DBI::errstr\n";
  18.  
  19.  $sql_statmnt2 = "LOAD DATA LOCAL INFILE \'$temp1\'
  20. INTO TABLE room1_temperature (temperature)";
  21. print "$sql_statmnt2 \n";
  22. $sth2 = $dbh->prepare($sql_statmnt2);
  23. $sth2->execute();
  24. $sth2->finish();
  25.  
it works great...the only defect is that it rewrites records when I reload the script..and I need to have it refresh all the time becouse it should work like a real time...
How to avoid rewriting the same data to mysql? Is any command that will do it?
Maybe have any idea?????
Thanks a lot...
Unfortunately, that I have no idea about and am hoping one of the experts can assist you with that.

What I would like to say though, is to please be sure and use the proper code tags around your code, that way the Moderators, like myself, do not have to clean up your threads.

Regards,

Jeff
Dec 4 '07 #5
Megi
14
Unfortunately, that I have no idea about and am hoping one of the experts can assist you with that.

What I would like to say though, is to please be sure and use the proper code tags around your code, that way the Moderators, like myself, do not have to clean up your threads.

Regards,

Jeff
Thanks anyway, I will follow Your tips about the code tags.
By the way how can I contact with the experts:)
Dec 4 '07 #6
KevinADC
4,059 Expert 2GB
If I knew the answer I would help but I don't. Perl is only the interface to the database, the problem is with your sql statements, not perl. I suggest you ask on the MySQL database forum why the data is getting over-written.
Dec 4 '07 #7
numberwhun
3,509 Expert Mod 2GB
Thanks anyway, I will follow Your tips about the code tags.
By the way how can I contact with the experts:)
Just by posting a thread as you have done. As you see, KevinADC has replied.

Regards,

Jeff
Dec 4 '07 #8
Megi
14
If I knew the answer I would help but I don't. Perl is only the interface to the database, the problem is with your sql statements, not perl. I suggest you ask on the MySQL database forum why the data is getting over-written.

I did as You told me,

so I have a different question, I hope You could help me out with that...the same problem but different way...how about using perl ..
1. open file read the data
2. insert it into mysql table
3. after inserting data into mysql table delete data form file which are sended
than new data ocurrs in the file and I would avoid inserting the same, but how know which data has been inserted?

could You help me with the script if it would work at all the way I think....
i am waiting for answer.Thanks a lot.
Dec 4 '07 #9
numberwhun
3,509 Expert Mod 2GB
I did as You told me,

so I have a different question, I hope You could help me out with that...the same problem but different way...how about using perl ..
1. open file read the data
2. insert it into mysql table
3. after inserting data into mysql table delete data form file which are sended
than new data ocurrs in the file and I would avoid inserting the same, but how know which data has been inserted?

could You help me with the script if it would work at all the way I think....
i am waiting for answer.Thanks a lot.
Well, imho, if you have data that is coming in, and the same script is reading it to add it to the database, then you would have to have some routine in there to check each line of data against the database to see if the data is identical or not.

Sounds like a new subroutine is in order to me.

Regards,

Jeff
Dec 4 '07 #10
KevinADC
4,059 Expert 2GB
I think I misunderstood your question. For some reason I read "overwrites" but you said "rewrites". I guess what you are trying to do is avoid duplicate data in the database. The answer might still be an SQL one so wait and see if someone in the MySQL forum has a suggestion.
Dec 4 '07 #11
Megi
14
I think I misunderstood your question. For some reason I read "overwrites" but you said "rewrites". I guess what you are trying to do is avoid duplicate data in the database. The answer might still be an SQL one so wait and see if someone in the MySQL forum has a suggestion.

Yes, I was saying about re-writing.
Nobody answerd on Mysql , so I have to change my way of work...now for perl..

I have a file temp1.txt and I open it and write it's content into temp1b1.txt - it would be my backup. the whole script is relading each 10s becouse file temp1.txt will be getting new values constantly..

now I need to delete or truncate data from file tem1.txt which are written to temp1b.txt , I have a problem with that , I know truncate function needs length of my file but I don't know how to use it and how to do it..

mayby with that You could help me, would be grateful.Thanks!

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use CGI qw(:standard);
  4. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  5.  
  6. while('true')
  7. {
  8.     my $infile = 'temp1.txt';
  9.     my $outfile = 'temp1b1.txt';
  10.  
  11.     open IN, "< $infile" or die "Can't open $infile : $!";
  12.     open OUT, ">> $outfile" or die "Can't open $outfile : $!";
  13.     print OUT <IN>;
  14.     close IN;
  15.     close OUT;
  16.     open IN "> $infile"or die"can't open $infile: $!";
  17.     truncate(....?)
  18.     ?????
  19.     sleep 10;
  20. }
  21.  
the former script will do writing to DB, but this one has to make a backup and delete backuped values...
Dec 4 '07 #12
eWish
971 Expert 512MB
Can you show the schema for your database ( ie: tables, types, index etc....) one(s) table(s) you are having problems with?

--Kevin
Dec 5 '07 #13
eWish
971 Expert 512MB
This addresses your second inquiry. The concept behind this is simple. Entering the contents of an array (contents of a file) into the db. I was taught that it is more efficient to replace the entries rather than check to see if an entry already exists. So with that in mind here is how it can be done.

This is untested code, but should work. You will have to modify this to your needs to work as you need it to. I don't know what your data looks like so this is just a generic example. Also, this is executed from the browser. Which is easier for me. You will have to modify if you are running from the command line.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. use CGI::Carp qw/fatalsToBrowser/;
  8.  
  9. my $q = CGI->new;
  10.  
  11. print $q->header; 
  12. print $q->start_html(); 
  13.  
  14.  
  15. my $db_vars = ( 
  16.     dbname   =>  'xxx',
  17.     servername  =>  'xxx',
  18.     username  => 'xxx',
  19.     password  => 'xxx',
  20.     );
  21.  
  22.  
  23. my $data_source = 'DBI:mysql:' . $db_vars{dbname} . ':' . $db_vars{servername};
  24. my $dbh = DBI->connect( $data_source, $db_vars{username}, $db_vars{password}, {PrintError=>1});
  25.  
  26.  
  27. my $data_file = 'path/to/file';
  28.  
  29.  
  30. my $replace = $dbh->prepare('REPLACE INTO table_name(column1) VALUES(?)');
  31. my $check  = $dbh->prepare('SELECT column1 FROM table_name WHERE column1=?');
  32.  
  33.  
  34. open (my $DATAFILE, '<', $file) || die "Can't open $file: $!";
  35.  while (<$DATAFILE>) {
  36.  
  37.   chomp;
  38.    my @contents = split(/\n/);
  39.  
  40.    foreach my $content(@contents) {
  41.  
  42.         if( my ($existing_count) = $dbh->selectrow_array($check, undef, ( $content ))){
  43.                 $replace->execute($content);
  44.          } else {
  45.                 $replace->execute($content);
  46.          }
  47.   }   
  48.  
  49.  $check->finish();
  50.  $replace->finish(); 
  51.  
  52.  
  53.  }
  54.  
  55. close ($DATAFILE);
  56.  
  57. print 'Done, go get the db and see if it worked.';
  58.  
  59. print $q->end_html();
  60.  
  61. 1;
I hope I understood you problem correctly.

--Kevin
Dec 6 '07 #14
eWish
971 Expert 512MB
This addresses your last question. You can do the following to clear or truncate an existing file.

Expand|Select|Wrap|Line Numbers
  1.    open ($CLEAR_FILE, '>', $temp_file_to_clear) || print "Error clearing $temp_file_to_clear: $!";
  2. close ($CLEAR_FILE);
--Kevin
Dec 6 '07 #15

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

Similar topics

4
by: Eric Kincl | last post by:
Hello, its been a while since I posted/looked here... my normal email client doesn't handle newsgroups :( (ximian evolution) I was wondering how you stick a file into a database, and then...
14
by: Bruce A. Julseth | last post by:
When I execute this SQL statement in my PHP code, I get an error "File '.\Address.txt' not found (Errcode: 2)" $File = addslashes(".\Address.txt"); $SQL = "Load Data InFile \"" . $File . "\"...
5
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...
0
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to...
0
by: Luc Foisy | last post by:
Last week many of our server and client servers had a power problem. Not = quite sure how the servers were handled, wasn't on site, but I don't = think some of these servers got shut down...
1
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason...
3
by: nsh | last post by:
mailing.database.mysql, comp.lang.php subject: does "LOAD DATA" EVER work?!? I've tried EVERYTHING! version info: my isp is running my web page on a linux box with php ver. 4.4.1 according to...
3
by: eieiohh | last post by:
MySQL 3.23.49 PHP 4.3.8 Apache 2.0.51 Hi All! Newbie.. I had a CRM Open Source application installed and running. Windows Xp crashed. I was able to copy the contents of the entire hard...
2
by: David Dawson | last post by:
I have forgotten a lot about SQL and would like to be (gently) reminded how to do this: In a MySQL query on the database (one table with 15 variable length fields, I want to put each field into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.