sqlitegogo-sqlite3

How can I handle a "no such table" error?


With an SQLite database in Go I got:

no such table: table_name

I know where it is coming from but how can I use this error message in Go to create that table, like:

_, err := getState(dbconn) // This the function that retrieves the table that does not exists
if err != nil {
    if err == err.New("no such table: table_name") {
        // do the work
    }
}

Solution

  • Errors returned by SQLite cannot be compared to a string using err.New("no such table: table_name") because it is an sqlite3.Error object, not a string.

    Check the error message using the Error() method:

    _, err = getState(dbconn)
    if err != nil {
        if err.Error() == "no such table: table_name" {
            fmt.Println("Creating the table")
            // do the work
        }
    }