亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

org.postgresql.util.PSQLException:錯誤:“7”處或附近的語法錯誤

org.postgresql.util.PSQLException:錯誤:“7”處或附近的語法錯誤

浮云間 2023-08-04 17:39:08
我正在嘗試在我的應用程序中創建私人聊天,但是當我嘗試創建表時出現此錯誤:org.postgresql.util.PSQLException:錯誤:“7”位置或附近的語法錯誤:14代碼: public static String checkIfChatExists(String code, String friend) throws SQLException {        String i = LoginManager.checkCode(code);        if(i.equals("code-not-exists")) {            return "invalid-code";        }        ResultSet rs = conn.prepareStatement("SELECT * FROM pwchats").executeQuery();        while (rs.next()) {            if(rs.getString("user1").equals(i) && rs.getString("user2").equals(friend) || rs.getString("user1").equals(friend) && rs.getString("user2").equals(i)) {                return "exists";            }        }        PreparedStatement pre = conn.prepareStatement("INSERT INTO pwchats(user1, user2) VALUES(?, ?)");        pre.setString(1, i);        pre.setString(2, friend);        pre.execute();        PreparedStatement getChatId  = conn.prepareStatement("SELECT * FROM pwchats WHERE user1 = ? AND user2 = ?;");        getChatId.setString(1, i);        getChatId.setString(2, friend);        ResultSet rss = getChatId.executeQuery();        while (rss.next()) {            PreparedStatement chat = conn.prepareStatement("CREATE TABLE " + rss.getInt("id") + "chat (username text NOT NULL, created_at timestamp with time zone NOT NULL DEFAULT now(), avatar text NOT NULL, message text NOT NULL, id serial NOT NULL);");            chat.execute(); //ERROR            return "done";        }        return "wat";    }
查看完整描述

1 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

您看到的錯誤可能來自那里:

preparedStatement chat = conn.prepareStatement("CREATE TABLE " + rss.getInt("id") + "chat (...);");

通常在 SQL 中,表名不能以數字開頭。如果您要用雙引號將表名引起來,Postgres 仍然允許這樣做,但是每次查詢時都需要雙引號表名,這可能很乏味。

一種解決方案是將數字放在表名稱的末尾,例如:

preparedStatement chat = conn.prepareStatement("CREATE TABLE chat" + rss.getInt("id") + " (...);");

DB Fiddle 演示

create table 7chat(x int);

ERROR:  syntax error at or near "7"

LINE 1: create table 7chat(x int);

                     ^

create table "7chat"(x int);

-- works


create table chat7(x int);

--works


查看完整回答
反對 回復 2023-08-04
  • 1 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號