2014-08-28 154 views
1

我有一個程序,它在執行時會從文件中獲取大量單詞,並將它們插入到數據庫中,插入後如果該單詞插入兩次,則會計算「 IDF「再次使用觸發器。問題是,如果我這樣做,直接到MySQL它有沒有問題,但如果我這樣做Java的返回此錯誤:java.sql.SQLException:子查詢返回多於一行

Exception in thread "main" java.sql.SQLException: Subquery returns more than 1 row 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2828) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2777) 
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:949) 
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:795) 
    at model.Consultas.altaBajaCambio(Consultas.java:29) 
    at model.DatosBD.agregarPalabra(DatosBD.java:23) 
    at search.Search.main(Search.java:36) 
Java Result: 1 

我認爲這個問題必須與st.execute()因爲它只返回一個int,但我在網上搜索解決方案,但找不到一個。

查詢:

String query2 = "INSERT IGNORE INTO Search.IndiceInv (DocID, Term, TF) VALUES ("+doc+",'"+term+"',1) ON DUPLICATE KEY UPDATE `TF` = `TF` + 1;"; 
c.altaBajaCambio(query2); 

執行:

try (Connection con = c.getConnection()) { 
      if (con == null) { 
       System.out.println("No hay conexion"); 
      } else { 
       Statement st = con.createStatement(); 
       st.execute(query); 
      } 

數據庫:

-- ----------------------------------------------------- 
-- Table `Search`.`Doc` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `Search`.`Doc` (
    `DocID` INT NOT NULL, 
    PRIMARY KEY (`DocID`)) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `Search`.`Term` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `Search`.`Term` (
    `Term` VARCHAR(45) NOT NULL, 
    `IDF` INT NOT NULL, 
    PRIMARY KEY (`Term`)) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `Search`.`IndiceInv` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `Search`.`IndiceInv` (
    `DocID` INT NOT NULL, 
    `Term` VARCHAR(45) NOT NULL, 
    `TF` INT NOT NULL, 
    PRIMARY KEY (`DocID`, `Term`), 
ENGINE = InnoDB; 

觸發:

delimiter // 
create trigger IDF 
after update on IndiceInv 
for each row 
begin 
    update Term 
    set IDF = (SELECT LOG((SELECT count(*) FROM Doc)/(SELECT count(DocID) FROM IndiceInv WHERE Term = new.Term)) FROM Doc, IndiceInv) 
    where Term = new.Term; 
end;// 
delimiter ; 
+1

嘗試手動運行:'(SELECT LOG((SELECT COUNT(*)FROM文件)/(SELECT COUNT (DocID)FROM IndiceInv WHERE Term = new.Term))FROM Doc,IndiceInv)'(將相關值賦給'new.Term'等) – alfasin 2014-08-28 05:55:46

+0

我看到了,我得到多個結果,並且不能分配給一個單細胞。 – arturojain 2014-08-28 06:06:58

+1

你明白了! :) – alfasin 2014-08-28 06:11:58

回答

2

嘗試手動運行:

SELECT LOG((SELECT count(*) FROM Doc)/(SELECT count(DocID) FROM IndiceInv WHERE Term = new.Term)) FROM Doc, IndiceInv 

(分配給new.Term等相關的值)