纠结的libpq代码 PQexecParams

弄懂了也不难,可就是当时费了好大的劲,插图片至postgresql,大家可以做参考

#include <stdio.h>

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "libpq-fe.h"

/* for ntohl/htonl */
#include <netinet/in.h>
#include <arpa/inet.h>


static void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}

int main(int argc, char **argv)
{
    const char *conninfo;
    PGconn       *conn;
    PGresult   *res;
    const char *paramValues[2];
    int        paramLengths[2];
    int        paramFormats[2];

    FILE    *fp,*filep;
    char buffer[1024];
    char * filename;
    char * filebuf;
    int size;
    
    if (argc > 1)
        conninfo = argv[1];
    else
        conninfo = "dbname=postgres";

    conn = PQconnectdb(conninfo);
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "Connection to database failed: %s",PQerrorMessage(conn));
        exit_nicely(conn);
    }

    fp = fopen("filename","rb");
    if(fp == NULL)
    {

        printf("error open filename");
        exit(1);
    }

    
    while(!feof(fp))
    {
        int i = 0;
        res = PQexec(conn, "BEGIN");
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            fprintf(stderr, "LISTEN command failed: %s", PQerrorMessage(conn));
            PQclear(res);
            exit_nicely(conn);
        }
        PQclear(res);

        while( i<500)
        {

            i ++;

            if((fgets(buffer,1024,fp)) == NULL)

            {

                        printf("get buffer error\n");

                        break;

            }

            filename = strstr(buffer,"\n");
            *filename = '\0';
            filename = strstr(buffer,"/");
            filename ++;
            //fputs(buffer,stdout);
            filep = fopen(buffer,"rb");
            if(filep == NULL)
            {
                printf("error open file\n");
                exit(1);
            }
        
            printf("reading file %s.......",filename);

            fseek(filep,0,SEEK_END);
            size=ftell(filep);
            fseek(filep,0,SEEK_SET);

            printf("%d\n",size);
            filebuf = (char *)malloc(size);
            if(fread(filebuf,size,1,filep) < 1)
                if(ferror(filep))
                {
                    printf("read error\n");
                    return 0;
                }
            fclose(filep);

            paramValues[0] = filename;
            paramValues[1] = filebuf;
            paramLengths[0] = 0;
            paramLengths[1] = size;
            paramFormats[0] = 0;
            paramFormats[1] = 1;

            printf("filename %s...\n",paramValues[1]);
            printf("size %d....\n",paramLengths[0]);
                        printf("size %d....\n",paramLengths[1]);

            res = PQexecParams(conn,
                               "insert into image(name,photo) values($1::text,$2::bytea)",
                               2,
                               NULL,
                               paramValues,
                               paramLengths,
                               paramFormats,
                               0);
            if (PQresultStatus(res) != PGRES_COMMAND_OK)
            {
                fprintf(stderr, "SELECT failed: %s\n", PQerrorMessage(conn));
                PQclear(res);
                exit_nicely(conn);

            }
            PQclear(res);

            free(filebuf);
        }


        res = PQexec(conn, "END");
        PQclear(res);
    }
    fclose(fp);

    PQfinish(conn);
    return 0;
}

编译命令:sudo gcc -o photo photo.c -I /usr/include/postgresql/ -L /usr/lib/postgresql/8.4/lib/ -lpq -g

你可能感兴趣的:(纠结的libpq代码 PQexecParams)