Logo

dev-resources.site

for different kinds of informations.

Reading data from Mysql server

Published at
7/28/2024
Categories
go
webdev
godot
godotengine
Author
sukmarizki04
Categories
4 categories in total
go
open
webdev
open
godot
open
godotengine
open
Author
12 person written this
sukmarizki04
open
Reading data from Mysql server

import the required packages, then prepare a struct with the same schema as in the example of the tb_student table in the database, later this struct will be used as a container for the query result data type.

package main
import "fmt"
import "database/sql"
import _ "github.com/go-sql-driver/mysql"


type student struct {
 id string,
 name string,
 age int,
 grade int,
}

Enter fullscreen mode Exit fullscreen mode

The database driver used needs to be imported using the _ sign, because even though it is needed by the database/sql package, we do not directly interact with the driver.

Next, create a function to connect to the database.

func connect() (*sql.DB, error) {
 db, err := sql.Open("mysql","root:@tcp(127.0.0.1:3306)/db_learn_go")
 if err != nil {
  return nil, err
 }
 return db, nil
}
Enter fullscreen mode Exit fullscreen mode

The connection string scheme for the mysql driver that we use is quite unique, root@tcp(127.0.0.1:3306)/db_learn_go below is a connection string scheme that can be used on the go MYSQL Driver driver, if you use another mysql, the connection scheme may be different depending on the driver used.

user:password@tcp(host:port)/dbname
user@tcp(host:port)/dbname
Enter fullscreen mode Exit fullscreen mode

Below is an explanation of the connection string used in the connect() function.

root@tcp(127.0.0.1:3306)/db_learn_go
// user => root
// password =>
// host => 127.0.0.1 atau localhost
// port => 3306
// dbname => db_learn_go
Enter fullscreen mode Exit fullscreen mode

After the function for connectivity with the database has been created, it's time to practice the process of reading data from the database server. Prepare the function
sqlQuery() with the following code.

func sqlQuery() {
 db, err := connect()
 if err != nil {
 fmt.Println(err.Error())
 return
 }
 defer db.Close()
 var age = 27
 rows, err := db.Query("select id, name, grade from tb_student where age = ?
 if err != nil {
 fmt.Println(err.Error())
 return
 }
 defer rows.Close()
 var result []student
 for rows.Next() {
 var each = student{}
 var err = rows.Scan(&each.id, &each.name, &each.grade)
 if err != nil {
 fmt.Println(err.Error())
 return
 }
 result = append(result, each)
 }
 if err = rows.Err(); err != nil {
 fmt.Println(err.Error())
 return
 }
 for _, each := range result {
 fmt.Println(each.name)
 }
}
Enter fullscreen mode Exit fullscreen mode

Every time a new connection is created, don't forget to always close the connection instance. You can use the defer keyword as in the code above, defer
db.Close() .
The db.Query() function is used to execute sql queries. The function's second parameter is variadic, so it can be left blank. In the code above, you can see that the value of one of the where clauses is a question mark (?).

The sign will then be replaced by the value in the parameter after it (the value of the age variable). This type of query writing technique is highly recommended, to prevent sql injection. The function produces an instance of type sql.*Rows , which also needs to be closed when it is no longer used ( defer rows.Close() ). Next, an array with the element type struct student is prepared with the name result . Later the query results will be stored in the variable. Then a loop is performed with the condition reference being rows.Next() . This loop is performed as many times as the total number of records, sequentially from the first record to the end, one by one. The Scan() method of sql.Rows functions to retrieve the value of the record that is being iterated, to be stored in a pointer variable. The variables used to store the record fields are written sequentially as variadic parameters, according to the fields selected in the query. Please see the comparison below for more details. // query
select id, name, grade ...
// scan
rows.Scan(&each.id, &each.name, &each.grade ...
The obtained record data is then appended to the result slice, via the statement
result = append(result, each) .
OK, now just call the sqlQuery() function in main , then run the program.

func main() {
sqlQuery()
}

I'm just an ordinary blog writer. If there are still too many shortcomings, please forgive me.

godotengine Article's
30 articles in total
Favicon
Game Dev Diary #1: Starting from zero
Favicon
Online Visual Novel in Godot: Case Study on Sentou Gakuen
Favicon
How to Press and Drag to Reposition a Window in Godot
Favicon
"Surf the Rails in Subway Surfers Online"
Favicon
AR Game ~ Geospatial API Sample ~
Favicon
Navigating the Skies: A Comprehensive Analysis of the Aircraft Engine MRO Market
Favicon
Going from Godot 3 to 4 (The Easy Way)
Favicon
Godot 2D & 3D Prototype Templates
Favicon
Perfecting Game Levels: The Essential Role of Playtesting
Favicon
Reading data from Mysql server
Favicon
Game Development Diary #11 : Second Day Back
Favicon
Game Development Diary #8 : Still Second Course
Favicon
Game Development Diary #7 : Second Course
Favicon
Game Development Diary #5 : Start My "BUMI" Project - Part 1
Favicon
Game Development Diary #4 : First Course Complete
Favicon
10 Useful Tips for Getting the Most Out of Godot
Favicon
Why Godot is a Game Changer in Game Development
Favicon
Game Development Diary #2 : GameDev.tv Course
Favicon
Estruturas de repetição em GDScript
Favicon
Estruturas condicionais em GDScript
Favicon
Boas Práticas nomeando variáveis e funções em GDScript
Favicon
Kingdom Hearts RE:Back Cover
Favicon
Godot Engine
Favicon
Hash SHA1 at go
Favicon
Let’s Learn Godot 3D by making an Endless Runner Game - Part 1: Project Overview & Setup 👟
Favicon
Let’s Learn Godot 3D by making an Endless Runner Game — Part 2: Environment Setup👟
Favicon
Let’s Learn Godot 3D by making an Endless Runner Game — Part 3: Player Setup & Animations👟
Favicon
Every beginning is hard or my first steps with GODOT - The 1st month
Favicon
BlackJack - Beta version
Favicon
Disney Gargoyles native porting Development Update 🚀

Featured ones: