Golang implementation of MySQL database operations

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

First, create a table

Marksql: = "CREATE TABLE mark (ID int (primary) key Auto_increment,markname char ()," + "Mlongitude float,mlatitude Floa T,mdescribe char (255), Mtype Int,creattime char (), mvalue float); " SMT, Err: = db. Db.prepare (Marksql) Checkerr (Err) Smt. Exec ()

Second, create a foreign key

Define friend FK1 foreign key Idx_userid: = "ALTER TABLE user add index Idx_userid (UID);" SMT, err = db. Db.prepare (Idx_userid) Checkerr (Err) Smt. Exec () Idx_userid = "ALTER TABLE friend add index Idx_friuid (UID);" SMT, err = db. Db.prepare (Idx_userid) Checkerr (Err) Smt. Exec ()
Define friend FK2 foreign key frifk2sql: = "ALTER TABLE friend add constraint Fk2_fri_user foreign key (UID) references user (UID);" SMT, err = db. Db.prepare (Frifk2sql) Checkerr (Err) Smt. Exec ()

Third, insert data

INSERT into the database table func (db *dboperation) insert2table (str string) bool {stmt, err: = db. Db.prepare (str) checkerr (ERR) res, err: = stmt. Exec () defer stmt. Close () Checkerr (err) I, err: = Res. rowsaffected () If i > 0 && err = nil {return true} else {return false}}

var sql = "INSERT into user (Uid,ucode,unickname,password,ufavicon,utel,utype,udescribe,ucreattime) VALUES (" + StrConv. Itoa (user. ID) + ", '" + user. Code + "', '" + user. Nickname + "', '" + user. Password + "', '" + user. IMG + "', '" + user. Mobile + "'," + StrConv. Formatfloat (user. Category, ' e ', ten, +) + ", '" + user. Description + "', '" + user. Createtime + "');" If!db. g_db. Insert2table (SQL) {FMT. PRINTLN ("User table insert not successful! ")} else {fmt. Println ("Entering weite2db OK!!!")}

Iv. Updating the database

var sql = "Update user set ufavicon= '" + user. IMG + "', udescribe= '" + user. Description + "', utype=" + StrConv. Formatfloat (user. Category, ' e ', ten, + "where uid=" + StrConv. Itoa (user. ID) + ";" If!db. g_db. Insert2table (SQL) {FMT. PRINTLN ("User table update not successful! ") return errors. New ("UPDATE error!!!")}

V. Find a database

Func (db *dboperation) Find (str string) *sql. Rows {fmt. PRINTLN (str) rows, err: = db. Db.query (str) FMT. Println ("--------------------err", err) if err! = Nil {Panic ("Error in:selecting in table")}//rows. Close () Return rows}

Func (Pusersset *userset) loaddata (pDb *db. dboperation) {var user userfmt.println ("begining:load userset ...") Selectstr: = "Select Uid,ucode,unickname, Password,ufavicon,utel,utype,udescribe,ucreattime from user ' rows: = Pdb.find (SELECTSTR) for rows. Next () {err: = rows. Scan (&user. Id, &user. Code, &user. Nickname, &user. Password, &user. IMG, &user. Mobile, &user. Category, &user. Description, &user. Createtime) If err! = Nil {Panic ("Error in:scanning the table user")}fmt. Println ("-------------------Load data to Cache--------------------") fmt. Println (user) Pusersset.writeuserfromdb2cache (user)}}


Related Article

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.