22. Toad Notes Go language--using SQLite driver

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

22. Toad Notes Go language--using SQLite driver

Download driver

Https://github.com/mattn/go-sqlite3

Unzip, put into the file mattn\go-sqlite3

In addition, you need to install GCC on windows, where the toad is installed: Tdm64-gcc-5.1.0-2.exe

Create a table

Sqlite3 foo.db

CREATE TABLE ' UserInfo ' (

' UID ' integerprimary KEY autoincrement,

' Username ' VARCHAR (+) NULL,

' Departname ' VARCHAR (+) NULL,

' Created ' DATE NULL

);

CREATE TABLE ' Userdeatail ' (

' UID ' INT (Ten) NULL,

' Intro ' TEXT NULL,

' Profile ' TEXT NULL,

PRIMARY KEY (' uid ')

);

Code

Package Main

Import (

"Database/sql"

"FMT"

_ "Github.com/mattn/go-sqlite3"

)

Func Main () {

DB, err:= SQL. Open ("Sqlite3", "foo.db")

Checkerr (ERR)

         // Inserting Data

Stmt,err: = db. Prepare ("INSERT into UserInfo (username, departname, created) VALUES (?,?,?)")

Checkerr (ERR)

Res,err: = stmt. Exec ("Astaxie", " Research and Development department ", "2012-12-09")

Checkerr (ERR)

ID, err:= Res. Lastinsertid ()

Checkerr (ERR)

Fmt. PRINTLN (ID)

         // Update Data

Stmt,err = db. Prepare ("Update userinfo set username=?") where uid=? ")

Checkerr (ERR)

Res,err = stmt. Exec ("astaxieupdate", id)

Checkerr (ERR)

Affect,err: = Res. Rowsaffected ()

Checkerr (ERR)

Fmt. PRINTLN (Affect)

         // Querying Data

Rows,err: = db. Query ("SELECT * from UserInfo")

Checkerr (ERR)

Forrows. Next () {

Varuid int

Varusername string

Vardepartment string

varcreated string

err= rows. Scan (&uid, &username, &department, &created)

Checkerr (ERR)

Fmt. PRINTLN (UID)

Fmt. PRINTLN (username)

Fmt. PRINTLN (department)

Fmt. PRINTLN (created)

}

         // Delete Data

Stmt,err = db. Prepare ("Delete from userinfo where uid=?")

Checkerr (ERR)

Res,err = stmt. Exec (ID)

Checkerr (ERR)

Affect,err = Res. Rowsaffected ()

Checkerr (ERR)

Fmt. PRINTLN (Affect)

Db. Close ()

}

Func Checkerr (err error) {

If err!= Nil {

Panic (ERR)

}

}

PS: The previously created foo.db need to be placed in the directory where the Go language code resides.

Execution results

1

1

1

Astaxieupdate

Research and Development Department

2012-12-09t00:00:00z

1

Consistent with MySQL.

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.