sqlsqlitego

How to fix missing column in Go code when using SQLite


i have a table in sqlite database:
sqlite>.schema mmdb_csv

CREATE TABLE mmdb_csv (
cidr TEXT,
asn TEXT
);

I have uploaded a csv file with the following structure to this table:
cidr, asn,autonomous_system_organization,isp,organization

sqlite>.mode csv
sqlite>.import /home/igor/go/src/universal_blocklist_service2/mmdb-csv.csv mmdb_csv  

Also i have this code in my progarmm:

str := fmt.Sprint("SELECT cidr FROM ", conf.SQLiteASN2CIDR, " WHERE asn=?;")
        w.FindCidrsStmt, err = w.DBase.Prepare(str)
        if err != nil {
            w.log.Fatal().Msgs(err, " from NewRepairStruct()")
        }

data, err := w.FindCidrsStmt.Query(asn)
    if err != nil { 
        w.log.Error().Msgs(err, " from getCIDRs() 1, asn=", asn)
        return nil
    }

conf.SQLiteASN2CIDR=mmdb_csv

And I get following log:

"SQL logic error: no such column: cidr (1) from getCIDRs() 1, asn=59711"

But when I make an SQL query from the terminal, everything works as it should:

sqlite> select cidr from mmdb_csv where asn=59711 limit 10;
5.149.248.0/23
5.149.254.0/23
77.83.196.0/23
77.83.198.0/24
79.141.164.0/23
79.141.172.0/24
79.141.174.0/23
88.218.206.0/24
91.193.18.0/24
91.250.244.0/24

Why do i get this log message?:

"SQL logic error: no such column: cidr (1) from getCIDRs() 1, asn=59711"

When i add quotes around cidr in statement preparation i get no error logs, but there are no rows in data:

str := fmt.Sprint("SELECT \"cidr\" FROM ", conf.SQLiteASN2CIDR, " WHERE \"asn\"=?;")
        w.FindCidrsStmt, err = w.DBase.Prepare(str)
        if err != nil {
            w.log.Fatal().Msgs(err, " from NewRepairStruct()")
        }

data, err := w.FindCidrsStmt.Query(asn)
    if err != nil { 
        w.log.Error().Msgs(err, " from getCIDRs() 1, asn=", asn)
        return nil
    }

data here has no rows, and

for data.Next() {

is not being executed


Solution

  • I think your function is cleared, so I suspect that you didn't input the database name when initialized the connection or maybe it's calling to another DB because the DB Name is inputted with another value.