我目前正在使用h2版本1.4.196。我有一个CSV文件,如下所示,
Status,message,code
1,hello,13
2,world,14
3,ciao,26
以下查询按预期工作&返回正确的状态值。
select "Status" from CSVREAD('myfile.csv',
'Status,message,code', null) ;
以下查询会产生异常。它似乎是文字字符串" Status"正在传递转换而不是由返回的值 执行查询。我不确定我对转换的理解是否正确 正确。请帮我修复此查询以返回所需的结果。提前谢谢。
select convert("Status", int) from CSVREAD('myfile.csv', 'Status,message,code', null) ;
Caused by: java.lang.NumberFormatException: For input string: "Status"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at org.h2.value.Value.convertTo(Value.java:940)
----------------------详细例外----------------------- -------
select convert("Status", int) from CSVREAD('myfile.csv',
'Status,message,code', null) ;
Data conversion error converting "Status"; SQL statement:
select convert("Status", int) from CSVREAD('myfile.csv',
'Status,message,code', null) [22018-196] 22018/22018 (Help)
org.h2.jdbc.JdbcSQLException: Data conversion error converting "Status"; SQL statement:
select convert("Status", int) from CSVREAD('myfile.csv',
'Status,message,code', null) [22018-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.value.Value.convertTo(Value.java:996)
at org.h2.expression.Function.getSimpleValue(Function.java:945)
at org.h2.expression.Function.getValueWithArgs(Function.java:1196)
at org.h2.expression.Function.getValue(Function.java:591)
at org.h2.command.dml.Select$LazyResultQueryFlat.fetchNextRow(Select.java:1459)
at org.h2.result.LazyResult.hasNext(LazyResult.java:79)
at org.h2.result.LazyResult.next(LazyResult.java:59)
at org.h2.command.dml.Select.queryFlat(Select.java:519)
at org.h2.command.dml.Select.queryWithoutCache(Select.java:625)
at org.h2.command.dml.Query.queryWithoutCacheLazyCheck(Query.java:114)
at org.h2.command.dml.Query.query(Query.java:371)
at org.h2.command.dml.Query.query(Query.java:333)
at org.h2.command.CommandContainer.query(CommandContainer.java:113)
at org.h2.command.Command.executeQuery(Command.java:201)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:186)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:164)
at org.h2.server.web.WebApp.getResult(WebApp.java:1380)
at org.h2.server.web.WebApp.query(WebApp.java:1053)
at org.h2.server.web.WebApp$1.next(WebApp.java:1015)
at org.h2.server.web.WebApp$1.next(WebApp.java:1002)
at org.h2.server.web.WebThread.process(WebThread.java:164)
at org.h2.server.web.WebThread.run(WebThread.java:89)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NumberFormatException: For input string: "Status"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at org.h2.value.Value.convertTo(Value.java:940)
... 22 more
答案 0 :(得分:2)
您使用它的方式是您定义列并阅读csv中的所有行,因此您也可以使用Status
获取第一行。然后你尝试将字符串Status
转换为显然导致exeption的整数。
相反,你不应该定义列,但让h2从csv的第一行获取它们:
select convert(status,int) from CSVREAD('myfile.csv');