数据库编程 学习C语言 Oracle 数据库编程之旅(c语言的oracle)

数据库编程 学习C语言 Oracle 数据库编程之旅

数据库编程是当今信息技术领域中不可或缺的一部分。在、大数据等技术的推动下,数据库更是成为了互联网产业的核心。因此,在学习编程的过程中,掌握数据库编程技术是尤为重要的。

C语言是一门历史悠久、应用广泛的编程语言,也是数据库编程中的重要一环。C语言可以帮助开发者编写高效、稳定的程序,并与数据库进行连接。而Oracle数据库又因为其高效性、可扩展性和安全性而成为业界受欢迎的数据库之一。

接下来,我们将开始Oracle数据库编程之旅,探索C语言和Oracle数据库的完美结合。

我们需要安装Oracle数据库和C语言开发环境。在安装Oracle数据库之后,我们需要创建一个数据库实例,然后创建必要的用户和数据表。

在C语言开发环境中,我们需要引入Oracle的头文件和库文件,以便在程序中进行数据库操作。具体操作如下:

#include 
#include
#include
#include
// 引入Oracle头文件

int mn()
{
OCIEnv *envhp; // OCI 句柄
OCIError *errhp;
OCISvcCtx *svchp;
OCIServer *srvhp;
OCIDefine *defhp;
OCIBind *bindhp;
OCILobLocator *locp;
OCIDateTime *datetime;
OCINumber *number;
OCIStmt *sthp;
char username[64]; // 数据库用户名
char password[64]; // 数据库密码
char dbname[64]; // 数据库名
char sql[1024] = "SELECT * FROM user"; // SQL 语句
sword status;
sb4 ora_errno;
text ora_errmsg[512];

// 定义变量和句柄

OCIInitialize((ub4) OCI_DEFAULT,
(dvoid *)0,
(dvoid * (*)(dvoid *, size_t))0,
(dvoid * (*)(dvoid *, dvoid *, size_t))0,
(void (*)(dvoid *, dvoid *))0);
OCIEnvInit(&envhp, OCI_DEFAULT, 0, (dvoid **)0);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, 0, 0);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&srvhp, OCI_HTYPE_SERVER, 0, 0);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, 0, 0);
OCIHandleAlloc((dvoid *)envhp, (dvoid **)&sthp, OCI_HTYPE_STMT, 0, 0);
// 初始化 Oracle 相关句柄

printf("Please input the username: ");
scanf("%s", username);
printf("Please input the password: ");
scanf("%s", password);
printf("Please input the dbname: ");
scanf("%s", dbname);
// 输入数据库信息

OCIAttrSet((dvoid *)svchp, OCI_HTYPE_SVCCTX,
(dvoid *)srvhp, (ub4)0,
OCI_ATTR_SERVER, errhp);
OCIServerAttach(srvhp, errhp,
(text *)dbname, strlen(dbname),
OCI_DEFAULT);
OCIAttrSet((void*)svchp, OCI_HTYPE_SVCCTX, (void*)sthp, 0, OCI_ATTR_STMT_CACHE_SIZE, errhp);
OCIAttrSet((void*)sthp, OCI_HTYPE_STMT, (void*)&sql, strlen(sql), OCI_ATTR_STATEMENT_TEXT, errhp);
// 连接到数据库

printf("Connected to Oracle database.\n");

OCIStmtPrepare(sthp, errhp, (text *)sql, strlen(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIStmtExecute(svchp, sthp, errhp, (ub4)0, (ub4)0,
(OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT);
printf("SQL statement executed.\n");
// 执行 SQL 语句

return 0;
}

在代码中,我们定义了各种变量和 OCI 句柄,然后初始化这些句柄,接着连接到了 Oracle 数据库,并执行了 SQL 语句。在连接到 Oracle 数据库时,我们输入了数据库的用户名、密码和数据库名等信息,这些信息有助于连接到指定的数据库实例。

除此之外,我们还可以在程序中进行数据的增删改查等操作,以实现更多功能。比如:

// 插入数据
char insert_sql[1024] = "INSERT INTO user VALUES (:1, :2, :3)";
OCIStmtPrepare(sthp, errhp, (text *)insert_sql, strlen(insert_sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIBind *val1, *val2, *val3;
OCIParamGet(sthp, OCI_HTYPE_STMT, errhp, (void**)&val1, 1);
OCIParamGet(sthp, OCI_HTYPE_STMT, errhp, (void**)&val2, 2);
OCIParamGet(sthp, OCI_HTYPE_STMT, errhp, (void**)&val3, 3);
char *name = "John";
OCIAttrSet((dvoid*)val1, OCI_HTYPE_BIND, (dvoid*)name, strlen(name), OCI_ATTR_BUFFER, errhp);
char *eml = "john@example.com";
OCIAttrSet((dvoid*)val2, OCI_HTYPE_BIND, (dvoid*)eml, strlen(eml), OCI_ATTR_BUFFER, errhp);
int age = 26;
OCIAttrSet((dvoid*)val3, OCI_HTYPE_BIND, (dvoid*)&age, sizeof(age), OCI_ATTR_BUFFER, errhp);
OCIStmtExecute(svchp, sthp, errhp, (ub4)1, (ub4)0,
(OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT);
// 删除数据
char delete_sql[1024] = "DELETE FROM user WHERE name = :1";
OCIStmtPrepare(sthp, errhp, (text *)delete_sql, strlen(delete_sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIBind *val;
OCIParamGet(sthp, OCI_HTYPE_STMT, errhp, (void**)&val, 1);

char *name = "John";
OCIAttrSet((dvoid*)val, OCI_HTYPE_BIND, (dvoid*)name, strlen(name), OCI_ATTR_BUFFER, errhp);
OCIStmtExecute(svchp, sthp, errhp, (ub4)1, (ub4)0,
(OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT);
// 更新数据
char update_sql[1024] = "UPDATE user SET age = :1 WHERE name = :2";
OCIStmtPrepare(sthp, errhp, (text *)update_sql, strlen(update_sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIBind *val1, *val2;
OCIParamGet(sthp, OCI_HTYPE_STMT, errhp, (void**)&val1, 1);
OCIParamGet(sthp, OCI_HTYPE_STMT, errhp, (void**)&val2, 2);
int age = 30;
OCIAttrSet((dvoid*)val1, OCI_HTYPE_BIND, (dvoid*)&age, sizeof(age), OCI_ATTR_BUFFER, errhp);
char *name = "John";
OCIAttrSet((dvoid*)val2, OCI_HTYPE_BIND, (dvoid*)name, strlen(name), OCI_ATTR_BUFFER, errhp);
OCIStmtExecute(svchp, sthp, errhp, (ub4)1, (ub4)0,
(OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT);
// 查询数据
char select_sql[1024] = "SELECT * FROM user WHERE age > :1";
OCIStmtPrepare(sthp, errhp, (text *)select_sql, strlen(select_sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
OCIBind *val;
OCIParamGet(sthp, OCI_HTYPE_STMT, errhp, (void**)&val, 1);

int age = 20;
OCIAttrSet((dvoid*)val, OCI_HTYPE_BIND, (dvoid*)&age, sizeof(age), OCI_ATTR_BUFFER, errhp);
OCIStmtExecute(svchp, sthp, errhp, (ub4)0, (ub4)0,
(OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT);
while (OCIStmtFetch(sthp, errhp, (ub4)1, OCI_FETCH_NEXT,
OCI_DEFAULT) == OCI_SUCCESS)
{
// 处理查询

数据运维技术 » 数据库编程 学习C语言 Oracle 数据库编程之旅(c语言的oracle)