[UDF] _SQLite_QueryToHtmlTable
Posté : sam. 10 avr. 2010 21:20
Fonction permettant de générer le code HTML d'un tableau contenant une table SQLite.
Pour comprendre, essayez l'exemple
L'udf:
L'exemple:
Pour comprendre, essayez l'exemple
L'udf:
Code : Tout sélectionner
#include-once
;===============================================================================
;
; Function Name: _SQLite_QueryToHtmlTable
; Description: Return HTML code of a table containing the SQLite table queried.
; Parameter(s): $dataBase - An Open Database, Use -1 To use Last Opened Database
; $queryText - SQL Statement to be executed
; $tableName (Optional) - The table name to be writen in the HTML page (Default = "")
; $css (Optional) - 0 -> Generate css style sheet for a simple table
; 1 -> Better table designe (Default)
; Any other value -> No table borders at all!
; $comment (Optional) - True -> Adds comment at begining and end of the HTML code
; (like: <!-- Start HTML Code generated by _SQLite_QueryToHtmlTable() -->)
; False -> Don't adds the comment
; Requirement(s): sqlite.au3 ##(This UDF must be included AFTER sqlite.au3)##
; Return Value(s): Success -> The HTML code
; Failure -> -1 : Problem in _SQLite_query()
; -2 : Problem in _SQLite_fetchNames()
; -3 : Problem in _SQLite_fetchData()
; Author(s): Matwachich (matwachich@gmail.com)
;
;===============================================================================
;
Func _SQLite_QueryToHtmlTable($dataBase, $queryText, $tableName = "", $css = 1, $comment = True)
Local $names, $rows, $i, $query, $ret
If _SQLite_Query($dataBase, $queryText, $query) <> $SQLITE_OK Then
Return -1
EndIf
If $comment <> 0 Then $ret &= "<!-- Start HTML Code generated by _SQLite_QueryToHtmlTable() -->" & @CRLF
If $css = 0 Then
$ret &= "<style>" & @CRLF
$ret &= "<!--table"
$ret &= "{"
$ret &= "border-collapse: collapse;"
$ret &= "}"
$ret &= "th"
$ret &= "{"
$ret &= "border: 1px solid black;"
;~ $ret &= "background-color: gray;"
;~ $ret &= "color: white;"
$ret &= "}"
$ret &= "td"
$ret &= "{"
$ret &= "border: 1px solid black;"
$ret &= "}-->"
$ret &= @CRLF & "</style>" & @CRLF
EndIf
If $css = 1 Then
$ret &= "<style>" & @CRLF
$ret &= "<!--table"
$ret &= "{"
$ret &= "border-collapse: collapse;"
$ret &= "}"
$ret &= "th"
$ret &= "{"
$ret &= "border: 1px solid black;"
$ret &= "background-color: gray;"
$ret &= "color: white;"
$ret &= "}"
$ret &= "td"
$ret &= "{"
$ret &= "border: 1px solid black;"
$ret &= "}-->"
$ret &= @CRLF & "</style>" & @CRLF
EndIf
If $tableName <> "" Then $ret &= "<p> <strong> Query from table: " & $tableName & " </strong> </p>" & @CRLF
$ret &= "<table>" & @CRLF
If _SQLite_FetchNames($query, $names) <> $SQLITE_OK Then
Return -2
EndIf
$ret &= "<tr>"
For $i in $names
$ret &= "<th>" & $i & "</th>"
Next
$ret &= "</tr>"
While _SQLite_FetchData($query, $rows) = $SQLITE_OK
If @error Then Return -3
$ret &= "<tr>"
For $i In $rows
$ret &= "<td>" & $i & "</td>"
Next
$ret &= "</tr>"
WEnd
$ret &= @CRLF & "</table>" & @CRLF
$ret &= "<p>Generated on " & @MDAY &"/"& @MON &"/"& @YEAR & " - " & @HOUR &":"& @MIN &":"& @SEC & "</p>"
If $comment <> 0 Then $ret &= @CRLF & "<!-- End HTML Code generated by _SQLite_QueryToHtmlTable() -->"
Return $ret
EndFunc
Code : Tout sélectionner
#include <SQLite.au3>
#include "SqliteQuerytoHtmlTable.au3"
;~ #AutoIt3Wrapper_Run_Debug_Mode=y
Dim $query, $row, $data
_SQLite_Startup()
_SQLite_Open(":memory:")
_SQLite_Exec(-1, "CREATE TABLE IF NOT EXISTS aTable(colonne1,colonne2)")
_SQLite_Exec(-1, "INSERT INTO aTable VALUES ('02','test 2');" & _
"INSERT INTO aTable VALUES ('01','test 1');" & _
"INSERT INTO aTable VALUES ('03','test 3');")
; ### Récupération des données dans la console de Scite ###
_SQLite_Query(-1, "SELECT ROWID,* FROM aTable ORDER BY colonne1 ASC;", $query)
_SQLite_FetchNames($query, $data)
ConsoleWrite("| " & $data[0] & @TAB & "| " & $data[1] & @TAB & "| " & $data[2] & @CRLF)
ConsoleWrite("-----------------------------------" & @CRLF)
While _SQLite_FetchData($query, $row) = $SQLITE_OK
ConsoleWrite("| " & $row[0] & @TAB & "| " & $row[1] & @TAB & @TAB & "| " & $row[2] & @CRLF)
WEnd
ConsoleWrite("<-------------------------------------->" & @CRLF)
; ### Et maintenant, ecriture des données dans le fichier testhtml ###
$htmlCode = _SQLite_QueryToHtmlTable(-1, "SELECT ROWID,* FROM aTable ORDER BY ROWID ASC", "aTable")
$file = FileOpen("test.html", 2)
FileWrite($file, $htmlCode)
FileClose($file)
_SQLite_Close()
_SQLite_Shutdown()