Insertion introduction

GetGoing: Introduction to Golang Introduction to API development with Go
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi everyone, in this video, we are going to be creating an SQL schema and then creating and adding some entries to it. So even simply do SQL, my SQL, and then minus you the name of your username minus p without any space, the name of the password. So we go to my SQL, I'm gonna use my SQL. And then let's create a table. So I'm gonna do a CREATE TABLE, the name of the table is to do and I can define what I wanted my table. So I want the name of the person was adding something.

Let's keep it at 50 and then I want to do it. So tomorrow is gonna be about care of 1000. And then so I just created a table called to do and then I don't need to do anything else. I'm exiting out of my my school instance. So Now let's create a function so that we can add some to dues. So I'm gonna create a new file called create.go where I'm going to be defining a function.

So func create to do, which is going to return an error. Now notice that in the last video, we had a global connection. So this function is of the same package it has access to the global connection. So I can simply say rows or insert query is equal to con dot query. And then I can end the query I can write SQL I can say that okay insert into to do values for syntax and personal return or dollar one and then I could say word dollar $1 two, or rather, I should do. The first one was name, and the second one was two.

Let's keep it simple and it's blurred only one in order to. And then I'm gonna say that Okay, my name is on my to do lists this video. Now, in the case of any error, first I'm gonna do insert. So like insert document opens a query, we need to do a close on this also, right? And if error is not equal to nil, then I simply want to return the error. Or I want to return nil, which means essentially that there is no error.

So now we've created a function. Let's use this function in our controller. So I'm going to create a new file called, according to the specification, it should be called creating an entry. So just slash so let's do let's also call it a create package controller. So in this is gonna have a function or function called create. And it's gonna do an HTTP, funk.

Let's return some funk HTTP dot HTTP response writer and our reference to HTTP requests. So, what we are going to do is if our dot method is equal to is equal to http dot method post so if the method Reaching this route is of method post, then we're gonna take some data out of the request, and then save it. So for the purpose of this video, let's just test if our create function function is working or not. So let's do a model dot create. Let's take a look at this do and do more. And it returns an error.

So I can simply do error sequel to is equal to model dot create to do it or not equal to null. Do I'm just gonna write the data and then I'm going to return. So the purpose purpose here is not Building the actual endpoint, but testing if this this function is working or not. So, in fact, let's see injure took method get only in the next video, we can complete this. So it seems you to method get and then quickly do a go on window connected to the database and solving. So let's do a quick curl command to the slash route.

And it's gonna say method not found. The reason is it, we didn't register it here. So we have to register it as a create function. So it's saying essentially that whenever we reach the slash route, then invoke this function or call this function. So the name of our table in SQL was my SQL. So we'll quickly go to connect and then we will say that, Okay, listen on the MySQL table.

And the syntax is this. So TCP or Transmission Control Protocol comes over here. Then we go to our create order of the modeler. And the syntax is not dollar one and dollar two, as it is in many other languages, it's a question mark and go. So, so quick, a quick recap. So what we did was, in the last video, we connected to the database In this video, we created a schema, then we created a function called create connect create.go in a model.

So what it does is it performs a query in the SQL, and then it simply returns no error. If it's fine, it returns an error if there's some problem. And if your controller.go we created a function called create a handler, func, which when it gets something on the get endpoint, it basically it creates something and calls the model dot create to do function. And if we go to a route or go we registered this on slash, so whenever we get a get on slash, we create something. Let's check this out in action. So we will create good and may not go and we Do a code.

So it says 200. Okay. Now let's see what happens. Let's see if anything got saved in our database or not, I'm going to do use my SQL. Then I'm going to do a select star from do. So as you can see over here, our NPM and our to do was saved.

So in this video, we made our SQL instance and we and we figured out how to make the function run and save something in the database. In the next video, we're going to be completing the second endpoint, which is this one, creating an entry where we post something on slash route and we create data and we create and we send back a status created response code. So stay tuned guys and see you guys in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.