Groovy SQL per prendere tutte le colonne
-
29-10-2019 - |
Domanda
Sto usando Groovy SQL per recuperare i risultati.Questa è l'uscita dalla mia scatola Linux.
%isql -U abc -P abc -S support
1> sp_configure 'number of open partitions'
2> go
Parameter Name Default Memory Used Config Value Run Value Unit Type
------------------------------ ----------- ----------- ------------ ------------ -------------------- ----------
number of open partitions 500 5201 5000 5000 number dynamic
(1 row affected)
(return status = 0)
1>
.
Sto usando il codice Groovy
def sql = Sql.newInstance("jdbc:abc:sybase://harley:6011;DatabaseName=support;",dbuname,dbpassword,Driver)
sql.eachRow("sp_configure 'number of open partitions'"){ row ->
println row.run_value
}
.
Ma mi dà
Exception in thread "main" java.sql.SQLSyntaxErrorException: [abc][Sybase JDBC Driver]Invalid column name: run_value
.
Quindi dice che non può ottenere le colonne specificate, c'è un modo in cui può recuperare il risultato e il display?
Aggiornamento
Ho usato il seguente codice
.
sql.eachRow("sp_configure 'number of open partitions'"){ row ->
println row
}
E mi dà
.
[Parameter Name:number of open partitions , Default: 500, Memory Used: 5201, Config Value: 5000, Run Value: 5000, Unit:number , Type:dynamic ]
Come posso ottenere Run Value
?(Dal momento che ha uno spazio in esso)
row.Run Value
non funzionerà per certo
Soluzione
funziona?Interrogando i meta-dati per il nome della colonna e ottenere il valore per indice:
sql.eachRow("sp_configure 'number of open partitions'"){ row ->
(1..row.getMetaData().columnCount).each {
println "${row.metaData().getColumnName( it )} = ${row[ it ]}"
}
}
.
o, per ottenere la colonna direttamente
row.'Run Value'
. Altri suggerimenti
Un'altra alternativa è getat (int).Nel tuo caso, questo restituirà il 'Valore run':
row.getAt(-3)
Aditioannelly a @ Tim_Yates La risposta vorrei notare che row
ha un metodo toRowResult()
.Permette di ottenere una riga semplicemente come una mappa.Non è necessario chiamare getMetadata()
se abbiamo bisogno solo di nomi di colonna o larghezza della tabella.
sql.eachRow("sp_configure 'number of open partitions'"){ row ->
row.toRowResult().each(){ colName, value ->
println "${colName} = ${value}"
}
}
.
Questo codice sta facendo assolutamente lo stesso ma sembra molto più chiaro