I am trying to use a prepared statement for querying a sqlite file. This worked fine before, but I change the code to make it more unit testable. To do this, I started using prepared statements. However, I am getting an error of "No rows in result set", yet when I type this query out manually on the file, it returns something.
I'm not really understanding whats going wrong here. I feel like its something to do with the argument I am passing in but I've tried everything on stackoverflow and nothing has worked
import (
"database/sql"
"database/sql/driver"
_ "github.com/mattn/go-sqlite3"
)
...
type Driver struct {
driver *driver.Driver
}
...
func getTransactionId(pkg string, db *sql.DB) (int, error) {
var id int
fmt.Println("Package is:", pkg)
fmt.Println("Db is this:", db)
sqlStatement := `SELECT tid FROM trans_cmdline WHERE cmdline LIKE '%install ?%' ORDER BY tid DESC LIMIT 1`
err := db.QueryRow(sqlStatement, pkg).Scan(&id)
if err != nil {
fmt.Println("Err:", err)
}
switch err {
case sql.ErrNoRows:
fmt.Println("ID is this:", id)
fmt.Println("No rows were returned:", err)
return 0, err
case nil:
return id, nil
default:
return 0, err
}
}
These queries are being ran against the default transaction yum db file on CentOS & RHEL machines. The output to the above code is like so:
Package is: telnet
Db is this: &{0 {/var/lib/yum/history/history-2022-10-14.sqlite 0xc0000ae140} 0 {0 0} [] map[] 0 0 0xc00008e1e0 false map[] map[] 0 0 0 0 <nil> 0 0 0 0 0x52cc80}
Err: sql: no rows in result set
ID is this: 0
No rows were returned: sql: no rows in result set
Error querying for transaction ID: sql: no rows in result set
The argument "WHERE LIKE" is supposed to match any row that contains "INSTALL {PACKAGE_HERE}", yet it doesn't seem to be finding any rows (when they certainly exist).
I've tried most things on Stackoverflow, read over the official docs for GO SQL. Nothing has worked for me so far.
The replace doesn't work inside the like
's %...%
try building the sql like
statement
SELECT * FROM analysis WHERE notes like CONCAT( '%',?,'%');