-1

I am trying to Query a single row from a PostgreSQL database table.

func getPrefix(serverID int64, db *sql.DB) string {
    var prefix string
    err := db.QueryRow("SELECT prefix FROM servers WHERE serverid = 1234").Scan(&prefix)
    if err != nil {
        fmt.Println(err.Error())
    }
    spew.Dump(prefix)
    fmt.Println("Prefix is " + prefix)
    return prefix
}

Apparently, the variable prefix is an empty String, but when I query it in the database, it's not empty

You are now connected to database "mewbot" as user "postgres".
mewbot=# select * from servers;
 serverid | prefix
----------+--------
     1234 | ;
(1 row)


mewbot=#

My question is, why is it returning an Empty String when it should be ; All checks taken; I've made sure I'm connected to the same database et al

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Dylee
  • 103
  • 2
  • 12
  • 3
    `fmt.Println(err.Error())` can be shortened to `fmt.Println(err)` – Jonathan Hall Nov 02 '19 at 14:56
  • What is the data type of the `prefix` column? – Jonathan Hall Nov 02 '19 at 14:56
  • @Flimzy Data type of `prefix` column is text – Dylee Nov 02 '19 at 15:24
  • 1
    @Dylee If you're certain you're connected to the same db, what happens if you prefix your table, in both Go and psql, with a schema name, for example `public.servers` or wherever the table is defined, do you still get the same result? And is the prefix column nullable, if so what is the output of `\pset null`? – mkopriva Nov 02 '19 at 15:39
  • @mkopriva I followed what you said and tried this `SELECT servers.prefix FROM public.servers WHERE serverid = 1234;` And it still didn't work, output of `\pset null` is `Null display is "".` – Dylee Nov 02 '19 at 17:23
  • And you're using `lib/pq` as the dirver? – mkopriva Nov 02 '19 at 17:30
  • @mkopriva Yes, these are my imports `import ( "database/sql" "fmt" "github.com/davecgh/go-spew/spew" _ "github.com/lib/pq" )` – Dylee Nov 02 '19 at 17:39
  • @Dylee I've tried your example code just now and I can't recreate the issue, everything works as it should. The problem must be somewhere in your setup / environment or in the way in which you execute your program... which makes it close to impossible to tell what's wrong from just the content of the question, since that all looks alright. https://imgur.com/a/bo0dI95 – mkopriva Nov 02 '19 at 17:41
  • Okay, let me give more info My main folder/workspace has only 1 folder and 1 file which are `utils/utils.go`[Here](https://pastiebin.com/5dbdc1318a4c1) and `main.go`[Here](https://pastiebin.com/5dbdc16c8942a) Finally, I am running my code with `go run main.go` from the main folder – Dylee Nov 02 '19 at 17:53
  • Also @mkopriva in my table, `serverid` is of type `bigint` and not `int`, just to point out because in your screenshot, it's of type `int` – Dylee Nov 02 '19 at 17:56
  • The id you pass in go does not match the id you got back from psql. `118456055842734083` is not `1234`. – mkopriva Nov 02 '19 at 17:57
  • sorry I just recently added that, it still doesn't work – Dylee Nov 02 '19 at 18:11
  • 1
    @Dylee note also that the first version of the go code on pastebin that you've linked had `118456055842734083` as an `int` argument, however that number don't fit into an `int`. Please make sure your code is in order, compiles, and executes. You could also try [turning on logs for your queries](https://stackoverflow.com/questions/722221/how-to-log-postgresql-queries) and compare the logs against what you think should be executed. – mkopriva Nov 02 '19 at 18:12
  • 1
    @Dylee also please never ever, when you receive back a non-nil error, continue as if nothing happend. Your code should exit if it encounters an error, but instead your code continues right where it left off after printing an error. – mkopriva Nov 02 '19 at 18:15

1 Answers1

0

Apparently it was not working because SSL mode was disabled on my server but I was trying to connect without specifying it was disabled.

Changing postgres://postgres:7890@localhost:5432/mewbot to postgres://postgres:7890@localhost:5432/mewbot?sslmode=disable solved my issue.

Dylee
  • 103
  • 2
  • 12