【问题标题】:SQL not adding file contents to tableSQL没有将文件内容添加到表中
【发布时间】:2019-03-19 06:25:37
【问题描述】:

目前我正在尝试通过 Apache Derby 将新数据传递给 SQL 表。我要从具有给定文本的文件中读取,并将读取的每个标记添加到其各自的列中。

但是,在尝试执行此操作时,我收到许多错误,表明令牌不是列...但这不是我想要做的。

carmpg.txt 内容:

Toyota  Prius   52  23475
Kia Optima  31  22600
Hyundai     Sonata  31  22050
Nissan  Altima  31  23260
Chevrolet   Malibu  30  21680
Honda   Accord  30  23570
Mazda   6   29  21945
Subaru  Legacy  29  22195
Toyota  Camry   27  23495
Chrysler    200 27  22115
Ford    Fusion  27  22120
Volkswagen  Passat  27  22995
Volkswagen  CC  25  34475
Chevrolet   Impala  25  27895
Buick   LaCrosse    25  29565
Nissan  Maxima  25  33270
Buick   Regal   24  27065
Lincoln MKZ 26  3560

所以,Toyota 应该进入 Manufacturer 列,Prius 应该进入 Model 列,依此类推。

代码如下:

 try(Connection conn = SimpleDataSource.getConnection();Statement stat = 
conn.createStatement()) 
  {
     try
     {
        stat.execute("DROP TABLE Car");
     }
     catch(SQLException e)
     {
         // get exception if table doesn't exist yet
     }

     stat.execute("CREATE TABLE Car (Manufacturer VARCHAR(20),"
           + "Model VARCHAR(20), " + "Efficiency DECIMAL(6,2), " + "Price DECIMAL(8,2)) ");


     String inputFileName = "carmpg.txt";
     File inputFile = new File(inputFileName);
     Scanner in = new Scanner(inputFile);

     // COMPLETE THIS WHILE LOOP to insert all cars from the input text file
     while (in.hasNextLine()){

         stat.execute("INSERT INTO Car VALUES " + in.next());


     }

     ResultSet rs = stat.executeQuery("SELECT * FROM Car");
     rs.next();
     System.out.println(rs.getString("Model"));

以及相关的stacktrace:

Exception in thread "main" java.sql.SQLSyntaxErrorException: Column 'TOYOTA' 
is either not in any table in the FROM list or appears within a join 
specification and is outside the scope of the join specification or appears 
in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or 
ALTER TABLE  statement then 'TOYOTA' is not a column in the target table.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown 
Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at 
org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown 
Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown 
Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
at CarDB.main(CarDB.java:49)
Caused by: ERROR 42X04: Column 'TOYOTA' is either not in any table in the 
FROM list or appears within a join specification and is outside the scope of 
the join specification or appears in a HAVING clause and is not in the GROUP 
BY list. If this is a CREATE or ALTER TABLE  statement then 'TOYOTA' is not a 
column in the target table.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.compile.ColumnReference.bindExpression(Unknown 
Source)
at org.apache.derby.impl.sql.compile.ColumnReference.bindExpression(Unknown 
Source)
at org.apache.derby.impl.sql.compile.ResultColumn.bindExpression(Unknown 
Source)
at org.apache.derby.impl.sql.compile.ResultColumnList.bindExpressions(Unknown 
Source)
at org.apache.derby.impl.sql.compile.RowResultSetNode.bindExpressions(Unknown 
Source)
at org.apache.derby.impl.sql.compile.DMLStatementNode.bindExpressions(Unknown 
Source)
at org.apache.derby.impl.sql.compile.InsertNode.bindStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
... 3 more

感谢您的帮助

【问题讨论】:

  • 您需要在插入语句中的 in.next()) 周围加上引号。此外,请指定 SQL 方言。
  • @nicomp 指定 SQL 方言是什么意思,比如哪种类型?我使用的类型是 Apache Derby。希望这就是你要找的。此外,如果我在 in.next() 周围加上引号,那么 Java 会将其视为字符串并且不会从文件中提取下一个标记
  • 您需要从 Java 中“隐藏”引号并将它们作为 SQL 语句的一部分传递给 SQL 引擎。查看错误信息:它认为 Toyota 是表中的一列。
  • @nicomp 这实际上是怎么做的?
  • 您需要做好功课并弄清楚。互联网上到处都是例子。

标签: java sql derby


【解决方案1】:

INSERT INTO 语句的语法是:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

另外,您的错误是您尝试从文本文件中插入整行而不将其分成代表 4 个不同列的 4 个部分。
你得到的错误被触发是因为你试图执行这个无效的语句:

INSERT INTO Car VALUES Toyota Prius   52  23475

应该是

INSERT INTO Car (Manufacturer, Model, Efficiency, Price) VALUES ('Toyota', 'Prius', 52, 23475)

【讨论】:

  • 对。但是,它在放入 Java 时仍然无法正常运行。我收到同样的错误,说“Toyota”不是列。
  • @Matthew 查看我编辑的答案,我忘记提及 VARCHAR 列的撇号。如果您对代码进行了更改,请发布。
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 2021-09-06
相关资源
最近更新 更多