golang-examples/example-favicon/main.go

170 lines
3.6 KiB
Go

package main
import (
"database/sql"
"log"
"fmt"
_ "github.com/mattn/go-sqlite3"
)
func parseFaviconsSQLite(filePath string) {
// Open the SQLite database
db, err := sql.Open("sqlite3", "favicons.sqlite")
if err != nil {
log.Fatal(err)
}
defer db.Close()
rowId, err := db.Query("SELECT id, icon_url, icon_data FROM moz_icons")
if err == nil {
for rowId.Next() {
var id int
var url string
var data any
err = rowId.Scan(&id, &url, &data)
log.Printf("Data:", id, url, data)
}
}
return
// Query the favicons table
rows, err := db.Query("SELECT * FROM moz_icons")
if err != nil {
log.Println("open worked, err =", err)
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var col1, col2, col3, col4, col5, col6, col7, col8 any // Define variables according to the column data types
err = rows.Scan(&col1, &col2, &col3, &col4, &col5, &col6, &col7, &col8)
if err != nil {
log.Fatal(err)
}
// Process the data
log.Printf("Data: %v, %v, %v, %v, %v, %v, %v, %v\n", col1, col2, col3, col4, col5, col6, col7, col8)
}
/*
log.Println("about to for rows.Next()")
// Iterate through the rows
for rows.Next() {
var id int
var iconURL string
var iconData []byte
var var1 any
var var2 any
var var3 []byte
log.Println("row.Scan()")
err = rows.Scan(&id, &iconURL, &iconData)
if err != nil {
log.Fatal(err)
}
// Process the favicon data (example: just print it)
fmt.Printf("ID: %d, URL: %s, Data: %x\n", id, iconURL, iconData)
}
*/
// Check for errors from iterating over rows
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}
func main() {
// Replace with the path to your favicons.sqlite file
filePath := "./"
parseFaviconsSQLite(filePath)
fmt.Println()
fmt.Println("SHOW tables:")
listTablesSQLite(filePath)
fmt.Println()
fmt.Println("SHOW schema:")
dumpTableSchemas(filePath)
}
func listTablesSQLite(filePath string) {
// Open the SQLite database
db, err := sql.Open("sqlite3", "favicons.sqlite")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Query the sqlite_master table for all table names
rows, err := db.Query("SELECT name FROM sqlite_master WHERE type='table'")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Iterate through the rows
for rows.Next() {
var tableName string
err = rows.Scan(&tableName)
if err != nil {
log.Fatal(err)
}
// Print each table name
fmt.Println(tableName)
}
// Check for errors from iterating over rows
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}
func dumpTableSchemas(filePath string) {
// Open the SQLite database
db, err := sql.Open("sqlite3", "favicons.sqlite")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Query for the SQL create statements of each table
rows, err := db.Query("SELECT name, sql FROM sqlite_master WHERE type='table'")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Iterate through the rows
for rows.Next() {
var tableName, createStatement string
err = rows.Scan(&tableName, &createStatement)
if err != nil {
log.Fatal(err)
}
// Print the table name and its create statement
fmt.Printf("Table: %s\n", tableName)
fmt.Printf("Create Statement:\n%s\n\n", createStatement)
}
// Check for errors from iterating over rows
err = rows.Err()
if err != nil {
log.Fatal(err)
}
// moz_pages_w_icons ( id INTEGER PRIMARY KEY, page_url TEXT
rowId, err := db.Query("SELECT id, page_url FROM moz_pages_w_icons")
for rowId.Next() {
var id int
var url string
rowId.Scan(&id, &url)
// if err != nil {
log.Println("row =", id, url)
}
}