golang-mysql

command module
v0.0.0-...-ab2fbee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2022 License: Unlicense Imports: 4 Imported by: 0

README

golang-mysql

Basic Golang Mysql Connection and Query Template

Steps

Run the go mod init command, giving it your new code’s module path.

go mod init golang-mysql

Create Table and Insert Data

DB Table create script

-- testdb.TestUsers definition
DROP TABLE IF EXISTS testdb.TestUsers;

CREATE TABLE testdb.TestUsers (
  	id INT AUTO_INCREMENT NOT NULL,
	name VARCHAR(50) NOT NULL,
	email VARCHAR(100) NOT null,
	PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO testdb.TestUsers
  (name, email)
VALUES
  ('Blue Train', '[email protected]'),
  ('Giant Steps', '[email protected]')

Use the go get to add the github.com/go-sql-driver/mysql module as a dependency for your own module. Use a dot argument to mean “get dependencies for code in the current directory.”

go get .

Run

go run .

Terminal

Connected!
User found: [{1 Blue Train [email protected]}]
ID of added users: 3

https://golang.ir/doc/tutorial/database-access

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL