Hello and welcome to the Go programming language course in the previous session.
We could define a connect function for communicating to database.
After we can connect to the database, we can perform crud operations by writing different functions.
First, you start by writing your query related to the creativity, a statement that using it, we can
create the table in the database automatically, so create the function to create a table.
For example, you name it, create table function.
Find,
create table.
Define an object variable
and say 3.9 or face.
This function is supposed to take in a straight instance from our site and convert it to a table in
the database.
But since we don't know which a start is going to be passed to this function in this package, it uses
ellipses as its function input parameter and receives the least of abstracts instance at the input parameter.
This feature enables us to send one or more awestruck ministers from outside of the package to this
function to create equivalent tables in the database.
Now, Colin Connect function for initializing minuscule data source name.
This function returns a DB object from gore and package.
This gives us access to the methods contained in the package to perform current operation.
So first, define a variable like DB call equals and in which connect function after creating a DB object
from core package.
The Invoke Artemis great function, it will automatically create the table based on given models.
This function accepts an instance of each stroke, as input parameter rate will only create tables,
missing columns and missing indexes and won't change existing columns.
Type or delete unused columns to protect your data.
So we write D.B. Dot also migrate.
And said object that ask parameter.
Now we could define the function to create the table automatically in database in this time, we create
a new package named Main and create find inside it named Main that cool.
And in this fine, we create the main function to execute queries.
So go to the project folder and create a new folder named Main.
And create a new file like Maine that go inside it.
First, right, package me and define main function, phone me.
Before we can access data in the MySchool database, we need to create a connection to the database.
Previously, we designed the method called Connect in the DB Tools package, which contains a method
called open from the go on in packages, which by taking two values of dialect or object and configuration,
creates a connection to the MySQL database and for parameter it uses from open function of MySchool
package data accept a data source name as parameter.
We also use the same data source name information inside a JSON file in the configuration package.
Now it's time to use the information our JSON file to connect to the database for using the JSON find
we calling open function from Always Package that opens the named file for reading.
If successful methods on the return find can be used for reading this function, accept file pass as
input parameter and returns to value first and object from file and the other and the object from error.
So first we define two variables like file and there are five comma error column equals and calling
open function from OS package OS start open.
And said Jason finally passed that input parameters, a string type currency creation, is the second
big dot.
Jason.
Check every status if there all.
The battle fatal error, but.
You know, we use Defer for closing the created foreign object, defer file that close.
Now, create this right on top of the main metal that contains valuables equivalent to the keys in the
JASON fine that are used to map those keys to their data types and go blank.
Thai Con Figuration District.
Define variable data source name and the type is a string and using JSON tech.
Jason Collins and said name for it.
Data source named.
Now go to the main function and create an object from configuration extract.
First, define a variable like Tom Cullen equals new configuration.
Now, using the new decoder function of the JSON package, we send the Jason fine to eat as parameter
after opening it and through the Decode Metal End reads the next JSON encoded value from its input and
restores it in the value pointer to object created from the configuration instructs.
So you're right, Jason Dart's new decoder and sit forward as arguements and calling d'école and set
comes as arguments for it.
Okay, this allowed us to assert the values inside the JSON file in the variables in the configuration
as struct.
Now it's time to call the DB initialize or method from DB tools package to connect to the database.
So we write DB tools and calling DB initialize our function.
DB initialize the function and set data source name from conf object that's arguement from the data
source name.
Each of the crop methods used the method called Connect to communicate with the database, so necessary
conditions for communication with my school database are provided.
Now it is time to call the available methods to perform current operations in DB tools patches.
In this case, before doing anything, they want to create a student tailored in-person database automatically
so you can create table function from DB Tools package.
But before creating a new table, we need to go into the MySQL database to see if there is a personal
schema inside the database or if does not exist.
It should be created for this purpose.
Inter to the MySQL database and in the left panel.
In the Navigator section, we will see the list of available schemas.
OK, now you see the person database is available, the go back to the Discord program and call the
Create Table function.
Twists are creating the student table automatically calling create table function for long DevTools
package DB tools, dot, create table and sit any stands of student ASRock and arguement Anderson Model
Dot Student
OK, reformat the code and save the projects and go to terminal and execute the program.
Go from Dot Package Slash Main and main dot go.
Now it's time go to the MySchool database.
Go to the Navigator and right click Press Refresh or open the person database and open the tables.
OK, now we see that is done correctly and we were able to create a student table automatically in the
MySQL database through our application in order not to prolong the time of decision.
We will finish it, then we will continue the content in the next session.
Goodbye.