Skip to content

Instantly share code, notes, and snippets.

@vedashree29296
Created February 9, 2021 06:19
Show Gist options
  • Save vedashree29296/f960d195c29fb969f076ce7094748a5a to your computer and use it in GitHub Desktop.
Save vedashree29296/f960d195c29fb969f076ce7094748a5a to your computer and use it in GitHub Desktop.
Create a new category and return an id
//CreateCategory : create a new Category in DB
func (c *Category) CreateCategory() (int, error) {
//Create and execute the query
row := client.DbClient.QueryRow("INSERT INTO category (category_name) values ($1) RETURNING id;", c.CategoryName)
// save the data in an object
switch err := row.Scan(&c.ID); err {
// success, no errors
case nil:
return c.ID, nil
// In case of no output rows
case sql.ErrNoRows:
return c.ID, errors.New("Insert Failed")
// other errors occured
default:
return c.ID, err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment