Gossamer Forum
Home : General : Databases and SQL :

sql to check if value exists in DB

Quote Reply
sql to check if value exists in DB
What would the code/sql to check if a value ($category_id) exists in table ('Category') under column ('FatherID')?

Code:
my $id_exists = 1; # 1 - Yes, 0-No in this case it is safer to defult to yes, as # this will be part of a delete function.

$id_exists = $DB->('Category')->select->('FatherID'==$category_id) ????


I might be way off on thisCrazy

Thanks!

Last edited by:

sooke: May 14, 2002, 11:57 PM
Quote Reply
Re: [sooke] sql to check if value exists in DB In reply to
I assume you are using GT::SQL libraries. Then try:
Code:
my $id_exists = 1;
$id_exists = $DB->table('Category')->count( { FatherID => $category_id } );
$obj->count($cond) gives you the number of records that satisfy the condition $cond.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] sql to check if value exists in DB In reply to
Why are you setting $id_exists to 1?

You could use the count or:

Code:
if ( $DB->table('Category')->select( { FatherID => $category_id }, 'ID' )->fetchrow ) {
print "found";
}

Last edited by:

Paul: May 15, 2002, 2:28 AM
Quote Reply
Re: [Paul] sql to check if value exists in DB In reply to
You're right, best thing is to use the if statement.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [Paul] sql to check if value exists in DB In reply to
Paul and Yogi to the resuce. Thanks both.

I figured the you could get an sql statement to return a bool value somehow. Yes, I just want to stick this in a function which returns a bool.

So could say if &Has_Children() then blah blah.

Smile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [sooke] sql to check if value exists in DB In reply to
Code:
use constant EXISTS => sub { $DB->table('Category')->select( { FatherID => $_[0] }, 'ID' )->fetchrow };


if (EXISTS->($category_id)) {
print "OK";
}
else {
print "NOT OK";
}


....Im just showing off now Laugh
Quote Reply
Re: [Paul] sql to check if value exists in DB In reply to
Thats ok Paul.... I am just cutting and pasting the fruits of your showing off!!! ThanksSmile
Quote Reply
Re: [Paul] sql to check if value exists in DB In reply to
I'm suprprised that you didn't write that:

Code:
EXISTS->($category_id) ? print "OK" : print "NOT OK";

;-)

- wil
Quote Reply
Re: [Wil] sql to check if value exists in DB In reply to
Why have cotton when you can have silk? Tongue


Code:
print EXISTS->($category_id) ? "OK" : "NOT OK";
Quote Reply
Re: [Paul] sql to check if value exists in DB In reply to
This statement is getting so small, I should just forget about putting it into a sub!Wink

I guess I can substitute PRINT for RETURN 1 : RETURN 0 ?

Last edited by:

sooke: May 15, 2002, 9:15 AM
Quote Reply
Re: [sooke] sql to check if value exists in DB In reply to
Where are you returning to?

...the constant is doing the returning, you then need to do something with the returned value.
Quote Reply
Re: [Paul] sql to check if value exists in DB In reply to
But you are probably better off making it a count rather then a select (less overhead):

sub { $DB->table('Category')->count( { FatherID => $_[0] } ) };

and no point using constant module since you don't gain the compile time benefit. Just do:

sub exists { $DB->table('Category')->count( { FatherID => $_[0] } ) }

and then:

print exists($category_id) ? "ok" : "not ok";

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Paul] sql to check if value exists in DB In reply to
I see your points, and thanks Alex. Lets put this in the scheme of what I am trying to do.

I am going to use this statement in my category tidy up tools.

Right now i'd like to have this check for subcats BEFORE any delte statement could be performed, so subcategories are not removed by accident.

For Example:

In Links - Remove all Categories which are children (any depth below or Directory Depth below) "The Category" AND contain 0 links (Number_of_Links == 0) AND &has_children() returns false. (this is the statment we are getting at here)

I (thanks to you Paul) I now understand that as long as I use $DB, the appropriate changes will automatically be effected on the other table when I removed a category, such as CatLinks.

I am thinking of a stack of uses for these types of little checks, for clean up tools and automated checking of my categories, since they are edited by editors.
Quote Reply
Re: [Alex] sql to check if value exists in DB In reply to
Yep I was just showing off, it wasn't necessary at all

Is:

$obj->count

...the same as....

$obj->select( 'COUNT(*)' )

?
Quote Reply
Re: [Paul] sql to check if value exists in DB In reply to
Yes, pretty much. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] sql to check if value exists in DB In reply to
Ok, just to confirm I am getting this:

#determine if category has children
sub has_children { $DB->table('Category')->count( { FatherID => $_[0] } ) }

if (&has_children{$category_id})

{

Dont Delete

}

else

{

Remove Category

}

Last edited by:

sooke: May 15, 2002, 2:55 PM
Quote Reply
Re: [sooke] sql to check if value exists in DB In reply to
Close,

sub has_children { $DB->table('Category')->count( { FatherID => $_[0] } ) }

if (has_children($category_id)) {

}
else {

}

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] sql to check if value exists in DB In reply to
Thanks Alex!

You know I debated taking that '&' out, but left it in. Tremendous Smile