scraper/main/db.go

56 lines
974 B
Go
Raw Permalink Normal View History

2023-09-28 12:27:31 +00:00
package main
import (
"database/sql"
"fmt"
"git.dvdrw.dev/nsmarter/scraper/scraper"
_ "github.com/mattn/go-sqlite3"
)
func readDb() ([]scraper.Station, error) {
db, err := sql.Open("sqlite3", stationDbPath)
if err != nil {
return nil, err
}
defer db.Close()
queryString := "SELECT stationId FROM stations"
if innerCityOnly {
queryString += " WHERE cityId=72 "
}
if(limitQuerySize > 0) {
queryString += fmt.Sprintf(" LIMIT %d ", limitQuerySize)
}
queryString += ";"
rows, err := db.Query(queryString)
if err != nil {
return nil, err
}
defer rows.Close()
var stations []scraper.Station = make([]scraper.Station, 0, 1000)
for i := 0; rows.Next(); i++ {
station := scraper.Station{}
err = rows.Scan(&station.Id)
if err != nil {
return nil, err
}
stations = append(stations, station)
Log.Printf("Imported station with ID=%v\n", station.Id)
}
err = rows.Err()
if err != nil {
return nil, err
}
return stations, nil
}