SQL(结构化查询语言)是第四代语言(4GL),用于定义、操作和控制RDBMS(关系数据库管理系统)。
在开始本文之前,让我们先熟悉一下使用过的工具。
-
编译程序: 代码::使用MinGW编译器阻止IDE
下载链接: 二进制下载 Blocks是一个交叉编译器(可以在Windows、Linux和Mac等任何平台上运行),可以免费下载。该IDE是专为C和C++设计的,使用方便。
-
应用程序编程接口: 我们将使用SQLAPI++库
下载链接: SQLAPI下载
SQLAIP++是一个C++库(基本上是一组头文件),用于访问多个SQL数据库(Oracle、SQL Server、DB2、Sybase、Informix、Inbase、SQLbase、MySQL、PostgreSQL、SQLite、SQL随处和ODBC)。它易于实现且简单。
-
OCCI: Oracle C++调用接口
下载链接: OCI C++下载 OCI是由数据库公司Oracle定义的接口,它定义了一个舒适的接口,用于C++程序员使用具有令人想起SQL语句的参数来访问类数据库。该接口适用于ORACLE 9i和ORACLE 10,并随ORACLE提供。
我们必须下载并安装以上三个(如果我们没有)。现在我们几乎可以开始了。
启动前的一些设置:
->打开code::blocks IDE并转到或单击 设置 -> 编译器和调试器设置 (现在您将看到全局编译器设置)
->现在点击“ 链接器设置 “在链接器设置中,单击“添加”按钮并添加以下内容:
对于 Windows操作系统 :
代码:
C:SqlApilibSqlApidll。A.
C:程序文件代码块SMINGWLIBILIBUSER32。A.
C:程序文件代码块smingwliblibversion。A.
C:程序文件CodeBlocksMingWLibleBoleAut32。A.
C:程序文件代码块smingwliblibole32。A.
这些文件将在SQLAPI++中找到(如果尚未在C:drive中提取,请选择适当的位置,并将提到的文件添加到链接器设置中)。
上面的代码用于添加库文件,以将C/C++程序与SQLAPI连接起来。
基本上有两个步骤:
- 连接到数据库(和错误处理) 代码:
// C++ pgroram for connecting to database (and error handling)
#include<stdio.h>
#include<SQLAPI.h> // main SQLAPI++ header
int
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!"
);
// Disconnect is optional
// autodisconnect will occur in destructor if needed
con.Disconnect();
printf
(
"We are disconnected!"
);
}
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"
, (
const
char
*)x.ErrText());
}
return
0;
}
输出:
We are Connected! We are Disconnected!
- 执行一个简单的SQL命令 现在,我们将执行一个简单的SQL查询。首先,为数据库创建一个表:
创建表tb1(id号,名称varchar(20);
现在,建立到数据库的连接,然后,在你的con。连接方法应该使用cmd。setCommandText方法将查询传递到数据库,如下所示:
con.Connect("test", "tester", "tester", SA_Oracle_Client); cmd.setCommandText("create table tb1(id number, name varchar(20));”);
现在,要执行查询,我们必须使用以下命令:
cmd。执行();
完整代码:
#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!"
);
}
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"
, (
const
char
*)x.ErrText());
}
return
0;
}
正如我们所知,Oracle不是自动提交的(提交是在数据库中永久反映数据),所以我们必须提交它。
con.Commit();
同样,我们可以在异常发生时回滚事务,因此我们使用:
con.Rollback();
要删除一行,我们使用以下命令。
cmd.setCommandText("delete from tb1 where id= 2");
因此,在本文结束时,我们已经学习了如何将C/C++程序连接到数据库并执行操作。
本文由 维奈·加格 .如果你喜欢GeekSforgek,并且想贡献自己的力量,你也可以写一篇文章,并将文章邮寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论