Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database 1

Status
Not open for further replies.

funyman

Programmer
Sep 20, 2000
13
0
0
CA
Hi
Can we connect to a Database with C? I heard there was a SQL header file, any one know about that ? [sig][/sig]
 
I think you need an ODBC Driver for the database and then it should be no problem. [sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
Ok ... but do you know the functions or the header file ?
how to open it , querry etc

funyman [sig][/sig]
 
I do not know this header File. Probably it is dependent on the compiler and/or Operating System. [sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
Hi,

I have a source in C++ to connect to a DataBase over the ODBC, when you're interesting, you can write a mail.

SiM [sig][/sig]
 
Thanks but no thanks ! What I'm really looking for, is a way to do it in C.

In C++ it's very easy and you have allot of .hlp on this, but I just can't find anything in C

[sig][/sig]
 
Hmmm ... know I the problem, because I have the same, I realy search for a ANSI C example to connect to a Database.
At the moment I have programmed with C++. When you find any source, can you please email me ?

Thank you.

SiM [sig][/sig]
 
hello SiM and funyman,

can u plzz expalin me how to connect ato database using C++. i am very much in need of ur hard core help... plz help me.

awaiting for ur reply..

dexter
 
Install the ODBC Driver and look in the Manual of your Compiler for the ODBC-Procedures.

Excuse me for this short answer but the handling of this topic cannot be explained within two words.

It is dependent on your Compiler, your application and on your database you are using.

I have done some projects on Windows 9x/NT, Borland C++Builder, Microsoft Access or MS-SQL-Server, and there no major Problem occured.
[sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
@dexters,

I can email you the source, when u want. Is it OK ?!

SiM [sig][/sig]
 
Actually I could do that, but those applications are pretty complex (>1 MB source code).Perhaps if I know what you really need I could prepare a small sample for you.

But then I need your Email Address. You can send it to me, and i will look for some examples). Perhaps we can post it here too.
[sig]<p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br> [/sig]
 
Hi hnd,

I'm very interesting in a source example. I need a example to connect in a DataBase, set SQL commands like SELECT * FROM bla ... and modify a DataBase. When it is in ANSI C it would be very great.

SiM

You can email me: virtual.studios@gmx.net Thanks a lot. [sig][/sig]
 
Hi hnd,

r u taking about C or C++. If it's c I will also be interest in this sample
Thanks [sig][/sig]
 
hi,

i just need a code to put the values in a text file to the database thru a C or C++ program. how this can be doone. my id is ramesh_tnj@yahoo.com

dexters [sig][/sig]
 
Hey all,

Can someone please post HERE a very short example of how to connect to an Access 97 database using ANSI C on a Windows 9x platform.

I think this would clear up all of the above mess as we would have something to actually look at!!!

Regards Napalm2000
hlechler@hotmail.com
 
This sample was tested on unix, so i put some windows stuff and it runs on win too.
So , comment or uncomment and it will run.


/* odbc.c

testing unixODBC
*/
#include <windows.h> //windows specific.
#include <stdlib.h>
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
//#include <odbc/sql.h> // unix
//#include <odbc/sqlext.h>
//#include <odbc/sqltypes.h>

SQLHENV V_OD_Env; // Handle ODBC environment
long V_OD_erg; // result of functions
SQLHDBC V_OD_hdbc; // Handle connection

char V_OD_stat[10]; // Status SQL
SQLINTEGER V_OD_err,V_OD_rowanz,V_OD_id;
SQLSMALLINT V_OD_mlen,V_OD_colanz;
char V_OD_msg[200],V_OD_buffer[200];


SQLHSTMT V_OD_hstmt; // Handle for a statement
SQLINTEGER V_OD_err,V_OD_id;
//char V_OD_buffer[200];


int main(int argc,char *argv[])
{
// 1. allocate Environment handle and register version
V_OD_erg=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&V_OD_Env);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf(&quot;Error AllocHandle\n&quot;);
exit(0);
}
V_OD_erg=SQLSetEnvAttr(V_OD_Env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf(&quot;Error SetEnv\n&quot;);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}
// 2. allocate connection handle, set timeout
V_OD_erg = SQLAllocHandle(SQL_HANDLE_DBC, V_OD_Env, &V_OD_hdbc);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf(&quot;Error AllocHDB %d\n&quot;,V_OD_erg);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}
SQLSetConnectAttr(V_OD_hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
// 3. Connect to the datasource &quot;web&quot;
V_OD_erg = SQLConnect(V_OD_hdbc, (SQLCHAR*) &quot;DSN&quot;, SQL_NTS,
(SQLCHAR*) &quot;USERNAME&quot;, SQL_NTS,
(SQLCHAR*) &quot;PASSWORD&quot;, SQL_NTS);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf(&quot;Error SQLConnect %d\n&quot;,V_OD_erg);
SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1,
V_OD_stat, &V_OD_err,V_OD_msg,100,&V_OD_mlen);
printf(&quot;%s (%d)\n&quot;,V_OD_msg,V_OD_err);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}
printf(&quot;Connected !\n&quot;);
V_OD_erg=SQLAllocHandle(SQL_HANDLE_STMT, V_OD_hdbc, &V_OD_hstmt);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf(&quot;Fehler im AllocStatement %d\n&quot;,V_OD_erg);
SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, V_OD_stat,&V_OD_err,V_OD_msg,100,&V_OD_mlen);
printf(&quot;%s (%d)\n&quot;,V_OD_msg,V_OD_err);
SQLDisconnect(V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}


V_OD_erg=SQLBindCol(V_OD_hstmt,2,SQL_C_CHAR, &V_OD_buffer,150,&V_OD_err);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf(&quot;Error binding buffer %d\n&quot;,V_OD_erg);
SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, V_OD_stat,&V_OD_err,V_OD_msg,100,&V_OD_mlen);
printf(&quot;%s (%d)\n&quot;,V_OD_msg,V_OD_err);
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);
SQLDisconnect(V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}

V_OD_erg=SQLBindCol(V_OD_hstmt,1,SQL_C_ULONG,&V_OD_id,150,&V_OD_err);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf(&quot;Error binding ID %d\n&quot;,V_OD_erg);
SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, V_OD_stat,&V_OD_err,V_OD_msg,100,&V_OD_mlen);
printf(&quot;%s (%d)\n&quot;,V_OD_msg,V_OD_err);
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);
SQLDisconnect(V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}


V_OD_erg=SQLExecDirect(V_OD_hstmt,&quot;SELECT * FROM table&quot;,SQL_NTS);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf(&quot;Error in Select %d\n&quot;,V_OD_erg);
SQLGetDiagRec(SQL_HANDLE_DBC, V_OD_hdbc,1, V_OD_stat,&V_OD_err,V_OD_msg,100,&V_OD_mlen);
printf(&quot;%s (%d)\n&quot;,V_OD_msg,V_OD_err);
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);
SQLDisconnect(V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}
V_OD_erg=SQLNumResultCols(V_OD_hstmt,&V_OD_colanz);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);
SQLDisconnect(V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}
printf(&quot;Number of Columns %d\n&quot;,V_OD_colanz);


V_OD_erg=SQLRowCount(V_OD_hstmt,&V_OD_rowanz);
if ((V_OD_erg != SQL_SUCCESS) && (V_OD_erg != SQL_SUCCESS_WITH_INFO))
{
printf(&quot;Number ofRowCount %d\n&quot;,V_OD_erg);
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);
SQLDisconnect(V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
exit(0);
}
printf(&quot;Number of Rows %d\n&quot;,V_OD_rowanz);
V_OD_erg=SQLFetch(V_OD_hstmt);
while(V_OD_erg != SQL_NO_DATA)
{
printf(&quot;Result: %d %s\n&quot;,V_OD_id,V_OD_buffer);
V_OD_erg=SQLFetch(V_OD_hstmt);
} ;
SQLFreeHandle(SQL_HANDLE_STMT,V_OD_hstmt);
SQLDisconnect(V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,V_OD_hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, V_OD_Env);
return(0);
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top