C / C++ FAQs & Programming Resources - ProkutFAQ : HowToConnectDatabase

HomePage Recent Changes Recently Commented Login/Register

Revision [896]

Most recent edit made on 2009-11-06 05:39:59 by SharathAV

Additions:
MySQL provides C language APIs for communicating with MySQL databases. These APIs are platform independant and comes with the MySQL setup package. For more information on how to use these APIs, read their reference manual which can be found at:http://dev.mysql.com/doc/refman/5.0/en/c.html


Deletions:
MySQL provides C language APIs for communicating with MySQL databases. These APIs are platform independant and comes with the MySQL setup package. For more information on how to use these APIs, read their reference manual which can be found at:http://dev.mysql.com/doc/refman/5.0/en/c.html




Revision [895]

Edited on 2009-11-06 05:39:33 by SharathAV

Additions:
SOCI is an open-source database access library for C++ which can be used with MCVC++ and GCC compilers. This library lets you embed the SQL queries in C++ code using the regular C++ code style. It supports Oracle, MySQL and PostgreSQL databases. You can find documentation, examples, and more information from their project website.


Deletions:
SOCI is an open-source database access library for C++ which can be used with MCVC++ and GCC compilers. This library lets you embed the SQL queries in C++ code using the regular C++ code style. It supports Oracle, MySQL and PostgreSQL databases, you can find documentation, examples, and more information from their project website.




Revision [894]

Edited on 2009-11-06 05:38:40 by SharathAV

Additions:
SOCI is an open-source database access library for C++ which can be used with MCVC++ and GCC compilers. This library lets you embed the SQL queries in C++ code using the regular C++ code style. It supports Oracle, MySQL and PostgreSQL databases, you can find documentation, examples, and more information from their project website.


Deletions:
SOCI is an open-source database access library for C++ which can be used with MCVC++ and GCC compilers. This library lets you embed the SQL queries in C++ code using the regular C++ code style. You can find documentation, examples, and more information from their project website.




Revision [890]

Edited on 2009-11-06 05:08:48 by SharathAV

Additions:

How can we connect/access databases using C/C++ ?



Deletions:

How can we connect to database using C/C++ ?





Revision [889]

Edited on 2009-11-06 05:07:21 by SharathAV [corrections]

Additions:
MySQL provides C language APIs for communicating with MySQL databases. These APIs are platform independant and comes with the MySQL setup package. For more information on how to use these APIs, read their reference manual which can be found at:http://dev.mysql.com/doc/refman/5.0/en/c.html
SOCI - The C++ Database Access Library
SOCI is an open-source database access library for C++ which can be used with MCVC++ and GCC compilers. This library lets you embed the SQL queries in C++ code using the regular C++ code style. You can find documentation, examples, and more information from their project website.


Deletions:
MySQL provides C language APIs for communicating with MySQL databases. These APIs are platform independant and comes with the MySQL setup package. For more information on how to use these APIs, read their reference manual which can be found at:http://dev.mysql.com/doc/refman/5.0/en/c.html
- The C++ Database Access Library
SOCI is an open-source database access library for C++ which can be used with MCVC++ and GCC compilers. This library lets you embed the SQL queries in C++ code using the regular C++ code style. You can find more information on this from their project website You can find examples and documentation in .




Revision [888]

Edited on 2009-11-06 05:02:44 by SharathAV

Additions:
There are no standard libraries in C or C++ for handling Databases. However, each of the database system comes with their own libraries for C and C++, and there are numerous libraries available which can enable you to write cross-platform database applications. Also, most of the modern compilers provide libraries to have easy connectivity to most of the popular database systems.
- The C++ Database Access Library
SOCI is an open-source database access library for C++ which can be used with MCVC++ and GCC compilers. This library lets you embed the SQL queries in C++ code using the regular C++ code style. You can find more information on this from their project website You can find examples and documentation in .
If you have a 16 bit compiler like Turbo C/C++ for DOS, then you have no way to access any database as there are no libraries supporting that compiler. You will need to upgrade to newer and modern compilers like MSVC++ or GCC. See our Free compilers section to find and download compilers..


Deletions:
There are no standard libraries in C or C++ for handling any Databases. However, each of the database systems provide their libraries for C and C++ for handling databases. The libraries come with the compilers or provided by different DBMS vendors and 3rd party providers.
If you have a 16 bit compiler like Turbo C/C++ for DOS, then you have no way to access any database as there are no libraries supporting that compiler. You will need to upgrade to newer version of Turbo C++ (ie. Turbo C++ 2006 for Win32) or get any other modern compilers to use database libraries.




Revision [856]

Edited on 2009-08-09 14:30:45 by SharathAV

Additions:
/*The MySQL library header file*/
/*#define the database details such as the host, the username, the password, and the database to connect to*/
#define username "root"
#define password "123456"
#define database "testdb"
/*You will have to change the values in the quotes to match the values that fit your database details. */
if( conn
NULL )
printf("Failed to initate MySQL\n");
return 1;
/*The next step is to actually connect to the database. This is where the conn variable and the host,
if( ! mysql_real_connect(conn,host,username,password,database,0,NULL,0) )
printf( "Error connecting to database: %s\n", mysql_error(conn));
return 1;
mysql_query(conn,"SELECT name, email, password FROM users");
/*In our example, there will most likely be more than one row returned (i.e., if there are more than one user in
the users table of the database). If so, then you need to find how many rows are returned so that you can
unsigned int num_fields = mysql_num_fields(res_set);
/*Finally, we retrive the result using the function mysql_fetch_row() and then print out all of the data in large chunks.
for(i = 0; i < num_fields; i++)
printf("%s\t", row[i] ? row[i] : "NULL");
printf("\n");
/*Last but certainly not least, we close our connection to the database*/


Deletions:
/*The 2 lines of code include the proper files in order to be able to compile the project. They are:*/
/*The several lines are used to #define the database details such as the host, the username, the password, and the database to connect to. Instead of creating 4 char pointers, I use 4 #define's like so:*/
#define username "db_username"
#define password "db_password"
#define database "db"
/*You will have to change the values in the quotes to match the values that fit you. */
/*This program, being as simple as it is, will contain only one user defined function, main() and the rest of the functions used will be from the mySQL++ API (application program interface).*/
/*The next lines are ones that we all know and love:*/
/*The next step is to actually connect to the database. This is where the conn variable and the host,
mysql_real_connect(conn,host,username,password,database,0,NULL,0);
/*The next 3 lines are code that define variables to get a result and row pointer from the database,
along with a variable to increment in the loop if there is more than one row returned by the query:*/
mysql_query(conn,"SELECT * FROM users WHERE userid=1");
/*In our example, there will most likely be more than one row returned (i.e., if there are more than one user in
the users table of the database). If so, then you need to find how many rows are returned so that you can
/*Finally, we retrive the result using the function mysql_fetch_row() and then print out all of the data in large chunks.
/*However, looping through the number of rows returned is useless if you don't find the number of fields in each row
(in this case, they are all the same because the query comes from the same table, but, if the query were produced
on the fly [in the while loop], then the function mysql_num_fields() is used to find the number of fields). I hope that
makes sense. Here goes:*/
for (i = 0; i < mysql_num_fields(res_set); i++)
/*And finally, after all this time, we print the stuff out using the plain old C standard function printf():*/
printf("%s\n",row[ i ] ! = NULL ? row : "NULL");
/*Last but certainly not least, we close our connection to the database. When there are multiple connects and many users
accessing the database at the same time, it is essential that you connect and disconnect as soon as possible, or you can
have disasterous results. The final lines of code disconnect from the database and then exit the main() function:*/




Revision [808]

Edited on 2009-05-07 03:27:15 by SharathAV

Additions:
There are no standard libraries in C or C++ for handling any Databases. However, each of the database systems provide their libraries for C and C++ for handling databases. The libraries come with the compilers or provided by different DBMS vendors and 3rd party providers.
MySQL
Below is an example of connecting and querying an MySQL Database, courtesy: Delbrooks's post in C community thread.
If you have a 16 bit compiler like Turbo C/C++ for DOS, then you have no way to access any database as there are no libraries supporting that compiler. You will need to upgrade to newer version of Turbo C++ (ie. Turbo C++ 2006 for Win32) or get any other modern compilers to use database libraries.


Deletions:
There are no standard libraries in C or C++ for handling any DBMS. We can only connect and communicate with DBMS using platform specific libraries that come with the compilers or 3rd party libraries, or libraries provided by different DBMS vendors.
If you have a 16 bit compiler like Turbo C/C++ for DOS, then you cannot connect or access any database as there are no libraries supporting that compiler. You will need the new version of Turbo C++ (ie. Turbo C++ 2006 for Win32) which provides libraries for many different databases such as MSAccess, MySQL, Oracle, InterBase etc. You can download the new Turbo C++ 2006 (Explorer Edition) for free from http://turboexplorer.com/cpp. The help and documentation for Turbo C++ 2006 contains examples for connecting and querying different databases.
Example for connecting and querying an MySQL Database, courtesy: Delbrooks's post in C community thread.




Revision [757]

Edited on 2008-11-18 08:48:11 by SharathAV

Additions:

for (i = 0; i < mysql_num_fields(res_set); i++)


Deletions:

for (i=0; i<mysql_num_fields(res_set); i++)




Revision [377]

Edited on 2007-06-06 08:18:14 by SharathAV

Deletions:
Unknown action "adsenseleaderboard"




Revision [326]

Edited on 2007-02-27 06:38:15 by SharathAV

Additions:

How can we connect to database using C/C++ ?

There are no standard libraries in C or C++ for handling any DBMS. We can only connect and communicate with DBMS using platform specific libraries that come with the compilers or 3rd party libraries, or libraries provided by different DBMS vendors.
If you have a 16 bit compiler like Turbo C/C++ for DOS, then you cannot connect or access any database as there are no libraries supporting that compiler. You will need the new version of Turbo C++ (ie. Turbo C++ 2006 for Win32) which provides libraries for many different databases such as MSAccess, MySQL, Oracle, InterBase etc. You can download the new Turbo C++ 2006 (Explorer Edition) for free from http://turboexplorer.com/cpp. The help and documentation for Turbo C++ 2006 contains examples for connecting and querying different databases.
MySQL provides C language APIs for communicating with MySQL databases. These APIs are platform independant and comes with the MySQL setup package. For more information on how to use these APIs, read their reference manual which can be found at:http://dev.mysql.com/doc/refman/5.0/en/c.html


Deletions:

How to connect to database using C/C++ ?

There are no standard libraries in C or C++ for DBMS. We can only connect and communicate with DBMS using platform specific libraries that come with the compilers or 3rd party libraries, or libraries provided by different DBMS vendors.
If you have a 16 bit compiler like Turbo C/C++ for DOS, then you cannot connect or access any database, because there are no libraries supporting Turbo C/C++ compilers for DOS. However, if you have the new version of Turbo C++, which is Turbo C++ 2006 for Win32, then you can connect and work with many different database management systems such as MSAccess, MySQL, Oracle, InterBase etc. You can download the new Turbo C++ 2006 (Explorer Edition) for free from http://turboexplorer.com/cpp. The help and documentation for Turbo C++ 2006 contains examples for connecting different databases.
MySQL provides a rich set of C API's for communicating with MySQL databases. For more information, read their reference manual which can be found at:http://dev.mysql.com/doc/refman/5.0/en/c.html




Revision [323]

Edited on 2007-02-19 09:06:58 by SharathAV

Additions:
MySQL C API
OTL
OTL is a platform independent database library. It covers the functionality of a whole database API with just a handful of concrete classes and several template PL/SQL (Oracle) table container classes. The OTL code gets expanded into direct database API function calls, so it provides ultimate performance, reliability and thread safety in multi-processor environments as well as traditional batch programs. It supports Oracle 7 (natively via OCI7), Oracle 8 (natively via OCI8), Oracle 8i (natively via OCI8i), Oracle 9i (natively via OCI9i), Oracle 10g (natively via OCI10g), DB2 (natively via DB2 CLI), ODBC 3.x as well as ODBC 2.5 compliant data sources in MS Windows and Unix (e.g. Oracle, MS SQL Server, Sybase, Informix, MySQL, DB2, Interbase / Firebird, PostgreSQL, SQLite, SAP/DB, TimesTen, MS ACCESS, etc.). The list of supported database backends is constantly growing.
MySQL++
SQLAPI++ Library
SQLAP++ provides C++ APIs which can be used to access different databases, such as Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL and ODBC. You can find more information and links to download the library from: http://www.sqlapi.com/


Deletions:
MySQL C API
SQLAPI++ Library
SQLAP++ is a set of C++ APIs which can be used to access different databases, such as Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL and ODBC. You can find more information and links to download the library from: http://www.sqlapi.com/
MySQL++




Revision [311]

Edited on 2007-02-06 08:52:49 by SharathAV

Additions:

Related Links

ODBC from C Tutorial by Easysoft.
Unknown action "adsenseleaderboard"




Revision [181]

Edited on 2006-11-06 15:57:07 by SharathAV [MySQL connection example added]

Additions:
Example for connecting and querying an MySQL Database, courtesy: Delbrooks's post in C community thread.
/*The 2 lines of code include the proper files in order to be able to compile the project. They are:*/
#include <stdio.h>
#include <mysql.h>
/*The several lines are used to #define the database details such as the host, the username, the password, and the database to connect to. Instead of creating 4 char pointers, I use 4 #define's like so:*/
#define host "localhost"
#define username "db_username"
#define password "db_password"
#define database "db"
/*You will have to change the values in the quotes to match the values that fit you. */
/*The next line creates a pointer of type MYSQL to the active database connection:*/
MYSQL *conn;
/*This program, being as simple as it is, will contain only one user defined function, main() and the rest of the functions used will be from the mySQL++ API (application program interface).*/
/*The next lines are ones that we all know and love:*/
/*Followed by that, we tell mySQL that are are about to connect with the mysql_init() function.
We pass a value of NULL to it because for our purposes, we don't need to go into it any further:*/
conn = mysql_init(NULL);
/*The next step is to actually connect to the database. This is where the conn variable and the host,
username, password, and database #define's are used:*/
mysql_real_connect(conn,host,username,password,database,0,NULL,0);
/*The next 3 lines are code that define variables to get a result and row pointer from the database,
along with a variable to increment in the loop if there is more than one row returned by the query:*/
MYSQL_RES *res_set;
MYSQL_ROW row;
unsigned int i;
/*Finally, we query the database with the following query using the mysql_query() function: "SELECT * FROM users" like so:*/
mysql_query(conn,"SELECT * FROM users WHERE userid=1");
/*After querying the result, we need to store the result data in the variable res_set we defined earlier. This is done like so:*/
res_set = mysql_store_result(conn);
/*In our example, there will most likely be more than one row returned (i.e., if there are more than one user in
the users table of the database). If so, then you need to find how many rows are returned so that you can
loop through each one and print the result of each one like so:*/
unsigned int numrows = mysql_num_rows(res_set);
/*Finally, we retrive the result using the function mysql_fetch_row() and then print out all of the data in large chunks.
This is done like so:*/
while ((row = mysql_fetch_row(res_set)) != NULL)
{
/*However, looping through the number of rows returned is useless if you don't find the number of fields in each row
(in this case, they are all the same because the query comes from the same table, but, if the query were produced
on the fly [in the while loop], then the function mysql_num_fields() is used to find the number of fields). I hope that
makes sense. Here goes:*/
for (i=0; i<mysql_num_fields(res_set); i++)
{
/*And finally, after all this time, we print the stuff out using the plain old C standard function printf():*/
printf("%s\n",row[ i ] ! = NULL ? row : "NULL");
}
}
/*Last but certainly not least, we close our connection to the database. When there are multiple connects and many users
accessing the database at the same time, it is essential that you connect and disconnect as soon as possible, or you can
have disasterous results. The final lines of code disconnect from the database and then exit the main() function:*/
mysql_close(conn);
return 0;
Below is an example code on how to connect to Sybase using C on UNIX flavors:
DBPROCESS *dbproc;
LOGINREC *login;
DBCHAR name1[15];
DBINT no1;
RETCODE ret_code;
fflush(stdout);
dbinit();
login=dblogin();
DBSETLUSER(login,<Login name>);
DBSETLPWD(login,<Password>);
dbproc=dbopen(login,NULL);
dbcmd(dbproc,"select * from table");
dbsqlexec(dbproc);
return 0;


Deletions:
Here's an example code on how to connect to Sybase using C on UNIX flavors:
DBPROCESS *dbproc;
LOGINREC *login;
DBCHAR name1[15];
DBINT no1;
RETCODE ret_code;
fflush(stdout);
dbinit();
login=dblogin();
DBSETLUSER(login,<Login name>);
DBSETLPWD(login,<Password>);
dbproc=dbopen(login,NULL);
dbcmd(dbproc,"select * from table");
dbsqlexec(dbproc);
return 0;




Revision [179]

Edited on 2006-11-04 05:16:22 by SharathAV

Additions:
There are no standard libraries in C or C++ for DBMS. We can only connect and communicate with DBMS using platform specific libraries that come with the compilers or 3rd party libraries, or libraries provided by different DBMS vendors.

Connecting to Database using Turbo C/C++

If you have a 16 bit compiler like Turbo C/C++ for DOS, then you cannot connect or access any database, because there are no libraries supporting Turbo C/C++ compilers for DOS. However, if you have the new version of Turbo C++, which is Turbo C++ 2006 for Win32, then you can connect and work with many different database management systems such as MSAccess, MySQL, Oracle, InterBase etc. You can download the new Turbo C++ 2006 (Explorer Edition) for free from http://turboexplorer.com/cpp. The help and documentation for Turbo C++ 2006 contains examples for connecting different databases.


Deletions:
There are no standard libraries in C or C++ for DBMS. We can only communicate with DBMS using platform specific libraries that come with the compilers or 3rd party libraries, or libraries provided by different DBMS vendors.




Revision [168]

Edited on 2006-10-31 13:13:17 by SharathAV

Additions:

Code examples



Deletions:
Code examples




Revision [167]

Edited on 2006-10-31 13:10:32 by SharathAV

Additions:
MySQL C API
MySQL provides a rich set of C API's for communicating with MySQL databases. For more information, read their reference manual which can be found at:http://dev.mysql.com/doc/refman/5.0/en/c.html
SQLAP++ is a set of C++ APIs which can be used to access different databases, such as Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL and ODBC. You can find more information and links to download the library from: http://www.sqlapi.com/


Deletions:
[to be added]
SQLAP++ is a set of C++ APIs which can be used to access different databases, such as Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL and ODBC. You can find more information and links to download the library from:
http://www.sqlapi.com/




Revision [166]

Edited on 2006-10-31 13:02:25 by SharathAV

Additions:

How to connect to database using C/C++ ?

There are no standard libraries in C or C++ for DBMS. We can only communicate with DBMS using platform specific libraries that come with the compilers or 3rd party libraries, or libraries provided by different DBMS vendors.

DBMS Libraries for C

[to be added]

DBMS Libraries for C++

SQLAPI++ Library
SQLAP++ is a set of C++ APIs which can be used to access different databases, such as Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL and ODBC. You can find more information and links to download the library from:
http://www.sqlapi.com/
MySQL++
MySQL++ provides C++ wrapper classes for C API's of MySQL database. More information can be found on their website at:
http://tangentsoft.net/mysql++/
Code examples
Here is an MSDN link on how to connect to an Oracle database using VC++:
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnaroledbp/html/msdn_ole4orcl.asp
Here's an example code on how to connect to Sybase using C on UNIX flavors:
/* Anyone who understands this code, please edit this page and add comments*/
int main()
return 0;


Deletions:
Connect to database : using C++ to Oracle
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnaroledbp/html/msdn_ole4orcl.asp
Connect to database : using C to Sybase on UNIX falvours
void main()




Revision [135]

The oldest known version of this page was edited on 2006-10-23 12:15:59 by s2-1.bear.com (unregistered user)
Connect to database : using C++ to Oracle
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnaroledbp/html/msdn_ole4orcl.asp

Connect to database : using C to Sybase on UNIX falvours
#include<iostream.h>
#include<sybfront.h>
#include<sybdb.h>
void main()
{
DBPROCESS *dbproc;
LOGINREC *login;
DBCHAR name1[15];
DBINT no1;
RETCODE ret_code;
fflush(stdout);
dbinit();
login=dblogin();
DBSETLUSER(login,<Login name>);
DBSETLPWD(login,<Password>);
dbproc=dbopen(login,NULL);
dbcmd(dbproc,"select * from table");
dbsqlexec(dbproc);
}
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.1952 seconds