hi,
 In recently, I have a mission for porting a source code from Pro*C. And the database is changed from Oracle to Mysql. The part of original source code(Pro*C) is shown below:

char i_id_no[13+1];
char accn_name[16+1], account[7+1], b_no[20+1], code_type[1];
int ii;

:
:
EXEC SQL DECLARE cursor_1 CURSOR FOR
SELECT accn_name, account, b_no, code_type
FROM ACCOUNTINFO
WHERE id_no = :i_id_no;
EXEC SQL OPEN cursor_1;

for(ii=0;; )
{
EXEC SQL FETCH cursor_1
INTO :accn_name, :account, :b_no, :code_type;
if (sqlca.sqlcode == 1403)
break;
:
:
}
:
:
EXEC SQL CLOSE cursor_1;

How could I make the same functions by mysql C API?

ps.
compiler: gcc
os: RH Linux AS 4.0
Mysql: 5.0

since you are going to rewrite the program you might as well use ODBC to make it more generic to any database -- next time the database changes you won't have to change the c code very much. google for "odbc" and you will find lots of c++ classes and other C function libraries.

thanks for you reply in advance.
what I want is just "how could I implement the CURSOR by Mysql C API?"
our project is only work in C(the entire system is compiled by gcc), so I didn't know how ODBC could work here also(I never used ODBC before).

thanks for you reply in advance.
what I want is just "how could I implement the CURSOR by Mysql C API?"
our project is only work in C(the entire system is compiled by gcc), so I didn't know how ODBC could work here also(I never used ODBC before).

ODBC can be accessed via several languages -- C, C++, java, perl, php, vb, and probably many others.

You probably don't need to set up a cursor at all. ODBC contains functions that will retrieve the result set one row at a time. All you have to do is put the SELECT statement in a C string, send it to the database using ODBC function, then create a loop to call ODBC fetch() until fetch() tells your program there is no more data.

I realize you want just a "quick and dirty" fix for your problem, but often that is not a good long-term solution. If this project is for where you work then it will be to your advantage, and your company's advantage, for you to spend a few days to learn ODBC. It doesn't really replace the code you have now but is a different way of accessing databases. ODBC still uses SQL language, but unlike the code you have now ODBC will send it to the database server to be processed.


[edit] Here is a good C tutorial.[/edit]

Thank you all about told me the solution to fix my problem , I will try to solve the problem by your way, also ODBC.
maybe mysql C API will be the better way to fix it (because of performance), but ODBC still is a seconary solution(maybe a easier way). I will try it anyway

Thank you guys.

This article has been dead for over six months. Start a new discussion instead.