C
C#3mo ago
Eve.

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

nothing is working
No description
88 Replies
Jimmacle
Jimmacle3mo 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.3mo 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
jiniux3mo ago
you don't need %% for that
Eve.
Eve.3mo ago
No description
Eve.
Eve.3mo ago
what shall i put where the error is
jiniux
jiniux3mo ago
you wrote searchPriceCPU, but the parameter is searchTermCPU
Eve.
Eve.3mo ago
omg i think it worked i have been trying to figure this out for weeks
Jimmacle
Jimmacle3mo ago
...weeks?
jiniux
jiniux3mo ago
💀
Eve.
Eve.3mo ago
thank you so much i owe you my life i am new to this.
Jimmacle
Jimmacle3mo ago
maybe you should go through some SQL tutorials so you understand what you're writing
jiniux
jiniux3mo ago
perhaps chatgpt may have helped you xD
Jimmacle
Jimmacle3mo ago
no chatgpt is awful for beginners
Eve.
Eve.3mo ago
believe me it did not
Jimmacle
Jimmacle3mo 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
jiniux3mo ago
ur right, i was just saying that chatgpt may have pointed out that the variable names were incorrect
Eve.
Eve.3mo 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
jiniux3mo ago
i don't want to let you down, but the way you're coding this does not follows today's standards
Eve.
Eve.3mo ago
my teacher doesn't help at all, i have to teach myself and idk where to go or what to do
Jimmacle
Jimmacle3mo ago
if it's a school project then raw dogging ADO.NET is expected tbh
jiniux
jiniux3mo ago
ah, then okay
Jimmacle
Jimmacle3mo ago
school courses are behind the times
Eve.
Eve.3mo ago
i did connect it to my database and created a database on my own though brownie points for me
Jimmacle
Jimmacle3mo 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.3mo ago
No description
Jimmacle
Jimmacle3mo ago
it's not specific to mysql but it might help you understand the queries you're writing
jiniux
jiniux3mo 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.3mo ago
i have actually never been so happy i want to do this after school but i am shit at computer science
jiniux
jiniux3mo ago
nobody is born knowing everything
Jimmacle
Jimmacle3mo ago
you can only get better with practice :bless:
Eve.
Eve.3mo ago
very true do you guys want to help with other aspects of my project fine if not may as well ask
jiniux
jiniux3mo ago
try to solve things on your own first. if you get stuck, just open a new post with the underlying problem
Eve.
Eve.3mo ago
believe me i have i don't know where to start
jiniux
jiniux3mo ago
what do you have to do?
Eve.
Eve.3mo 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
jiniux3mo ago
ye that's what i was about to say why is there age in the first place xD
Eve.
Eve.3mo 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
jiniux3mo ago
you mean of the budget?
Eve.
Eve.3mo ago
yes
Jimmacle
Jimmacle3mo ago
sounds like just math take the budget and compute that % of it
jiniux
jiniux3mo ago
probably you won't find a solution that matches your budget 100%
Jimmacle
Jimmacle3mo ago
almost definitely wont
Eve.
Eve.3mo ago
yes but if the budget is 600
jiniux
jiniux3mo ago
you have to get the closest solution
Eve.
Eve.3mo ago
it'll get all the CPU's which are 200 and less is what i want it to do
Jimmacle
Jimmacle3mo ago
yeah that's just math
Eve.
Eve.3mo ago
im unsure how to code maths
Jimmacle
Jimmacle3mo 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.3mo ago
so if i do CPUAmount = textBox1.text how would i factor that into searching that wait no
jiniux
jiniux3mo ago
you can't just get the text
Jimmacle
Jimmacle3mo ago
you'll need to convert the text into a number before you can do math on it $tryparse
MODiX
MODiX3mo 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.3mo ago
i usually do newvariablename = Convert.ToInt32(variablename)
Jimmacle
Jimmacle3mo ago
that's an old and wrong way
Eve.
Eve.3mo ago
:/
jiniux
jiniux3mo ago
tryparse is better you can show an error if the text is not a number
Jimmacle
Jimmacle3mo 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.3mo 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
Jimmacle3mo ago
if the type in your database is also int then you might as well use ints everywhere or change the database
Eve.
Eve.3mo ago
i can change the database i did it before
Jimmacle
Jimmacle3mo ago
i'd do that just because prices aren't always full dollar amounts
jiniux
jiniux3mo ago
in some countries they are 🤓 ☝️
Eve.
Eve.3mo ago
yeah i just rounded the prices
Eve.
Eve.3mo ago
No description
Eve.
Eve.3mo ago
okay so i need to create 3 variables making the % i want into decimals timesing the amount then searching for that number
jiniux
jiniux3mo ago
you probably want the first item you get from the query
Eve.
Eve.3mo ago
what does that mean
jiniux
jiniux3mo ago
the query returns many records you just get the first one
Eve.
Eve.3mo ago
i still don't understand
jiniux
jiniux3mo ago
so, if you want to find the best cpu what would you do? what is the query you'd write?
Eve.
Eve.3mo ago
SELECT * FROM ram WHERE price @search => x 0.4 that was a guess
jiniux
jiniux3mo ago
that is not valid sql.. why is @search there
Eve.
Eve.3mo ago
No description
Eve.
Eve.3mo ago
that is what gets the userinput i think
jiniux
jiniux3mo ago
SELECT * FROM ram WHERE price <= (@search * 0.4) perhaps something like this it is unclear what you wanna do here
Eve.
Eve.3mo ago
oh i was kind of close though i think percentage wise 40% for CPU 40% for GPU 20% for RAM
jiniux
jiniux3mo ago
in any case this query will not return just one output it will return a list
Eve.
Eve.3mo ago
okay how do we make it return the top one or the priciest
jiniux
jiniux3mo 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.3mo ago
it didnt like that
Eve.
Eve.3mo ago
No description
Eve.
Eve.3mo ago
No description
jiniux
jiniux3mo 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.3mo ago
oh i see
jiniux
jiniux3mo ago
if you want from the cheapest to the pricest, you do order by price asc
jiniux
jiniux3mo 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.3mo 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
jiniux3mo ago
🫡