C/C++如何连接数据库(代码实现示例)

SQL(结构化查询语言)是第四代语言(4GL), 用于定义, 操纵和控制RDBMS(关系数据库管理系统)。
在开始主要文章之前, 让我们熟悉使用的工具。
编译器:Code::Block用MinGW编译器IDE
下载链接:二进制下载 – http://www.codeblocks.org/downloads/5
Code::Blocks是一个交叉编译器(它可以在Windows, Linux和Mac等任何平台上运行), 可以免费下载。该IDE专为C和C++++设计, 易于使用。
API:我们将使用SQLAPI ++库
下载链接: SQLAPI下载 – http://www.sqlapi.com/Download/index.html
SQLAPI ++是一个C++++库(基本上是一组头文件), 用于访问多个SQL数据库(Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL, SQLite, SQL Anywhere和ODBC)。它易于实现且简单。
OCCI:Oracle C++++调用接口
下载链接:OCCI C++++下载 http://www.oracle.com/technetwork/database/features/oci/index-090820.html
OCCI是数据库公司ORACLE定义的接口, 该接口定义了一个舒适的接口, 供C++++程序员使用令人联想到SQL语句的参数使用类访问Oracle数据库。该接口适用于ORACLE 9i, ORACLE 10, 并且随Oracle一起提供。
我们必须下载并安装以上三个文件(如果没有)。现在我们几乎可以开始了。
开始之前的一些设置:
-> 打开代码::blocks IDE,然后点击设置->编译器和调试器设置(现在你会看到全局编译器设置)
-> 现在点击” 链接器设置在链接器设置中, 点击添加按钮, 然后添加以下内容
对于Windows操作系统:
代码如下:
C:\SQLAPI\lib\libsqlapiddll.a
C:\Program Files\CodeBlocks\MinGW\lib\libuser32.a
C:\Program Files\CodeBlocks\MinGW\lib\libversion.a
C:\Program Files\CodeBlocks\MinGW\lib\liboleaut32.a
C:\Program Files\CodeBlocks\MinGW\lib\libole32.a
这些可以在你的SQLAPI ++中找到(如果尚未在C: 驱动器中解压缩, 则选择适当的位置, 并将提到的文件添加到链接器设置中)。
上面的代码用于添加库文件以将C++/ C++++程序与SQLAPI连接。
基本上, 有两个步骤:
连接到数据库(和错误处理)
代码如下:

//C++ pgroram for connecting to database (and error handling) #include< stdio.h> #include< SQLAPI.h> //main SQLAPI++ headerint main( int argc, char * argv[]) { //create connection object to connect to database SAConnection con; try { //connect to database //in this example, it is Oracle, //but can also be Sybase, Informix, DB2 //SQLServer, InterBase, SQLBase and ODBC con.Connect ( "test" , //database name "tester" , //user name "tester" , //password SA_Oracle_Client); //Oracle Client printf ( "We are connected!\n" ); //Disconnect is optional //autodisconnect will occur in destructor if needed con.Disconnect(); printf ( "We are disconnected!\n" ); }catch (SAException & x) { //SAConnection::Rollback() //can also throw an exception //(if a network error for example), //we will be ready try { //on error rollback changes con.Rollback (); } catch (SAException & ) { } //print error message printf ( "%s\n" , ( const char *)x.ErrText()); } return 0; }

输出如下:
We are Connected! We are Disconnected!

执行一个简单的SQL命令
现在, 我们将执行一个简单的SQL查询, 首先为数据库创建一个表:
创建表tb1(id号, 名称varchar(20);
现在, 在连接连接之后, 建立与数据库的连接;方法, 你应该使用cmd.setCommandText方法将查询传递到数据库, 如下所示:
con.Connect("test", "tester", "tester", SA_Oracle_Client); cmd.setCommandText("create table tb1(id number, name varchar(20)); ");

现在, 要执行查询, 我们必须使用以下命令:
cmd.Execute();
完整代码:
#include< stdio.h> #include < SQLAPI.h> //main SQLAPI++ header int main( int argc, char * argv[]) { SAConnection con; //connection object to connect to database SACommandcmd; //create command object try { //connect to database (Oracle in our example) con.Connect( "test" , "tester" , "tester" , SA_Oracle_Client); //associate a command with connection //connection can also be specified in SACommand constructor cmd.setConnection(& con); //create table cmd.setCommandText( "create table tbl(id number, name varchar(20)); " ); cmd.Execute(); //insert value cmd.setCommandText( "Insert into tbl(id, name) values (1, "Vinay")" ); cmd.setCommandText( "Insert into tbl(id, name) values (2, "Kushal")" ); cmd.setCommandText( "Insert into tbl(id, name) values (3, "Saransh")" ); cmd.Execute(); //commit changes on success con.Commit(); printf ( "Table created, row inserted!\n" ); }catch (SAException & x) { //SAConnection::Rollback() //can also throw an exception //(if a network error for example), //we will be ready try { //on error rollback changes con.Rollback(); } catch (SAException & ) { } //print error messageprintf ( "%s\n" , ( const char *)x.ErrText()); } return 0; }

众所周知, Oracle不是自动提交的(提交正在永久反映数据库中的数据), 因此, 我们必须提交它。
con.Commit();

同样, 当发生异常时, 我们可以回滚事务, 因此我们可以使用以下方法:
con.Rollback();

要删除一行, 我们使用此命令。
cmd.setCommandText("delete from tb1 where id= 2");

因此, 到本文结尾, 我们已经学习了如何将你的C++/ C++++程序连接到数据库并执行操作。
【C/C++如何连接数据库(代码实现示例)】如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。

    推荐阅读