I am trying to run insert statements using load data infile (also tried load data local infile) in my Java Program and I can't seem to figure out what's wrong. Either it's the SQL statement or the filename/path... I feel my filename/path is wrong. In Eclipse, I added a .txt file in the project folder.
I tried to get the path of the file by using getAbsolutePath but I still the error saying there's a syntax error in load.
Here's where I am trying to write the insert statement: I've also changed the " to ' in my statement because i've seen so many versions of how to write this. I changed the file.getabsolutepath in the part of the insert statement to just filename.
String filename = "studentData.txt";
String workingDirectory = System.getProperty("user.dir");
File file = new File(workingDirectory, filename);
System.out.println("Final filepath : " + file.getAbsolutePath());
sql = "LOAD DATA INFILE \"" + file.getAbsolutePath()
+ "\" INTO TABLE STUDENT FIELDS "
+ "TERMINATED BY ',' LINES TERMINATED BY '\\n' "
+ "(STUDENT_ID, NAME, MAJOR, GRADE);";
stmt.executeUpdate(sql);
}
Here is what my error is saying:
Final filepath : /Users/amnakhan/Documents/workspace/DatabaseExample/studentData.txt
Exception in thread "main" java.sql.SQLException: near "LOAD": syntax error
at org.sqlite.core.NativeDB.throwex(NativeDB.java:397)
at org.sqlite.core.NativeDB._exec(Native Method)
at org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)
at DatabaseAccessHelper.createTables(DatabaseAccessHelper.java:72)
at DatabaseAccessHelper.main(DatabaseAccessHelper.java:296)
My program was working fine.. if I hard code in the insert statements, the program works fine. So I know everything else is okay. I am trying to just load a text file and run the insert statements.
Any ideas?