Quando si utilizza SQLcl con Oracle Database, è possibile utilizzare SPOOL comando per esportare i risultati della query in un file con un .html estensione e puoi impostare SQLFORMAT a html per produrre i risultati effettivi della query in formato HTML.
Esempio
Ecco un esempio da dimostrare:
SET SQLFORMAT html;
SPOOL '/Users/barney/data/regions.html';
SELECT * FROM regions;
SPOOL off;
SET SQLFORMAT ansiconsole; Ecco cosa ha fatto, riga per riga:
- La prima riga imposta
SQLFORMATahtml. Ciò garantisce che il nostro risultato.htmlil file contiene infatti codice HTML. - La seconda riga utilizza il
SPOOLcomando per specificare dove verrà scritto il file di output. Assicurati di modificare/Users/barney/data/regions.htmlin una posizione sul tuo sistema e un nome file appropriato. - Sulla terza riga, ho eseguito la query SQL, i risultati per i quali sto esportando. In questo caso, ho esportato tutte le
regionstabella. - Poi ho girato
SPOOLspento. - Infine, ho impostato
SQLFORMATtornando alla mia impostazione originale, che eraansiconsole. Questo è facoltativo:puoi lasciarlo sujsonse preferisci, o cambialo con qualcos'altro.
Ecco come appare il file risultante:
regions.html
Ed ecco il codice sorgente dietro quel file:
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>Result Data</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
margin: 0;
padding: 0;
}
body {
font: 14px/1.4 Palatino, Serif;
}
/*
Generic Styling, for Desktops/Laptops
*/
table {
width: 100%;
border-collapse: collapse;
}
/* Zebra striping */
tr:nth-of-type(odd) {
background: #eee;
}
th {
background: #333;
color: white;
font-weight: bold;
}
td, th {
padding: 6px;
border: 1px solid #9B9B9B;
text-align: left;
}
@media
only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px) {
table, thead, tbody, th, td, tr { display: block; }
thead tr { position: absolute;top: -9999px;left: -9999px;}
tr { border: 1px solid #9B9B9B; }
td { border: none;border-bottom: 1px solid #9B9B9B; position: relative;padding-left: 50%; }
td:before { position: absolute;top: 6px;left: 6px;width: 45%; padding-right: 10px; white-space: nowrap;}
/*
Label the data
*/
td:nth-of-type(1):before { content: "REGION_ID"; }
td:nth-of-type(2):before { content: "REGION_NAME"; }
}
/* Smartphones (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
body {
padding: 0;
margin: 0;
width: 320px; }
}
/* iPads (portrait and landscape) ----------- */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
body {
width: 495px;
}
}
</style>
<!--<![endif]-->
<script type="text/javascript">
function search(){
var s = document.getElementById('search').value;
rows = document.getElementById('data').getElementsByTagName('TR');
for(var i=0;i<rows.length;i++){
if ( rows[i].textContent.indexOf(s)>0 || s.length==0 ) {
rows[i].style.display ='';
} else {
rows[i].style.display ='none';
}
}
}
var timer;
function delayedSearch() {
clearTimeout(timer);
console.log('delay-ing')
timer = setTimeout(function () {
console.log('delay-running')
search();
}, 500);
}</script>
</head>
<body>
<div><input type="text" size="30" maxlength="1000" value="" id="search" onkeyup="delayedSearch();" /><input type="button" value="Go" onclick="lsearch();"/> </div>
<table><thead><tr> <th>REGION_ID</th>
<th>REGION_NAME</th>
</tr></thead>
<tbody id="data">
<tr>
<td align="right">1</td>
<td>Europe</td>
</tr>
<tr>
<td align="right">2</td>
<td>Americas</td>
</tr>
<tr>
<td align="right">3</td>
<td>Asia</td>
</tr>
<tr>
<td align="right">4</td>
<td>Middle East and Africa</td>
</tr>
</tbody></table><!-- SQL:
SELECT * FROM regions--></body></html>
4 rows selected. Quindi genera l'intero documento HTML, non solo la tabella.
Noterai che alcuni CSS sono stati aggiunti per scopi di stile e JavaScript è stato aggiunto per creare una funzione di ricerca.
Rimuovi feedback
Puoi rimuovere il X rows selected con SET FEEDBACK off :
SET SQLFORMAT html;
SET FEEDBACK off;
SPOOL '/Users/barney/data/regions_feedback_off.html';
SELECT * FROM regions;
SPOOL off;
SET FEEDBACK on;
SET SQLFORMAT ansiconsole;
In questo caso mi sono rivolto a FEEDBACK riattivare dopo aver esportato il file.