列名称包含下划线时的语法错误

编程入门 行业动态 更新时间:2024-10-24 15:19:05
本文介绍了列名称包含下划线时的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以编译但无法执行以下代码,并出现错误(使用Postgres):

I can compile but can't execute the following code with the error (using Postgres):

Fatal database error ERROR: syntax error at or near "as" Position: 13

import java.sql.*; public class JDBCExample { private static final String JDBC_DRIVER = "org.postgresql.Driver"; private static final String URL = "jdbc:postgresql://hostname/database"; private static final String USERNAME = "username"; private static final String PASSWORD = "password"; public static void main(String[] args) throws Exception { Connection dbConn = null; Statement query = null; ResultSet results = null; Class.forName(JDBC_DRIVER); try { dbConn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { System.out.println("Unable to connect to database\n"+e.getMessage()); System.exit(1); } try { query = dbConn.createStatement(); results = query.executeQuery("select 20_5 as name from flowshop_optimums"); while (results.next()) { System.out.println(results.getString("name")); } dbConn.close(); } catch (SQLException e) { System.out.println("Fatal database error\n"+e.getMessage()); try { dbConn.close(); } catch (SQLException x) {} System.exit(1); } } // main } // Example

推荐答案

不是下划线,而是列名以数字开头的事实。您需要对此进行转义。

It's not the underscore, it's the fact that the column name starts with a number. You'd need to escape this.

对于MySQL,请使用反引号。

For MySQL, use backticks.

select `20_5` as name from flowshop_optimums

对于SQL Server,请使用方括号。

For SQL Server, use square brackets.

select [20_5] as name from flowshop_optimums

对于PostgreSQL,请使用双引号。

For PostgreSQL, use double quotes.

select "20_5" as name from flowshop_optimums

更多推荐

列名称包含下划线时的语法错误

本文发布于:2023-11-16 09:50:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下划线   名称   语法错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!