Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 612243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:52:19+00:00 2026-05-13T17:52:19+00:00

I try to run the same query in several dbs in mysql: def m=’xxx’

  • 0

I try to run the same query in several dbs in mysql:

def m='xxx'
def dbs =  ['DB05DEC05','DB06DEC06','DB07DEC07','DB08DEC08','DB09DEC09','DB10DEC10']
def sql =Sql.newInstance("jdbc:mysql://localhost:3306", "root","", "org.gjt.mm.mysql.Driver")
dbs.each{
 db-> sql.eachRow("select * from ${db}.mail where mid=$m", { println "\t$db ${it.mid}"} );
}

This gives an error:

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 ''DBJAN05DEC05'.mail where mid='xxx'

Groovy apparently does some custom stuff with quotes and asks you not to use quotes in the sql (notice mid=$m, if you use mid=’$m’ it warns you against using the quotes).
The problem is that in the first $ I dont know want quotes at all, the quotes are the problem…

groovy 1.7 on vista.
thanks

editing: I have found a similar question, but it does not have an accepted answer either… Groovy GString issues

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:52:19+00:00Added an answer on May 13, 2026 at 5:52 pm

    The problem is that the SQL query method sees the GString, with its embedded variable references, and turns each reference into a ? in a prepared statement.

    So:

    sql.query("select * from table where col = ${value}")
    

    … is equivalent to:

    sql.query("select * from table where col = ?", [ value ])
    

    But also:

    sql.query("select * from ${db}.table where col = ${value}")
    

    is equivalent to:

    sql.query("select * from ?.table where col = ?", [ db, value ])
    

    … which fails at the DB layer because the select statement is not valid.

    The obvious workaround is to use the explicit prepared statement version of query().

    dbs.each{ db-> 
        sql.eachRow("select * from ${db}.mail where mid=?", m, { 
           println "\t$db ${it.mid}"
        });
    }
    

    However, the Sql class gives you an expand() method, that appears to be designed for this purpose.

    dbs.each{ db -> 
       sql.eachRow(
          "select * from ${Sql.expand(db)}.mail where mid=${m}", 
          { println "\t$db ${it.mid}"} );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I try to run this query in SQL Server 2005: SELECT 1 WHERE
When I try to run the following code in the MySQL console it gives
When I try and run this query on my database: $mysqli->query(UPDATE catalogsearch_fulltext SET data_index
When i run this SQL query via psql client it runs for several seconds
I try to run jsf application in myeclipse using jboss web server and following
I try to run one program in myeclipse where after creating one web project
I try to run my project in windows. Project use android ndk. I install
I try to run the following code: public void Init(Url rootUrl) { var web
I try to run the Time Profiler on my iPad 2(5.0.1) on Xcode 4.2
I try to run in JavaFX application background thread periodically, which modifies some GUI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.