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

LOAD DATA INFILE problem....

Hi guys,

I have got a question regarding LOAD DATA INFILE. Can some one save my day.

1. I want to import a txt file into mysql database.
when I use the following query in the mysql> prompt its fine.
which is

when I do it java I am getting error

Error
-----------
java.sql.sqlException: General error message from server:
"File 'D:/Sen.txt' not found <ErrCode: 2>"


But The file already exists

Code:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.            con = DriverManager.getConnection(url, "root", "");
  3.             stmt = con.createStatement();
  4.             stmt.executeUpdate(createString);
  5.             String filename = "D:/Sen.txt";
  6.             String tablename = "IMPORT";
  7. stmt.executeUpdate("LOAD DATA INFILE \"" + filename + "\" INTO TABLE " + tablename + " FIELDS TERMINATED BY ','");
  8.             stmt.close();
  9.             con.close();
  10. }
My text file(Sen.txt) is like this...

abc,1
bcd,2
adf,3

I dont know why its say like that
Can anyone help me out in this...

thanks...
senthil.
Feb 13 '07 #1
9 3474
Hi guys,

I have got a question regarding LOAD DATA INFILE. Can some one save my day.

1. I want to import a txt file into mysql database.
when I use the following query in the mysql> prompt its fine.
which is

when I do it java I am getting error

Error
-----------
java.sql.sqlException: General error message from server:
"File 'D:/Sen.txt' not found <ErrCode: 2>"


But The file already exists

Code:
Expand|Select|Wrap|Line Numbers
  1. try {
  2.            con = DriverManager.getConnection(url, "root", "");
  3.             stmt = con.createStatement();
  4.             stmt.executeUpdate(createString);
  5.             String filename = "D:/Sen.txt";
  6.             String tablename = "IMPORT";
  7. stmt.executeUpdate("LOAD DATA INFILE \"" + filename + "\" INTO TABLE " + tablename + " FIELDS TERMINATED BY ','");
  8.             stmt.close();
  9.             con.close();
  10. }
My text file(Sen.txt) is like this...

abc,1
bcd,2
adf,3

I dont know why its say like that
Can anyone help me out in this...

thanks...
senthil.

hi dude try this code
try {
Connection con = // getConnection here
// Create the statement
Statement stmt = con.createStatement();
String tablename = "IMPORT";
String filename = "D:\\\\Sen.txt";
String query = "LOAD DATA INFILE \"" + filename + "\" INTO TABLE " + tablename;
stmt.executeUpdate(query);
} catch (Exception e) {
}
Feb 13 '07 #2
r035198x
13,262 8TB
Hi guys,

I have got a question regarding LOAD DATA INFILE. Can some one save my day.

1. I want to import a txt file into mysql database.
when I use the following query in the mysql> prompt its fine.
which is

when I do it java I am getting error

Error
-----------
java.sql.sqlException: General error message from server:
"File 'D:/Sen.txt' not found <ErrCode: 2>"


But The file already exists

Code:
Expand|Select|Wrap|Line Numbers
  1. try {
  2. con = DriverManager.getConnection(url, "root", "");
  3. stmt = con.createStatement();
  4. stmt.executeUpdate(createString);
  5. String filename = "D:/Sen.txt";
  6. String tablename = "IMPORT";
  7. stmt.executeUpdate("LOAD DATA INFILE \"" + filename + "\" INTO TABLE " + tablename + " FIELDS TERMINATED BY ','");
  8. stmt.close();
  9. con.close();
  10. }
My text file(Sen.txt) is like this...

abc,1
bcd,2
adf,3

I dont know why its say like that
Can anyone help me out in this...

thanks...
senthil.
Must be some directory thing. I've never used it in Java myself. A quick check here shows that you have it right for running from mysql prompt. Java looks for the file starting in the current directory where your code is but mysql looks for the file from the database directory... Still trying to fihure out what would work there if you get it please post and tell us as well.
Feb 13 '07 #3
Must be some directory thing. I've never used it in Java myself. A quick check here shows that you have it right for running from mysql prompt. Java looks for the file starting in the current directory where your code is but mysql looks for the file from the database directory... Still trying to fihure out what would work there if you get it please post and tell us as well.
Hey another one things...

But when i tried the filename = "C:/test.txt"

itz import the data from the database and load it to the text file perfectly..

but whatz special in this is.... there is no test.txt file in the C:/

Actually test.txt file is a created file by using Query

SELECT * INTO OUTFILE in the java program for export data to a file(C:/test.txt)..

When i run the above program...

there is no test.txt file in the C:/....

But when i tried to run the program again... its throwing exception

that C:/test.txt file is already exist... But there is no test.txt file..

i dont know what is happening here...

What to do for it...

plzz explain me....
Feb 13 '07 #4
r035198x
13,262 8TB
Hey another one things...

But when i tried the filename = "C:/test.txt"

itz import the data from the database and load it to the text file perfectly..

but whatz special in this is.... there is no test.txt file in the C:/

Actually test.txt file is a created file by using Query

SELECT * INTO OUTFILE in the java program for export data to a file(C:/test.txt)..

When i run the above program...

there is no test.txt file in the C:/....

But when i tried to run the program again... its throwing exception

that C:/test.txt file is already exist... But there is no test.txt file..

i dont know what is happening here...

What to do for it...

plzz explain me....
When you run it first time it creates the file right?
When you run it second time, do you delete that file first?
Feb 13 '07 #5
When you run it first time it creates the file right?
When you run it second time, do you delete that file first?
Ya it is creating a file..... But it is not storing in c:/

i dont know where it is storing......

the file is storing in some other location...

the file is in invisible mode....
Feb 13 '07 #6
r035198x
13,262 8TB
Ya it is creating a file..... But it is not storing in c:/



i dont know where it is storing......



the file is storing in some other location...



the file is in invisible mode....


Let us eliminate all the other possibilities first.



Expand|Select|Wrap|Line Numbers
  1.  } catch (Exception e) {
  2. }
is not handling any exception. Change it to



Expand|Select|Wrap|Line Numbers
  1.  } catch (Exception e) { 
  2.  
  3. e.printStackTace();
  4.  
  5.  
  6. }
  7.  
  8.  




and run it to make sure the program is not throwing any exceptions
Feb 13 '07 #7
Now also the same result....

i will explain u clearly..

When i want a export data from a database into a text file..

i used the query, SELECT * INTO OUTFILE 'C:/test.txt' FROM TABLE....

there is no test.txt file in C:/..i cant see it...

But when i run the program again, it throwing an exception that
C:/test.txt already exists....

thats what, i came to the conclusion that, the above query creating a file

and exporting the data into that file... but i think that created file is in

invisible mode...

Now i'm trying to importing data from database from a file...

in this i had used the following query...

stmt.executeUpdate("LOAD DATA INFILE \"" + 'C:/test.txt' + "\" INTO TABLE " + tablename + " FIELDS TERMINATED BY ','");

its working perfectly running.... and importing the data from the test.txt file into

the database...

But whats special in this is.... there is no test.txt file in C:/


Can u understand what i'm saying?

Plzz tell me a solution for this....

thanks for replying...
senthil.
Feb 13 '07 #8
r035198x
13,262 8TB
Now also the same result....



i will explain u clearly..



When i want a export data from a database into a text file..



i used the query, SELECT * INTO OUTFILE \'C:/test.txt\' FROM TABLE....



there is no test.txt file in C:/..i cant see it...



But when i run the program again, it throwing an exception that

C:/test.txt already exists....



thats what, i came to the conclusion that, the above query creating a file



and exporting the data into that file... but i think that created file is in



invisible mode...



Now i\'m trying to importing data from database from a file...



in this i had used the following query...



stmt.executeUpdate(\"LOAD DATA INFILE \\\"\" + \'C:/test.txt\' + \"\\\" INTO TABLE \" + tablename + \" FIELDS TERMINATED BY \',\'\");



its working perfectly running.... and importing the data from the test.txt file into



the database...



But whats special in this is.... there is no test.txt file in C:/





Can u understand what i\'m saying?



Plzz tell me a solution for this....



thanks for replying...

senthil.


Maybe some blocking as explained here..Wait a minute. I think this is taking the shape of a mysql problem so let me copy your post there as well so you can be able to view replies posted from there and here as well.
Feb 13 '07 #9
ronverdonk
4,258 Expert 4TB
As for the file definition, have a look at the MySQL decoumentation regarding the use of file names with and without specifying the LOCAL attribute in your LOADFILE statement:
The LOCAL keyword, if specified, is interpreted with respect to the client end of the connection:

If LOCAL is specified, the file is read by the client program on the client host and sent to the server. The file can be given as a full pathname to specify its exact location. If given as a relative pathname, the name is interpreted relative to the directory in which the client program was started.

If LOCAL is not specified, the file must be located on the server host and is read directly by the server. The server uses the following rules to locate the file:

If the filename is an absolute pathname, the server uses it as given.

If the filename is a relative pathname with one or more leading components, the server searches for the file relative to the server's data directory.

If a filename with no leading components is given, the server looks for the file in the database directory of the default database.

Note that, in the non-LOCAL case, these rules mean that a file named as ./myfile.txt is read from the server's data directory, whereas the file named as myfile.txt is read from the database directory of the default database. For example, if db1 is the default database, the following LOAD DATA statement reads the file data.txt from the database directory for db1, even though the statement explicitly loads the file into a table in the db2 database:

LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
Windows pathnames are specified using forward slashes rather than backslashes. If you do use backslashes, you must double them.

For security reasons, when reading text files located on the server, the files must either reside in the database directory or be readable by all. Also, to use LOAD DATA INFILE on server files, you must have the FILE privilege.
Ronald :cool:
Feb 13 '07 #10

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

Similar topics

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 . "\"...
3
by: Otto | last post by:
Hello to all I have a problem how to write correctly a PHP string. I did the following import with the help of PHPMyAdmin: Nombre d'enregistrements insérés : 364 (traitement: 0.0553 sec.)...
3
by: hall | last post by:
I have a problem with my design of a templatized class. I'm trying to figure out how to load and save the data inside it, but can't. My class looks like this ------------------------------------...
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...
1
by: Jami Bradley | last post by:
HI all, For the past several months we have been using LOAD DATA LOCAL INFILE to bulk load tables within Perl modules. Recently, someone thought it would be a good idea to upgrade the Solaris...
0
by: Rajesh Kapur | last post by:
Hello, I am running MySQL 4.0.16. I get the error 'The used command is not allowed with this MySQL version' when using the 'load data local infile' command. The local_infile is ON; the select *...
2
by: Alex Hunsley | last post by:
I'm using a mysql monitor under cygwin (on win xp) to do a 'load data infile' to put some data into a mysql database (I'm using the xampp bundle).. My problem is that I have a four line CSV file...
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...
5
by: Justin | last post by:
Hi, im facing a problem here. First of all here is my program requirement. I got a .csv file with thousands of records inside, i need to import them into my mysql database. So i tried using load...
0
by: lanesbalik | last post by:
hi all, right now i'm trying to migrate from db2 running under linux to mysql v5.1. i manage to export out the db2 structure & data into a del (ascii) file. but when i try to load the data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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...

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.