使用grails中的groovy脚本迁移数据库
我目前有一个数据库需要在 grails 中迁移。我已经使用过
def sql =groovy.sql.Sql.newInstance(url,username,password,driver)
,之后我调用了
sql.eachRow("""select *\
from kontakt k join kommunikation c on k.kontakt_id = c.kontakt_id \
join kommunikationsmittel cm on c.kommittel_id = cm.kommittel_id \
join kommunikationstyp ct on c.komtyp_id = ct.komtyp_id \
join adresse a on a.Kontakt_ID = k.Kontakt_ID
""") {row->
}
问题是,我可以通过 row.Strasse、row.PLZ 等访问列名,但是如果有两列,在两个不同的表中具有相同的名称。如何访问不同的列? 例如,在“Kommunikationsmittel”表中有一列“Bezeichnung”。在“Kommunikationstyp”中还有一栏“Bezeichnung”。如何通过行访问?
I have currently a database need to be migrated in grails. I have used
def sql =groovy.sql.Sql.newInstance(url,username,password,driver)
and after that I call
sql.eachRow("""select *\
from kontakt k join kommunikation c on k.kontakt_id = c.kontakt_id \
join kommunikationsmittel cm on c.kommittel_id = cm.kommittel_id \
join kommunikationstyp ct on c.komtyp_id = ct.komtyp_id \
join adresse a on a.Kontakt_ID = k.Kontakt_ID
""") {row->
}
The thing is that I can access the columnname via row.Strasse, row.PLZ and so on, but if there are two columns, which have the same name inside two different tables. How can I access different columns?
For example in 'Kommunikationsmittel' table there is a column 'Bezeichnung'. In 'Kommunikationstyp' there is also a column 'Bezeichnung'. How can I access via row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果您有重复的字段名称,就会导致问题。
您的选择是更改选择以使其特定,并且您可以重命名冲突的字段,即:
或者,您可以使用行的元数据来获取所有字段,如这个例子:
顺便说一句,当您使用
"""
时,您的 SQL 中不需要\
字符,因此行将继续,直到尾随" “”
Yeah, if you have duplicate field names, it's going to cause problems.
Your options are to either change the select so it is specific, and you can rename the conflicting fields, ie:
Or, you can use the
metaData
for the row to get at all of the fields, as in this example:By the way, you don't need
\
chars in your SQL as you are using"""
, so lines will continue till the trailing"""