-5

我的Java代码是:

conn = DriverManager.getConnection(url, "user", "password"); // line 1

stm = conn.createStatement(); // line 2

stm.execute("CREATE TEXT TABLE someTableName("NLID" VARCHAR(20),
 "Scheduled.Primary.Scripting.Code" VARCHAR(20), "Scheduled.Site" VARCHAR(20),
 "Scheduled.Location.Long.Name" VARCHAR(20),
 "primary_key_1644" int PRIMARY KEY)"); // line 3

stm.execute("SET TABLE someTableName SOURCE 
"/some.csv;ignore_first=true;all_quoted=true;shutdown=true""); // line 4

链接到 hsqldb 的 CSV 是:

NLID,Scheduled.Primary.Scripting.Code,Scheduled.Site,Scheduled.Location.Long.Name,primary_key_1644   
100,INMRSB,Shopping,Shopping General-Banner-728x90-INMRSB-I,1 
100002,MSVT08,MSN Video,msnbc.com-TODAYshow.com Special Sponsorships 8-Streaming Media-300x60-MSVT08-S,2  
100004,MSV10T,MSN Video,msnbc.com-TODAYshow.com Special Sponsorships 10-Streaming Media-300x60-MSV10T-S,3 

我在第 4 行得到以下异常:

java.sql.SQLException: bad TEXT table source file - line number: 1196 java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" in statement [SET TABLE someTableName SOURCE "/some.csv;ignore_first=true;all_quoted=true;shutdown=true"] at org.hsqldb.jdbc.Util.sqlException(Unknown Source) at org.hsqldb.jdbc.Util.sqlException(Unknown Source) at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source) at java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" at org.hsqldb.error.Error.error(Unknown Source) at org.hsqldb.TextTable.connect(Unknown Source) at org.hsqldb.TextTable.openCache(Unknown Source) at org.hsqldb.TextTable.setDataSource(Unknown Source) at org.hsqldb.StatementCommand.getResult(Unknown Source) at org.hsqldb.StatementCommand.execute(Unknown Source) at org.hsqldb.Session.executeCompiledStatement(Unknown Source) at org.hsqldb.Session.executeDirectStatement(Unknown Source) at org.hsqldb.Session.execute(Unknown Source) ... 6 more Caused by: java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at org.hsqldb.rowio.RowInputText.readInteger(Unknown Source) at org.hsqldb.rowio.RowInputBase.readData(Unknown Source) at org.hsqldb.rowio.RowInputText.readData(Unknown Source) at org.hsqldb.rowio.RowInputBase.readData(Unknown Source) at org.hsqldb.rowio.RowInputText.readData(Unknown Source) at org.hsqldb.RowAVLDiskData.getRowData(Unknown Source) at org.hsqldb.persist.RowStoreAVLDiskData.get(Unknown Source) ... 14 more

任何帮助都将是可观的。

编辑: Hsqldb 在读取定义为 VARCHAR 的列时抛出 NumberFormatException !!!

MODIFIED:发生异常是因为其中一个列值中包含逗号,例如"Mind, Body Connection-Banner-728x90-HEAMBA-S"。由于 hsqldb 将 csv 作为表读取,因此附加逗号被 hsql 解释为附加列。谁能指导我如何规避这个?

4

1 回答 1

1

正如 OP 发现的那样,遵循这些简单的步骤有助于在 CSV 文件中找到问题:

  1. 查看报错信息:bad TEXT table source file - line number: 1196 行号是CSV文件的行号。使用文本编辑器,转到该行。行从 1 开始编号。
  2. ava.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S"注意 NumberFormatException 表示需要一个数字,而不是找到报告的字符串。
  3. 幸运的是,表中只有一个数字 (INT) 列,因此很容易看到 CVS 中有一个额外的逗号,它终止了最后一个 VARCHAR 字符串并导致了问题。
  4. TEXT 表定义包括all_quoted=true,这意味着如果您在包含逗号的字符串周围使用双引号,则整个字符串将被视为一个字段,问题就会消失。
于 2013-03-03T20:31:03.057 回答