Golang DB.Prepare Examples

Golang DB.Prepare - 30 examples found. These are the top rated real world Golang examples of database/sql.DB.Prepare extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func doSelect(res http.ResponseWriter, db *sql.DB) error {
	var stmt *sql.Stmt
	var err error

	stmt, err = db.Prepare(selectStatement)
	if err != nil {
		fmt.Printf("db.Prepare error: %v\n", err)
		return err
	}

	var rows *sql.Rows

	rows, err = stmt.Query()
	if err != nil {
		fmt.Printf("stmt.Query error: %v\n", err)
		return err
	}

	defer stmt.Close()

	for rows.Next() {
		var firstname string
		var lastname string

		err = rows.Scan(&firstname, &lastname)
		if err != nil {
			fmt.Printf("rows.Scan error: %v\n", err)
			return err
		}

		fmt.Fprintln(res, "firstname: "+firstname+"    lastname: "+lastname+"\n")
	}

	return nil
}
Example #2
0
func push(db *sql.DB, iter, routine int, queryStr string) {
	defer timeTracker(time.Now(), "pump", routine)
	//fmt.Printf("StartTime: %v\n", time.Now())
	stmt, prepErr := db.Prepare(queryStr)
	if prepErr != nil {
		log.Fatal(prepErr)
	}
	defer stmt.Close()

	//var rowIds = []int64{}
	var rowNumCount int64
	for i := 0; i < iter; i++ {
		res, resErr := stmt.Exec("tester", time.Now(), i)
		if resErr != nil || res == nil {
			log.Fatal(resErr)
		}

		// count number of rows efected
		numRow, _ := res.RowsAffected()
		rowNumCount += numRow
		//lins, _ := res.LastInsertId()
		//rowIds = append(rowIds, lins)
		//lins, errLin := res.LastInsertId()
		//numRow, errRow := res.RowsAffected()
	}

	//fmt.Printf("Last ID: %d Number of rows: %d\n", rowIds[:len(rowIds)-1], rowNumCount)
	fmt.Printf("Number of rows: %d\n", rowNumCount)
	//fmt.Printf("StopTime: %v\n", time.Now())
	// cant do this cause we'll exit when first goroutine is done!
	//os.Exit(0)
}
Example #3
0
func insert_session(db *sql.DB, user_id int, sid string) {
	row := db.QueryRow("select count(id) from auth_session  where user_id=?", user_id)
	var count int
	err := row.Scan(&count)
	if err != nil {
		fmt.Println(err)
	}
	if count > 0 {
		stmt, err := db.Prepare("update auth_session set sid=? where user_id=?")
		if err != nil {
			fmt.Println(err)
			return
		}
		stmt.Exec(sid, user_id)
		defer stmt.Close()
		return
	}
	stmt, err := db.Prepare("insert into auth_session(user_id, sid) values (?,?)")
	if err != nil {
		fmt.Println(err)
	}
	defer stmt.Close()
	if _, err := stmt.Exec(user_id, sid); err != nil {
		fmt.Println("smt.Exec failed: ", err)
	}
}
Example #4
0
func FundHandler(rw http.ResponseWriter, r *http.Request, db *sql.DB, params martini.Params) {
	id := params["id"]
	sqlStr := "SELECT * FROM funds WHERE id=?"
	stmt, err := db.Prepare(sqlStr)
	if err != nil {
		log.Fatal(err)
	}
	defer stmt.Close()
	fund := new(Fund)
	err = stmt.QueryRow(id).Scan(&fund.Id, &fund.Name)
	if err != nil {
		if err == sql.ErrNoRows {
			rw.Header().Set("Status", "404")
			rw.Header().Set("Content-Type", "application/json; charset=utf-8")
			notFound := map[string]string{"error": "Not Found"}
			b, _ := json.Marshal(notFound)
			rw.Write(b)
		} else {
			log.Fatal(err)
		}
	} else {
		rw.Header().Set("Content-Type", "application/json; charset=utf-8")
		b, err := json.Marshal(fund)
		if err != nil {
			log.Fatal(err)
		}
		rw.Write(b)
	}
}
Example #5
0
// Save the User data to the DB
func SaveUser(db *sql.DB, u User) (id int64, err error) {

	// Prepare Insert Statement
	stmt, err := db.Prepare(`
		INSERT
			tbl_users
		SET
			steam64=?,
			steam32=?,
			name=?
	`)
	if err != nil {
		fmt.Println(err)
	}

	defer stmt.Close()

	res, err := stmt.Exec(
		u.Steam64,
		u.Steam32,
		u.Name,
	)

	if err != nil {
		fmt.Println(err)
	}

	id, err = res.LastInsertId()
	if err != nil {
		fmt.Println(err)
	}

	return id, err

}
Example #6
0
func mustPrepare(db *sql.DB, query string) *sql.Stmt {
	stmt, err := db.Prepare(query)
	if err != nil {
		log.Fatalf("Error when preparing statement %q: %s", query, err)
	}
	return stmt
}