473,888 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

putting data from file to mysql...

14 New Member
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 1827
Megi
14 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Specialist
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 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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

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

Similar topics

4
3245
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 retrive it again for the user with PHP/MySQL. I tried the following which apparently didnt work... Very quick overview of what I did... html
14
9770
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 . "\" into table addresses"; $result = mysql_query($SQL) or die(mysql_error()); The file is located in the same directory as my .PHP file. How do I generate a relative address for this file so that it can be found?
5
3282
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
6693
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 be able to place the PHP file on the server, and I guess you probably can't do that either. Talk about catch 22... The only other way I can think of is to install MySQL on a machine you control, then import the data there using the method I...
0
1744
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 gracefully. but anyways that = shouldn't matter to my question I ran myisamchk on the data directories and I get a large report = containing things such as myisamchk: MyISAM file /usr/data/mysql/qbslive/MANIFESTSPOOL.MYI myisamchk: warning: 1 clients...
1
16094
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 is for the sake of security. What does that mean ?
3
2767
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 phpinfo, the "mysql api client is ver. 4.0.25" - I have no idea how this relates, if at all, to the mysql engine's version. background:
3
5783
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 drive onto a USB External Hard Drive. I have to assume I also copied the data. I
2
6819
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 Bash variable so that I can handle each field as an entity. The query I have is something like this: mysql -e "use $database; select field1, field2, field3..., from Table1 where fieldN like '%something%';"
0
9961
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
9800
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
11178
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
10777
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
10882
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
10438
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...
0
9597
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3251
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.