3

I am trying to do a simple thing, check if there is a table, if not then create that table in database.

Here this is the logic I used.

    test := "June_2019"
    sql_query := `select * from ` + test + `;`

    read_err := db.QueryRow(sql_query, 5)
    error_returned := read_err.Scan(read_err)
    defer db.Close()

    if error_returned == nil {
        fmt.Println("table is there")

    } else {
        fmt.Println("table not there")
    }

In my database I have June_2019 table. But still this code returns me not nil value. I used db.QueryRow(sql_query, 5) 5 as I have five colomns in my table.

What am I missing here? I am still learning golang.

Thanks in advance.

6
  • 2
    Offtopic; Consider if making a table from application code is logical as your MySQL user has to be more powerfull have CREATE and maybe DROP rights, which can be unsafe.. Also seeing metadata (Year) used as table separator, why are you doing this? ideally you should have one table where you have a year column Commented Jul 6, 2019 at 14:42
  • 1
    .. As MySQL can handle milllion or even in the billions numbers of records just fine in one table when indexes are involved and when in doubt deploy Partitioning which does more or less the same thing you are doing but better in every way.. Commented Jul 6, 2019 at 14:44
  • @RaymondNijland true. but here I did this to learn golang and satisfy a simple need. Commented Jul 6, 2019 at 15:02
  • 4
    SQL is a language. go is a language. When trying to solve a problem, it's best to use the language best suited for the task at hand. Checking for table existence should be left to the SQL side of things e.g. CREATE TABLE IF NOT EXISTS Commented Jul 6, 2019 at 15:37
  • See this answer for an SQL query for table existence. Commented Jul 6, 2019 at 15:40

1 Answer 1

3

I have solved the problem using golang and MySQL.

_, table_check := db.Query("select * from " + table + ";")

    if table_check == nil {
        fmt.Println("table is there")
    } else {
        fmt.Println("table not there")
    }

I have used db.Query() which returns values and and error, here I checked only error.

I think most people thought I want to do it in MySQL way, I just wanted to learn how to use golang to do MySQL operations.

Sign up to request clarification or add additional context in comments.

3 Comments

You forgot rows.Close() (the first return variable) in case of table_check == nil and that will probably leak memory
@k3a does that mean that you shouldn't assign the result to _ so that you can close the object?
Yes, every Query() call with nil error returns *sql.Rows. Those rows need a full for loop iteration with Next() until Next() returns false (which would call Close() internally) or an explicit call to Close() to free the memory used by the resultset. Close() is idempotent and can also be called multiple times safely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.