[h2] Re: Usage of 'classpath:/filename.csv' - SQLSyntax and IOExceptions

2017-12-07 Thread Alan Freeman
The following code seems to work; so it's possible to use a URL, but not 
the documented classpath: protocol. I haven't tried reconfiguring this to 
run from a jar as yet, which is the object of the exercise.

String url = getClass().getClassLoader().getResource(
"timeStamps.csv").getPath(); // Gets the path of the class or jar.
if (url.startsWith("/")) url= url.substring(1, url.length());
System.out.println("URL of timeStamps.csv is:"+url);
String queryString="INSERT INTO timestamps (timeStampID, projectFK, 
Description) SELECT TIMESTAMPID, PROJECTFK, DESCRIPTION FROM CSVREAD('"+url+
"');";
System.out.println("Query string is:"+queryString);
executeQuery(queryString,s);



On Wednesday, December 6, 2017 at 3:53:42 PM UTC-6, Alan Freeman wrote:
>
> Hi
> I'm tryng to populate a small database using H2's CSVREAD function, using 
> the syntax described in http://www.h2database.com/html/functions.html.
> I would like to reference my data files by placing them in the classpath, 
> so that I do not need to refer to an absolute file location and can produce 
> a runnable jar.
>
> Up until now I am working with eclipse link. I first set a variable 
> @FILELOCATION with the following code:
>
> and execute the following three statements without problems:
>
> String setFilesLocationQuery = "SET @FILELOCATION ='" + 
> userFilesLocation+"'";
> try {
> Statement fl;
> fl = conn.createStatement();
> fl.executeUpdate(setFilesLocationQuery);
> } catch (SQLException e) {
> logger.info("Could not create the file locator statement 
> because/" + e.getMessage() + "/n");
> return;
> }...
>
>
> Iterator it = batchQuery.iterator();
> while (it.hasNext()) {
> String nextQuery = it.next();
> 
> logger.info("Initialisation: processing the following query\n 
>  " + nextQuery + "\n");
> try {
> s.executeUpdate(nextQuery);
> } catch (Exception f) {
> logger.debug("Query failed because:\n" + f.getMessage());
> f.printStackTrace();
> }
> }
>
> Batch query is an ArrayList in which each element is a query. 
> userFileLocation is set to the directory of the Eclipse project in which 
> the .csv files reside viz:C:\Users\{ME}\eclipse-workspace\{MYPROJECT}\daata
>
> the following  statements execute fine:
>
> CREATE TABLE timeStamps (`timeStampID` int Default 1 NOT NULL, `projectFK` 
> INT default 1 NOT NULL, `Description` VARCHAR(30) default NULL, PRIMARY 
> KEY (`timeStampID`,projectFK)); 
> INSERT INTO timestamps (timeStampID, projectFK, Description) SELECT 
> TIMESTAMPID, PROJECTFK, DESCRIPTION FROM CSVREAD(CONCAT(@FILELOCATION,
> 'timeStamps.csv'));
>
> However, I don't think I can use this technique to produce a runnable 
> jar,  so I tried using the classpath: syntax. However, I get errors of one 
> kind or another for all the combinations I have tried.
>
> Substitutiing CSVREAD('classpath:timeStamps.csv') gives me a Syntax Error. 
> the Stacktrace is
>
> Syntax error in SQL statement " INSERT INTO TIMESTAMPS (TIMESTAMPID, 
> PROJECTFK, DESCRIPTION) SELECT TIMESTAMPID, PROJECTFK, DESCRIPTION FROM 
> CSVREAD('classpath:timeStamps.csv'))[*]; "; SQL statement:
>  INSERT INTO timestamps (timeStampID, projectFK, Description) SELECT 
> TIMESTAMPID, PROJECTFK, DESCRIPTION FROM CSVREAD(
> 'classpath:timeStamps.csv')); [42000-195]
> org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement " INSERT INTO 
> TIMESTAMPS (TIMESTAMPID, PROJECTFK, DESCRIPTION) SELECT TIMESTAMPID, 
> PROJECTFK, DESCRIPTION FROM CSVREAD('classpath:timeStamps.csv'))[*]; "; 
> SQL statement:
>  INSERT INTO timestamps (timeStampID, projectFK, Description) SELECT 
> TIMESTAMPID, PROJECTFK, DESCRIPTION FROM CSVREAD(
> 'classpath:timeStamps.csv')); [42000-195][CAPITALISM] DBHandler - 
> Initialisation: processing the following query
>insert into projects (ProjectID, Description) select ProjectID, 
> Description  from CSVREAD(CONCAT(@FILELOCATION,'\\projects.csv'));
>
>
> at org.h2.message.DbException.getJdbcSQLException(DbException.java:345
> )
> at org.h2.message.DbException.get(DbException.java:179)
> at org.h2.message.DbException.get(DbException.java:155)
> at org.h2.message.DbException.getSyntaxError(DbException.java:191)
> at org.h2.command.Parser.getSyntaxError(Parser.java:533)
> at org.h2.command.Parser.prepareCommand(Parser.java:260)
> at org.

Re: [h2] Usage of 'classpath:/filename.csv' - SQLSyntax and IOExceptions

2017-12-07 Thread Alan Freeman
Hi,I'm using the syntax in the documentation, as mentioned: this is at 
http://www.h2database.com/html/functions.html.Under the section headed 
'CSVREAD" it states:

"Instead of a file, an URL may be used, for example 
jar:file:///c:/temp/example.zip!/org/example/nested.csv. To read a stream 
from the classpath, use the prefix classpath:. To read from HTTP, use the 
prefix http: (as in a browser)."

It then gives the following examples: 

CALL CSVREAD('test.csv');
-- Read a file containing the columns ID, NAME with
CALL CSVREAD('test2.csv', 'ID|NAME', 'charset=UTF-8 fieldSeparator=|');
SELECT * FROM CSVREAD('data/test.csv', null, 'rowSeparator=;');
-- Read a tab-separated file
SELECT * FROM CSVREAD('data/test.tsv', null, 'rowSeparator=' || CHAR(9));
SELECT "Last Name" FROM CSVREAD('address.csv');
SELECT "Last Name" FROM CSVREAD('classpath:/org/acme/data/address.csv');

I think this is quite recent. I am using H2 version 1.4.195 and I believe 
the classpath: protocol was introduced in version 1.3

On Thursday, December 7, 2017 at 12:21:56 AM UTC-6, Noel Grandin wrote:
>
> Sorry, CSVREAD does not support URL's or anything smarter than a normal 
> file path.​
>

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to h2-database+unsubscr...@googlegroups.com.
To post to this group, send email to h2-database@googlegroups.com.
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.