代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
用c进行简单的postgresql操作
作者:
OP菠菜
/ 发布于
2015/4/8
/
627
#include <stdio.h> #include <string.h> #include <assert.h> #include <libpq-fe.h> #include "error.h" #include "pgsql.h" typedef struct tagInfo { char name[24]; char age[8]; char birthday[24]; }INFO_S; int main(int argc, char **argv) { int ret, i = 0; INFO_S stInfo; DB_HANDLER_S stHandler; //const char *command = "INSERT INTO usertbl(name, age, birthday) VALUES('win', 23, '1992-07-23');"; const char *command = "SELECT * FROM usertbl;"; memset(&stInfo, 0, sizeof(stInfo)); memset(&stHandler, 0, sizeof(stHandler)); ret = DB_Connect(&stHandler); if (ERROR_SUCCESS != ret) { return ret; } ret = DB_Exec(&stHandler, command); if (ERROR_SUCCESS != ret) { DB_Close(&stHandler); return ret; } printf("affect rows:%ld, tuples:%ld, fields:%ld\r\n", stHandler.affect_rows, stHandler.tuples, stHandler.fields); snprintf(stInfo.name, sizeof(stInfo.name), "%s", DB_Fetch(&stHandler, 0, i++)); snprintf(stInfo.age, sizeof(stInfo.age), "%s", DB_Fetch(&stHandler, 0, i++)); snprintf(stInfo.birthday, sizeof(stInfo.birthday), "%s", DB_Fetch(&stHandler, 0, i++)); printf("%s, %s, %s\r\n", stInfo.name, stInfo.age, stInfo.birthday); DB_Close(&stHandler); return ERROR_SUCCESS; } 2. [文件] pgsql.c ~ 2KB 下载(1) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <libpq-fe.h> #include "error.h" #include "pgsql.h" static void db_ClearResult(DB_HANDLER_S *handler) { if (NULL != handler->result) { PQclear(handler->result); handler->result = NULL; } return; } int DB_Connect(DB_HANDLER_S *handler) { const char *conninfo = "host='127.0.0.1' dbname='exampledb' user='dbuser' password='123456'"; assert(NULL != handler); handler->connection = PQconnectdb(conninfo); if (NULL == handler->connection) { printf("connect db failed !\r\n"); return ERROR_CONNECTDB_FAILED; } return ERROR_SUCCESS; } int DB_Exec(DB_HANDLER_S *handler, const char *command) { db_ClearResult(handler); handler->result = PQexec(handler->connection, command); if (NULL == handler->result) { printf("db exec sql failed ! ERROR : %s\r\n", PQerrorMessage(handler->connection)); handler->exec_status = PGRES_FATAL_ERROR; } else { handler->exec_status = PQresultStatus(handler->result); } switch (handler->exec_status) { case PGRES_COMMAND_OK://insert { handler->affect_rows = strtoul(PQcmdTuples(handler->result), NULL, 0); break; } case PGRES_TUPLES_OK://select { handler->tuples = (unsigned long) PQntuples(handler->result); handler->fields = (unsigned long) PQnfields(handler->result); break; } default: { printf("db exec sql failed ! ERROR : %s\r\n", PQerrorMessage(handler->connection)); break; } } return ERROR_SUCCESS; } char *DB_Fetch(DB_HANDLER_S *handler, unsigned long tuple, unsigned long field) { return PQgetvalue(handler->result, tuple, field); } void DB_Close(DB_HANDLER_S *handler) { db_ClearResult(handler); PQfinish(handler->connection); handler->connection = NULL; return; } #ifndef PGSQL_H #define PGSQL_H typedef struct tagDBHandler { PGconn *connection; ExecStatusType exec_status; PGresult *result; unsigned long affect_rows; /*for insert, update, delete*/ unsigned long tuples; /*for select*/ unsigned long fields; /*for select*/ }DB_HANDLER_S; int DB_Connect(DB_HANDLER_S *handler) ; int DB_Exec(DB_HANDLER_S *handler, const char *command); char *DB_Fetch(DB_HANDLER_S *handler, unsigned long tuple, unsigned long field); void DB_Close(DB_HANDLER_S *handler); #endif /* PGSQL_H */ #ifndef ERROR_H #define ERROR_H #define ERROR_SUCCESS (int)0 #define ERROR_FAILED (int)1 #define ERROR_CONNECTDB_FAILED (int)2 #define ERROR_EXECDB_FAILED (int)3 #endif /* ERROR_H */
试试其它关键字
postgresql
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
OP菠菜
贡献的其它代码
(
1
)
.
用c进行简单的postgresql操作
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3