2012-08-30 28 views
0

我遇到了一些麻煩。爲了從我的一個drupal SQL數據庫獲取一些數據,我需要相當詳細的查詢。問題是,在控制檯/ MysqlWorkBench查詢工作正常,而在我的JAVA程序使用JDBC它根本不工作。錯誤的JDBC SQL查詢,儘管它在控制檯/ MysqlWorkBench中工作

原來的查詢是:

# Tag use from the perspective of all tag entries belonging to nodes in table 'field_data_field_topic' 

    SELECT fdf.entity_type, node.nid, td.name, td.tid, ti.created, users.uid 
    FROM field_data_field_topic fdf LEFT OUTER JOIN taxonomy_term_data td 
    ON(fdf.field_topic_tid = td.tid) 
    LEFT OUTER JOIN taxonomy_index ti 
    ON(fdf.field_topic_tid = ti.tid AND fdf.entity_id = ti.nid) 
    LEFT OUTER JOIN node 
    ON(fdf.entity_id = node.nid) 
    LEFT OUTER JOIN field_data_body fd 
    ON(fd.entity_id = node.nid) 
    LEFT OUTER JOIN users 
    ON(users.uid = node.uid) 


    #Restricting to content from 01-02-2012 up to 11-06-2012 
    WHERE node.created > 1328054400 
    #Restricting to either Nodes or Comments 
    AND fdf.entity_type = "node" 
    #Restricting to node status=1 
    AND node.status=1 
    #Remove defective node with nid=1594 
    AND node.nid not like "1594" 

    UNION 

    # Tag use from the perspective of all tag entries belonging to comments in table 'field_data_field_topic' 

    SELECT fdf.entity_type, comment.cid, td.name, td.tid, ti.created, users.uid 
    FROM field_data_field_topic fdf LEFT OUTER JOIN taxonomy_term_data td 
    ON(fdf.field_topic_tid = td.tid) 
    LEFT OUTER JOIN taxonomy_index ti 
    ON(fdf.field_topic_tid = ti.tid AND fdf.entity_id = ti.nid) 
    LEFT OUTER JOIN comment 
    ON(fdf.entity_id = comment.cid) 
    LEFT OUTER JOIN field_data_comment_body fc 
    ON(comment.cid=fc.entity_id) 
    LEFT OUTER JOIN users 
    ON(users.uid = comment.uid) 

    #Restricting to content from 01-02-2012 up to 11-06-2012 
    WHERE comment.created > 1328054400 
    #Restricting to Comments 
    AND fdf.entity_type = "comment" 
    #Restricting to node status=1 
    AND comment.status=1 

在Java中我把:

String sql = "SELECT fdf.entity_type, node.nid, td.name, td.tid, ti.created, users.uid " 
     + "FROM field_data_field_topic fdf LEFT OUTER JOIN taxonomy_term_data td " + 
     "ON(fdf.field_topic_tid = td.tid) " + 
     "LEFT OUTER JOIN taxonomy_index ti " + 
     "ON(fdf.field_topic_tid = ti.tid AND fdf.entity_id = ti.nid) " + 
     "LEFT OUTER JOIN node " + 
     "ON(fdf.entity_id = node.nid) " + 
     "LEFT OUTER JOIN field_data_body fd " + 
     "ON(fd.entity_id = node.nid) " + 
     "LEFT OUTER JOIN users " + 
     "ON(users.uid = node.uid " + 
     "WHERE node.created > 1328054400 " + 
     "AND fdf.entity_type = 'node' " + 
     "AND node.status = 1 " + 
     "AND node.nid not like '1594' " + 
      "UNION " + 
       "SELECT fdf.entity_type, comment.cid, td.name, td.tid, ti.created, users.uid " + 
       "FROM field_data_field_topic fdf LEFT OUTER JOIN taxonomy_term_data td " + 
       "ON(fdf.field_topic_tid = td.tid) " + 
       "LEFT OUTER JOIN taxonomy_index ti " + 
       "ON(fdf.field_topic_tid = ti.tid AND fdf.entity_id = ti.nid) " + 
       "LEFT OUTER JOIN comment " + 
       "ON(fdf.entity_id = comment.cid) " + 
       "LEFT OUTER JOIN field_data_comment_body fc " + 
       "ON(comment.cid=fc.entity_id) " + 
       "LEFT OUTER JOIN users " + 
       "ON(users.uid = comment.uid) " + 
       "WHERE comment.created > 1328054400 " + 
       "AND fdf.entity_type = 'comment' " + 
       "AND comment.status=1" 
       ; 

而我得到的錯誤是這樣的:

MySQL Database connection established 
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right **syntax to use near 'WHERE node.created > 1328054400 AND fdf.entity_type = "node" AND node.status = 1' at line 1** 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.Util.getInstance(Util.java:386) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144) 
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2310) 
    at pp.orientTest.Orient4GraphUsersCreatedTagsTaggedNodes.main(Orient4GraphUsersCreatedTagsTaggedNodes.java:51) 
MySQL Database connection terminated 

回答

0

看起來是一個靠近托架從該線丟失:

"ON(users.uid = node.uid " + 
0
fdf.entity_type = "node", 

這應該是單引號

喜歡:

fdf.entity_type = 'node' 
+0

感謝您的提示的響應。但是如果仔細觀察,您會發現該節點實際上是用單引號括起來的。至少它在Java SQL語句中。 –

+0

但是當它被翻譯成時,有些如何包裹在雙引號中。如果你仔細閱讀異常消息有雙引號。 – kosa

+0

嗯..我知道這就是例外說..但這不是問題。但是,我的一位同事剛剛指出了一個缺失的')',在:「ON(users.uid = node.uid」+ –

相關問題