Solved

Embedded SQL in C code, problem with LIKE Statement

Posted on 2003-11-17
5
246 Views
Last Modified: 2010-04-15
All,

I have a database that is suppose to be a mock of a database a library would use.  I am now writing a c program that would be the interface for the librarians and users of the library.  One of the menu options I have is an option to search for a book by its title.  For instance, one of the books in the library is "The Humane Interface".  If the user searched for "Humane", they should get that as a result.  If the user searched for "Hum", they should get that result.

My table that holds the titles of my books is called BOOK.  Here is my select statement.

SELECT TITLE
FROM BOOK
WHERE TITLE LIKE '%:title%';

:title is the value I get from the user.  It seems to be searching for ":title" instead of what :title is a variable for.  I also tried to use strcat like so:

strcpy(temp, "'%");
strcat(temp, title);
strcat(temp, "%'");

SELECT TITLE
FROM BOOK
WHERE TITLE LIKE :temp;

The SELECT statement works because if I hardcode "Humane" into the select, it returns the book.  I just need to know how to pass the variable into the SELECT statement.  I am not sure if there is a problem with newlines at the end of the string or not.

The database I am interacting with is an Oracle9i and I am using sqlplus.

Hope this is enough information.

Thanks
0
Comment
Question by:stevedave
  • 2
5 Comments
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 9767440
try this:

SELECT TITLE
FROM BOOK
WHERE TITLE LIKE (temp);
0
 

Author Comment

by:stevedave
ID: 9768009
I tried it in all the ways that I could possibly think of and none worked.  Thanks for the idea though.
0
 

Author Comment

by:stevedave
ID: 9771263
I found the solution to my problem.  When using strcat, the only thing wrong was that I did not need the single quotes.  Here is what I should have had:

strcpy(temp, "%");
strcat(temp, title);
strcat(temp, "%");

SELECT TITLE
FROM BOOK
WHERE TITLE LIKE :temp;

Just the % is put around it with no single quote.

Nate
0
 

Accepted Solution

by:
SpazMODic earned 0 total points
ID: 10420067
PAQed, with points refunded (125)

SpazMODic
EE Moderator
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I test for current date? 9 109
Pointer in one class to member in another 6 125
C#, VS15, StructLayout 1 135
Problem to ASCII 1 197
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question