• Postgresql使用笔记


    0.下载&安装

      系统,虚拟机中的centos6.3. 直接yum进行下载,下载完要进行数据库初始化操作,还有修改登录数据库的验证方式,还有数据库不能用root用户进行数据库管理。

      安装需要选择性安装,postgresql, postgresql-devel, postgresql-libs, postgresql-libs, posgresql-test, postgresql-server.

    1.一些用到的命令

     1 使用yum安装好postgresql后,要进行初始化
     2 #service postgresql initdb
     3 修改数据库配置文件 
     4 #vim /var/lib/pgsql/data/postgresql.conf
     5 主要修改这几个
     6 listen_addresses = '*'
     7 port = 5432
     8 其他按需进行修改
     9 启动数据库
    10 #service postgresql start  或 #systemctl start postgresql.service
    11 切换用户并登录,修改密码
    12 #su postgres
    13 #psql
    14 #alter user postgres with password '' ;
    15 出现这个错误 psql: 致命错误:  用户 "postgres" Ident 认证失败
    16 修改认证文件 
    17 #vim /var/lib/pgsql/data/pg_hba.conf
    18 最后几行配置 修改认证METHOD的ident为trust,实现用帐号密码来访问数据库。
    19 最后重启服务
    20 #service postgresql restart

    2.开发

      1 #include <stdio.h>
      2 #include <libpq-fe.h>
      3 
      4 int postgresql_insert(PGconn *conn,char *sql);
      5 int postgresql_update(PGconn *conn,char *sql);
      6 int postgresql_delete(PGconn *conn,char *sql);
      7 int postgresql_select(PGconn *conn,char *sql);
      8 int postgresql_count(PGconn *conn,char *sql);
      9 
     10 int main()
     11 {
     12     int i;
     13     int ret = 0;
     14     //for(i=0;i<10000;i++)
     15     {
     16     char sql[512];
     17     const char *conninfo="hostaddr=127.0.0.1 port=5432 user=postgres dbname=postgres";
     18     PGconn * conn;
     19 
     20     //conn = PQsetdb(pghost,pgport,pgoptions,pgtty,dbName);
     21     conn = PQconnectdb(conninfo);
     22 
     23     if(PQstatus(conn) == CONNECTION_BAD)
     24     {
     25         fprintf(stderr,"%s
    ",PQerrorMessage(conn));
     26         PQfinish(conn);
     27         return 0;
     28     }
     29 
     30     sprintf(sql,"insert into test(id,name,pwd) values(1,'root','pwddddd'); ");
     31     postgresql_insert(conn,sql);
     32 
     33     sprintf(sql,"update test set id=2,name='caonima',pwd='中文' where pwd='pwddd'; ");
     34     postgresql_update(conn,sql);
     35 
     36     sprintf(sql,"select * from test;");
     37     postgresql_select(conn,sql);
     38 
     39     sprintf(sql,"select * from test;");
     40     ret = postgresql_count(conn,sql);
     41     printf("count --- >  %d.
    ",ret);
     42     sprintf(sql,"select * from logs; ");
     43     ret = postgresql_count(conn,sql);
     44     printf("count --- >  %d.
    ",ret);
     45 
     46     sprintf(sql,"delete from test where id=1; ");
     47     postgresql_delete(conn,sql);
     48 
     49     PQfinish(conn);
     50     }
     51     return 0;
     52 }
     53 
     54 int postgresql_count(PGconn *conn,char *sql)
     55 {
     56     PGresult *res;
     57     int ret;
     58 
     59     res = PQexec(conn,sql);
     60     if(!res || PQresultStatus(res) != PGRES_TUPLES_OK)
     61     {
     62         fprintf(stderr,"Select Error.
    ");
     63         PQclear(res);
     64         return -1;
     65     }
     66     ret = PQntuples(res);
     67     PQclear(res);
     68 
     69     return ret;
     70 }
     71 
     72 int postgresql_select(PGconn *conn,char *sql)
     73 {
     74     PGresult *res;
     75     int nFields=0;
     76     int i,j;
     77 
     78     res = PQexec(conn,sql); //select 查询自带事务
     79     if(!res || PQresultStatus(res) != PGRES_TUPLES_OK)
     80     {
     81         fprintf(stderr,"Select Error.
    ");
     82         PQclear(res);
     83         return -1;
     84     }
     85 
     86     nFields = PQnfields(res);
     87     for(i=0;i<nFields;i++)
     88     {
     89         printf("%-15s",PQfname(res,i));
     90     }
     91     printf("
    ");
     92     for(i=0;i<PQntuples(res);i++)
     93     {
     94         for(j=0;j<nFields;j++)
     95         {
     96             printf("%-15s",PQgetvalue(res,i,j));
     97         }
     98         printf("
    ");
     99     }
    100     printf("
    
    ");
    101     PQclear(res);
    102 
    103     return 0;
    104 }
    105 
    106 int postgresql_delete(PGconn *conn,char *sql)
    107 {
    108     int ret = postgresql_insert(conn,sql);
    109     return ret;
    110 }
    111 
    112 int postgresql_update(PGconn *conn,char *sql)
    113 {
    114     int ret = postgresql_insert(conn,sql);
    115     return ret;
    116 }
    117 
    118 int postgresql_insert(PGconn *conn,char *sql)
    119 {
    120     PGresult * res;
    121 
    122     res = PQexec(conn,"BEGIN"); //显式事务
    123 
    124     if(!res || PQresultStatus(res) != PGRES_COMMAND_OK)
    125     {
    126         PQclear(res);
    127         return -1;
    128     }
    129 
    130     //should PQclear PGresult whenever it is no longer needed to avoid memory leaks.
    131     PQclear(res); //每次这个res的PGresult结合都要进行关闭,防止内存泄漏
    132 
    133     res = PQexec(conn,sql);
    134     if(!res || PQresultStatus(res) != PGRES_COMMAND_OK)
    135     {
    136         fprintf(stderr,"%s
    ",PQerrorMessage(conn));
    137         PQclear(res);
    138         return -1;
    139     }
    140     PQclear(res);
    141 
    142     res = PQexec(conn,"COMMIT");
    143     PQclear(res);
    144     return 0;
    145 }
    # gcc -ggdb test.c -o test -lpq  #我 postgresql 安装后有自带这个libpq库

    参考资料:

    http://www.linuxidc.com/Linux/2014-09/106772.htm

  • 相关阅读:
    运行时动态的创建form的方法
    用X++代码来动态的改变表的属性
    使用WinAPI类来查找文件
    用循环得到表中所有的字段
    用X++建立和调用报表(Report)
    JAVA 保留字
    Cygwin使用
    系统程序员成长计划-算法与容器(三) (上)
    系统程序员成长计划工程管理(二)
    系统程序员成长计划-算法与容器(三) (下)
  • 原文地址:https://www.cnblogs.com/wunaozai/p/4610757.html
Copyright © 2020-2023  润新知