Go language error handling for MySQL data query

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Go language error handling for MySQL data query package mainimport ("Database/sql" "FMT" "Runtime")//Go Run Test.gofunc Main () {// MySQL extension https://github.com/go-sql-driver/mysql db, err: = SQL. Open ("MySQL", "Root: @tcp (127.0.0.1:3306)/test?charset=utf8") if err! = Nil {fmt. PRINTLN (ERR)}//DB connection generally does not shut down actively, unless it is confirmed that the connection is no longer needed, go will recycle defer db.        Close ()//user model, table structure, requires a structure to receive the query result set type User struct {Id int32 Name string Age int8} Save user Information list var user user//1, query a series of values//query result set needs to call the next () method to go through rows, err: = db. Query (' SELECT id,name,age from user ') if err! = Nil {fmt. PRINTLN (ERR)} defer rows. Close () for rows. Next () {//For traversal, it is only necessary to determine if there is an error each time//parameter binding requires number and position one by one corresponds to If err: = rows. Scan (&user. Id, &user. Name, &user. Age); Err! = Nil {fmt. PRINTLN (ERR) continue} FMT. Println (user. Id, user. Name, user. Age)}//After processing is complete, you need to determine the traversalIf there is an error in the procedure to produce if err: = rows. ERR (); Err! = Nil {fmt. PRINTLN (ERR)}//2, query a record//query a record, cannot use similar if err: = db. Queryrow (). Scan (& ...); Err! = Nil {} is processed//because when querying a single data, var errnorows = errors may be returned. New ("Sql:no rows in result set") This type of error message//And this is a normal error, err = db.    Queryrow (' SELECT id,name,age WHERE id =? ', 2). Scan (&user. Id, &user. Name, &user. Age,) switch {case err = = SQL. Errnorows:case Err! = Nil://Use this method to print out the runtime error message, which is not determined at compile time if _, file, line, OK: = runtime. Caller (0); OK {fmt. PRINTLN (err, file, line)}} FMT. Println (user. Id, user. Name, user. Age)//3, about null//All fields queried are not allowed to have null, the best way to avoid this is to create a table field, do not set similar to the default NULL property//There are some unavoidable situations, such as the following query/ /The query, if it does not exist, returns a value of NULL instead of 0, for that simple query, directly using the HAVING clause can//specific query, need in the process of encoding self-processing var age int32 err = db.        Queryrow (' SELECT SUM (age) ' age from user WHERE id =? Having aGE <> NULL ', 10). Scan (&age) Switch {case err = = SQL. Errnorows:case Err! = nil:fmt. PRINTLN (Err)} FMT. Println (age)}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.