Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Scripting Languages (http://www.programmingforums.org/forum39.html)
-   -   mysql database search (http://www.programmingforums.org/showthread.php?t=13432)

programmingnoob Jun 27th, 2007 3:01 AM

mysql database search
 
so I am writing a C#program that determines whether a given name exists in the MySql database

after determining the database and all that...
first, I execute
:

"select * from tablename"
on C#....
and then I go through the entries until it finds the given name....

alternatively, what I could do is use...
:

select * from tablename where name = 'example';
but then I dont know how C# would catch it if mysql tells it that it matched nothing, which implies empty set.


I was also wondering... whether my approach is appropriate and efficient for searching large databases... or are there better (yet simple) approaches?

programmingnoob Jun 27th, 2007 3:09 AM

Quote:

Originally Posted by programmingnoob (Post 129692)
but then I dont know how C# would catch it if mysql tells it that it matched nothing, which implies empty set.


I was also wondering... whether my approach is appropriate and efficient for searching large databases... or are there better (yet simple) approaches?

I mean for example, while inserting an entry into mysql, it can catch for exception and determine if error number is 1062, then the entry already exists....

Infinite Recursion Jun 27th, 2007 9:04 AM

I suggest your alternative approach, but make sure you take the case sensitivity into consideration. Depending on how you query the database, normally you could determine if the query yielded any results based on how many records were returned from the query.

The "already exists" error is something that can be handled also, again, depending on how you are connection to the database and which method you are using to submit transactions.

programmingnoob Jun 27th, 2007 11:31 AM

Quote:

Originally Posted by Infinite Recursion (Post 129715)
I suggest your alternative approach, but make sure you take the case sensitivity into consideration. Depending on how you query the database, normally you could determine if the query yielded any results based on how many records were returned from the query.

The "already exists" error is something that can be handled also, again, depending on how you are connection to the database and which method you are using to submit transactions.

oh I know how to deal with 'already exists' error....
what I was asking is what does it do when I execute the below command using C#
:

select * from tablename where name = 'example';
then if there is no 'example' entry in name column, then what does it return to C#, that I could check against?
I know I am not saying it in a very clear way...

lectricpharaoh Jun 27th, 2007 2:34 PM

If you're using VS.NET 2005, you might consider using the wizards for the table adapter, etc stuff. Then you can create your queries with the wizard as well, or enter the actual SQL statements if you're comfortable with that. Then, when you execute your select query, you get a table filled with zero or more records. Simply count the number of rows (ie records), and you know whether anything was returned.

Infinite Recursion Jun 27th, 2007 3:07 PM

"then if there is no 'example' entry in name column, then what does it return to C#, that I could check against?"

Execute the query using an invalid value for the name, then print out what the resulting value is (probably null), and note the Exception returned. There may also be a mysql_fetch_row type of function that will yield 0 or null based on the results of the query that is passed in. If time permits, I'll tinker with it a bit when I get home and respond with a more descriptive answer.

Fourth Jun 27th, 2007 5:55 PM

Quote:

Originally Posted by Infinite Recursion (Post 129727)
Execute the query using an invalid value for the name, then print out what the resulting value is (probably null), and note the Exception returned. There may also be a mysql_fetch_row type of function that will yield 0 or null based on the results of the query that is passed in.

this is the answer to my knowledge. at least in my experience with asking for things in a database in mysql the return of an empty result is null.


All times are GMT -5. The time now is 4:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC