SHARE
TWEET

Untitled

a guest Mar 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tx, err := db.Begin()
  2. if err != nil {
  3.     return err
  4. }
  5. defer tx.Rollback()
  6.  
  7. var results []MyStruct
  8. qSelect := tx.Prepare(select_sql)
  9. qInsert := tx.Prepare(insert_sql)
  10.  
  11. for _, thing := range *things {
  12.     var id int
  13.     var err error
  14.     var result sql.Result
  15.     err = qSelect.QueryRow(thing.FieldOne).Scan(&id)
  16.     if err != nil {
  17.         if err == sql.ErrNoRows {
  18.             result, err = qInsert.Exec(thing.FieldOne, thing.FieldTwo)
  19.             if err != nil { /* this is the line where I get the error */
  20.                 return err
  21.             }
  22.         } else {
  23.             return err
  24.         }
  25.     }
  26.     if num, err := result.RowsAffected(); err != nil {
  27.         return err
  28.     } else if num > 0 {
  29.         results = append(results, thing)
  30.     }
  31. }
  32.  
  33. tx.Commit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top