c语言数据库查询编程,c语言编程基础之数据库操作

查询:

vi opsql.ec

C代码

EXEC SQL include sqlca.h;

#include 

#include 

intmain(intargc,char**argv)

{

EXEC SQL BEGIN DECLARE SECTION;

charname[50];

chardbname[20];

EXEC SQL END DECLARE SECTION;

strcpy(dbname,"hostdb");

EXEC SQL CONNECT TO :dbname;

if(sqlca.sqlcode)

{

printf("opeb db error,code=%d\n",sqlca.sqlcode);

return-1;

}

EXEC SQL DECLARE cur1 CURSOR FOR

SELECT khxm FROM accoinfo;

EXEC SQL OPEN cur1;

if(sqlca.sqlcode)

{

printf("open cur1 error,code=%d\n",sqlca.sqlcode);

return-1;

}

while(1)

{

memset(name,0x00,sizeof(name));

EXEC SQL FETCH cur1 INTO :name;

if(sqlca.sqlcode==100)break;

elseif(sqlca.sqlcode){

printf("open cur1 error,code=%d\n",sqlca.sqlcode);

return-1;

}

printf("name is [%s]\n",name);

}

EXEC SQL CLOSE cur1;

EXEC SQL DISCONNECT CURRENT;

return0;

}

EXEC SQL include sqlca.h;

#include

#include

int main(int argc,char **argv)

{

EXEC SQL BEGIN DECLARE SECTION;

char name[50];

char dbname[20];

EXEC S

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c语⾔数据库查询系统,C语⾔实现sqlite3数据库查询的基本⽅ 法 sqlite回传函数相关 说了3种基本⽅法:callback,gettable和预处理stmt⽅法 下⾯给出测试代码和测试⽤数据库,代码如下 #include #include #include #include "sqlite3.h" // //typedef int (*sqlite3_callback)( // void* data, /* Data provided in the 4th argument of sqlite3_exec() */ // int ncols, /* The number of columns in row */ // char** values, /* An array of strings representing fields in the row */ // char** headers /* An array of strings representing column names */ //); int callback(void* data, int ncols, char** values, char** headers) { int i; int len =0; int ll=0; for(i=0; i < ncols; i++) { if(strlen(headers[i])>len) len = strlen(headers[i]); } for(i=0; i < ncols; i++) { ll = len-strlen(headers[i]); while(ll) { fprintf(stdout," "); --ll; } fprintf(stdout, "%s: %sn", headers[i], values[i]); } fprintf(stdout, "n"); return 0; } int search_by_callback(const char* db_name, const char* sql_cmd) { int i = 0 ; int j = 0 ; int nrow = 0, ncolumn = 0; char **azResult; //⼆维数组存放结果 sqlite3 *db=NULL; char *zErrMsg = 0; int rc; int len=0; if(access(db_name, 0) == -1) { fprintf(stderr, "%s not foundn", db_name); return -1; } rc = sqlite3_open(db_name, &db); if( rc != SQLITE_OK) { fprintf(stderr, "%s open failed: %sn", db_name,sqlite3_errmsg(db)); sqlite3_close(db); return -1; } //查询数据 rc = sqlite3_exec( db,sql_cmd, callback, NULL, &zErrMsg ); if( rc != SQLITE_OK) { fprintf(stderr, "%s %s: %sn", db_name,sql_cmd, sqlite3_errmsg(db)); if(zErrMsg) { fprintf(stderr,"ErrMsg = %s n", zErrMsg); sqlite3_free(zErrMsg); } sqlite3_close(db); return -1; } if(zErrMsg) { sqlite3_free(zErrMsg); } //关闭数据库 sqlite3_close(db); return 0; } int search_by_table(const char* db_name, const char* sql_cmd) { int i = 0 ; int j = 0 ; int nrow = 0, ncolumn = 0; char **azResult; //⼆维数组存放结果 sqlite3 *db=NULL; char *zErrMsg = 0; int rc; int len=0; if(access(db_name, 0) == -1) { fprintf(stderr, "%s not foundn", db_name); return -1; } rc = sqlite3_open(db_name, &db); if( rc != SQLITE_OK) { fprintf(stderr, "%s open failed: %sn"

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值