C
C#8mo ago
Eve.

how do you search a database for an integer with the @search

nothing is working
No description
88 Replies
Jimmacle
Jimmacle8mo ago
what does "nothing" mean? at first glance it looks like you're trying to use something like %123.45% as a number and that is not a number the % symbols are used as wildcards for text search, they aren't valid for numbers
Eve.
Eve.OP8mo ago
i copied a youtube video its meant to get the user input e.g 400 and get the data from the database which matches 400 or less
jiniux
jiniux8mo ago
you don't need %% for that
Eve.
Eve.OP8mo ago
No description
Eve.
Eve.OP8mo ago
what shall i put where the error is
jiniux
jiniux8mo ago
you wrote searchPriceCPU, but the parameter is searchTermCPU
Eve.
Eve.OP8mo ago
omg i think it worked i have been trying to figure this out for weeks
Jimmacle
Jimmacle8mo ago
...weeks?
jiniux
jiniux8mo ago
💀
Eve.
Eve.OP8mo ago
thank you so much i owe you my life i am new to this.
Jimmacle
Jimmacle8mo ago
maybe you should go through some SQL tutorials so you understand what you're writing
jiniux
jiniux8mo ago
perhaps chatgpt may have helped you xD
Jimmacle
Jimmacle8mo ago
no chatgpt is awful for beginners
Eve.
Eve.OP8mo ago
believe me it did not
Jimmacle
Jimmacle8mo ago
in order to use LLMs effectively you have to be able to tell good code from bad code and beginners don't have that experience it's just as likely to tell them some made up garbage that won't work as give them an actual answer
jiniux
jiniux8mo ago
ur right, i was just saying that chatgpt may have pointed out that the variable names were incorrect
Eve.
Eve.OP8mo ago
yes it did not help at all i am trying to do a big project and i have been given an extension because it is so bad small steps lol
jiniux
jiniux8mo ago
i don't want to let you down, but the way you're coding this does not follows today's standards
Eve.
Eve.OP8mo ago
my teacher doesn't help at all, i have to teach myself and idk where to go or what to do
Jimmacle
Jimmacle8mo ago
if it's a school project then raw dogging ADO.NET is expected tbh
jiniux
jiniux8mo ago
ah, then okay
Jimmacle
Jimmacle8mo ago
school courses are behind the times
Eve.
Eve.OP8mo ago
i did connect it to my database and created a database on my own though brownie points for me
Jimmacle
Jimmacle8mo ago
i was shown this recently https://sqlbolt.com/
SQLBolt - Learn SQL - Introduction to SQL
SQLBolt provides a set of interactive lessons and exercises to help you learn SQL
Eve.
Eve.OP8mo ago
No description
Jimmacle
Jimmacle8mo ago
it's not specific to mysql but it might help you understand the queries you're writing
jiniux
jiniux8mo ago
ye it's okay, but if you want to do this as a professional after school, you should expand your knowledge to more modern approaches
Eve.
Eve.OP8mo ago
i have actually never been so happy i want to do this after school but i am shit at computer science
jiniux
jiniux8mo ago
nobody is born knowing everything
Jimmacle
Jimmacle8mo ago
you can only get better with practice :bless:
Eve.
Eve.OP8mo ago
very true do you guys want to help with other aspects of my project fine if not may as well ask
jiniux
jiniux8mo ago
try to solve things on your own first. if you get stuck, just open a new post with the underlying problem
Eve.
Eve.OP8mo ago
believe me i have i don't know where to start
jiniux
jiniux8mo ago
what do you have to do?
Eve.
Eve.OP8mo ago
it has to take the budget, purpose and age into consideration and depending on all of those things will depend on the output for example budget: 600 purpose: gaming age: 21 it should output where everything is under a certain amount, the CPU is AMD for gaming age doesn't really matter
jiniux
jiniux8mo ago
ye that's what i was about to say why is there age in the first place xD
Eve.
Eve.OP8mo ago
it matters in my analysis i spoke about age a lot in the analysis this is 20% of my a level which is what determines what apprenticeship i get do you have an idea on how to distribute the price evenly e.g the cpu gets 45% the gpu gets 30% and the ram gets 25%
jiniux
jiniux8mo ago
you mean of the budget?
Eve.
Eve.OP8mo ago
yes
Jimmacle
Jimmacle8mo ago
sounds like just math take the budget and compute that % of it
jiniux
jiniux8mo ago
probably you won't find a solution that matches your budget 100%
Jimmacle
Jimmacle8mo ago
almost definitely wont
Eve.
Eve.OP8mo ago
yes but if the budget is 600
jiniux
jiniux8mo ago
you have to get the closest solution
Eve.
Eve.OP8mo ago
it'll get all the CPU's which are 200 and less is what i want it to do
Jimmacle
Jimmacle8mo ago
yeah that's just math
Eve.
Eve.OP8mo ago
im unsure how to code maths
Jimmacle
Jimmacle8mo ago
if you consider a percentage as a number from 0 to 1 you can use it as a multiplier like 50% of 100 -> 100 * 0.50 -> 50
Eve.
Eve.OP8mo ago
so if i do CPUAmount = textBox1.text how would i factor that into searching that wait no
jiniux
jiniux8mo ago
you can't just get the text
Jimmacle
Jimmacle8mo ago
you'll need to convert the text into a number before you can do math on it $tryparse
MODiX
MODiX8mo ago
When you don't know if a string is actually a number when handling user input, use int.TryParse (or variants, e.g. double.TryParse)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
TryParse returns a bool, where true indicates successful parsing. Remarks: - Avoid int.Parse if you do not know if the value parsed is definitely a number. - Avoid Convert.ToInt32 entirely, this is an older method and Parse should be preferred where you know the string can be parsed. Read more here
Eve.
Eve.OP8mo ago
i usually do newvariablename = Convert.ToInt32(variablename)
Jimmacle
Jimmacle8mo ago
that's an old and wrong way
Eve.
Eve.OP8mo ago
:/
jiniux
jiniux8mo ago
tryparse is better you can show an error if the text is not a number
Jimmacle
Jimmacle8mo ago
the thing i pasted above says what to use and when and you don't necessarily want an int, prices have a decimal point and ints can only represent whole numbers
Eve.
Eve.OP8mo ago
did not think about that will it be able to search through the database if the datatype for the price for the components is int32
Jimmacle
Jimmacle8mo ago
if the type in your database is also int then you might as well use ints everywhere or change the database
Eve.
Eve.OP8mo ago
i can change the database i did it before
Jimmacle
Jimmacle8mo ago
i'd do that just because prices aren't always full dollar amounts
jiniux
jiniux8mo ago
in some countries they are 🤓 ☝️
Eve.
Eve.OP8mo ago
yeah i just rounded the prices
Eve.
Eve.OP8mo ago
No description
Eve.
Eve.OP8mo ago
okay so i need to create 3 variables making the % i want into decimals timesing the amount then searching for that number
jiniux
jiniux8mo ago
you probably want the first item you get from the query
Eve.
Eve.OP8mo ago
what does that mean
jiniux
jiniux8mo ago
the query returns many records you just get the first one
Eve.
Eve.OP8mo ago
i still don't understand
jiniux
jiniux8mo ago
so, if you want to find the best cpu what would you do? what is the query you'd write?
Eve.
Eve.OP8mo ago
SELECT * FROM ram WHERE price @search => x 0.4 that was a guess
jiniux
jiniux8mo ago
that is not valid sql.. why is @search there
Eve.
Eve.OP8mo ago
No description
Eve.
Eve.OP8mo ago
that is what gets the userinput i think
jiniux
jiniux8mo ago
SELECT * FROM ram WHERE price <= (@search * 0.4) perhaps something like this it is unclear what you wanna do here
Eve.
Eve.OP8mo ago
oh i was kind of close though i think percentage wise 40% for CPU 40% for GPU 20% for RAM
jiniux
jiniux8mo ago
in any case this query will not return just one output it will return a list
Eve.
Eve.OP8mo ago
okay how do we make it return the top one or the priciest
jiniux
jiniux8mo ago
SELECT * FROM ram WHERE price <= (@search * 0.4) order by price desc and you will get the first item of the list
Eve.
Eve.OP8mo ago
it didnt like that
Eve.
Eve.OP8mo ago
No description
Eve.
Eve.OP8mo ago
No description
jiniux
jiniux8mo ago
sorry i missed a thing xD order by price desc will order the elements in the output list from the pricest to the cheapest
Eve.
Eve.OP8mo ago
oh i see
jiniux
jiniux8mo ago
if you want from the cheapest to the pricest, you do order by price asc
jiniux
jiniux8mo ago
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Eve.
Eve.OP8mo ago
it came up with the same result i did change it from 0.4 to 0.2 im not too bothered at least it works thank you
jiniux
jiniux8mo ago
🫡
Want results from more Discord servers?
Add your server