Querying the DB

GetGoing: Introduction to Golang Introduction to API development with Go
5 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're going to be learning about how to query the database to read an entry according to a particular value, we need an ID of username. So let's get started. In the last video, we left it over here. In this video, we're gonna search things by ID. So let's dive straight right into it. Let's go to let's go to the greens.go.

And I want us to copy this function and then paste it over here. And then we can name this function read by name, which takes a name, string. And then what this is going to do is it is going to do select star from to do the name is equal to question mark. And in this question mark, we can say name. So, notice that this function is exactly similar similar to this function. Word Read My name is only going to show all of the entries according to a particular name.

So let's go to our control over here. And let's do read by name instead of reload, read by name. And notice this query that I've added over here. So when you're looking at a URL, let's look at our terminal when whenever you're looking at a URL, or other groups, postman. So this is how we sent POST requests, right? So when we want to send a get request, we can simply send a get request on postman like this.

And let me spin up the server real quick. So when we are sending a get request, we are getting this response. Right. So what if we want to send some queries along with a get request? So we can simply say Q, or question mark, name is equal to any name we want it to be, let's say. So notice that postman automatically DC realizes this query and parses it And says that okay, use electrodes and key is the name and the value is longer.

And then you can pass different queries by saying, and let's say you have query h is equal to 10. So postman automatically recognizes this. And if you want to add additional fields like job, then it's going to automatically added in the URL parameters. So I'm not, I just need me more here. Now to access these queries in the back end, all we do is we use this method. so we can simply say name is equal to r dot URL, which gives us the access to the URL dot query.

Query is a function which returns URL dot values, objects, query. Mind when writing query nor get now don't get means I want to get one query, and that query is name. So I have my name right here and I'm going to read by name, I'm simply going to pass into me Now, for this to work, let me create a new entry so that you can see if this works or not. So, instead of 100, I'm going to say DC VAT. Let's use the source name DC VAT to do by house. And I'm going to add one more name and let's say called chef VAT.

And so now if you take a look at our database over here, and as the name got longer, let's restart our server and then send a get request to query the database. So, an email request you see I got only the queries with the name on good. If I say DSC VAT then I got the ones only from DC VAT. If I say kosher, you get the idea. Then I could the query is only from kosher VAT. So in this way, we can easily query our database resources or we can easily anything we want.

And the thing is that it's better to use question mark here, rather than inserting a value directly or by adding some query after this, the reason is because of SQL injections. So SQL injections is a vulnerability where in the front end, the people can write the whole SQL queries which can be executed in your back end. So it's better if you don't give them the access to those. And then you use something like this. So in this video, we learned how to query the database, we didn't change anything, we just created a new function, which was like read all but instead of reading all values, it gave us certain values sorted by name. And then we didn't change anything.

We just we grabbed the query from the URL parameters, and then we inserted into the model form. So if you take a look at our requirement specifications real quick, you've done read entries by ID. In the next video, I'm going to be showing you how to delete things from a database. So see you guys in the next video and stay tuned.

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.