Character conversion, Can = or != be converted from string?

P

Pedro Pinto

Hi again to everyone!

First of all i would like to thank all the people in this forum for
their help, i've gone far in my project and i have to thank you for
that! I hope i can repay all the help given.

My question is this, can i make a converstion from a string to an = or
!= Operators?

I have a client program that sends information to a suposed "sql
server" that processes the requests. Both Create and Insert are easy
but Update and Select use = or != operands. Of course i can do a
comparison of the caracters inserted with if and elses but the
eficiency of the program would go sky high if a direct comparison could
be made. For example:

UPDATE Table_name SET colum_name = new_value WHERE colum_name =
old_value

or

SELECT column FROM table WHERE column operator value

My question applies better to the Select command has with the Update
command this question is also easy to solve.
Can this be done? Thanks in advance for any help.

Best Regards

Pedro Pinto
 
S

santosh

Pedro said:
Hi again to everyone!

First of all i would like to thank all the people in this forum for
their help, i've gone far in my project and i have to thank you for
that! I hope i can repay all the help given.

My question is this, can i make a converstion from a string to an = or
!= Operators?
<snip>

Your question is not very clear, but if you're asking whether you can
directly compare two strings with ==, (not =), or !=, then no, C
doesn't have such a feature. You can however use a library function to
compare two or more strings. The standard library functions, strcmp()
and strncmp() do exactly this. Note that they're case sensitive. If you
want to ignore case, convert the strings to a common case via tolower()
or toupper() and then compare.
 
S

santosh

santosh said:
Pedro Pinto wrote:
My question is this, can i make a converstion from a string to an = or
!= Operators?
<snip>

Your question is not very clear, but if you're asking whether you can
directly compare two strings with ==, (not =), or !=, then no, [ ... ]

There're also memcmp() and strcoll().
 
B

Ben Pfaff

Pedro Pinto said:
My question is this, can i make a converstion from a string to an = or
!= Operators?

I have a client program that sends information to a suposed "sql
server" that processes the requests. Both Create and Insert are easy
but Update and Select use = or != operands. Of course i can do a
comparison of the caracters inserted with if and elses but the
eficiency of the program would go sky high if a direct comparison could
be made. For example:

It's really not clear to me what you're trying to do. It's not
even clear whether you're trying to write the client or the
server program. Can you give some more details?
 
S

Simon Biber

Pedro said:
I have a client program that sends information to a suposed "sql
server" that processes the requests. Both Create and Insert are easy
but Update and Select use = or != operands. Of course i can do a
comparison of the caracters inserted with if and elses but the
eficiency of the program would go sky high if a direct comparison could
be made. For example:

There's no way to automatically execute code found in a string. C code
is compiled by a compiler into machine code. Once your program is
running, the compiler is no longer available. If you need to evaluate
arbitrary expressions supplied at run time, you must write your own
interpreter.
UPDATE Table_name SET colum_name = new_value WHERE colum_name =
old_value

or

SELECT column FROM table WHERE column operator value

You want to write a SQL server? You want to write a program that
understands and executes UPDATE and SELECT commands?

You need to parse the command into an array or list of tokens:

{"SELECT", "column", "FROM", "table",
"WHERE", "column", "operator", "value"}

Then identify the keywords, and get the variable data (column name, the
table name, etc). You will need to write code that searches through the
data. The simplest way would be to go through each record in your
database and match it against the given conditions.

Here's some pseudocode. You need to implement the functions parse,
get_value, and add_result.

parse("SELECT name FROM classmates WHERE sex = female",
select_column,
select_table,
where_column,
where_operator,
where_value);

/* After tokenisation and parsing now
select_column is "name",
select_table is "classmates",
where_column is "sex",
where_operator is "=",
and where_value is "female"
*/

for(row = 0; row < num_rows; row++)
{
const char *data_value = get_value(row, where_column);
if(!strcmp(where_operator, "=")
{
if(!strcmp(where_value, data_value)) add_result(row);
}
else if(!strcmp(where_operator, "!=")
{
if(strcmp(where_value, data_value)) add_result(row);
}
else
{
fprintf(stderr, "Unrecognised operator %s\n", where_operator);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top