zoukankan      html  css  js  c++  java
  • 学习笔记之postgresql

    
    

    /*************************************************************************
    创建人:LYK 创建时间:2019/05/05 14:47
    IDE: vs2013 库版本:32位 静态库
    数据库管理平台 pgAdmin III -- postgresql

    
    

    注意事项:
    1,添加头文件 及库文件
    libpq-fe.h libpq.lib

    
    

    2,电脑开启postgresql数据库服务
    **************************************************************************/

    void pg_test::pg_init()
    {
        char *pghost = "127.0.0.1",
            *pgport = {0},
            *pgoptions = { 0 },
            *pgtty = { 0 };
    
        char  *dbName = "db_cctv";
        int  nFields = 0;
        int  i = 0;
        int j =0;
    
    #ifdef DEBUG
        FILE    *debug;
    #endif  /* DEBUG */
    
        PGconn    *conn = {0};
        PGresult   *res = { 0 };
    
        /*
        * Begin by setting the parameters for a backend connection.
        * If the parameters are NULL, the system tries to use
        * reasonable defaults by looking up environment variables
        * or, failing that, using hardwired constants.
        */
    
        const char *conninfo = "postgresql://dxh:123456@localhost:5432/db_cctv";
    
        /* make a connection to the database */
        //conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
        conn = PQconnectdb(conninfo);
    
        /* check to see that the backend connection was successfully made */
        if (PQstatus(conn) == CONNECTION_BAD)
        {
            fprintf(stderr, "Connection to database '%s' failed.
    ", dbName);
            fprintf(stderr, "%s", PQerrorMessage(conn));
            exit_nicely(conn);
        }
    
    #ifdef DEBUG
        debug = fopen("/tmp/trace.out", "w");
        PQtrace(conn, debug);
    #endif  /* DEBUG */
    
        /* start a transaction block */
        res = PQexec(conn, "BEGIN");
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            fprintf(stderr, "BEGIN command failed
    ");
            PQclear(res);
            exit_nicely(conn);
        }
    
        /*
        * should PQclear PGresult whenever it is no longer needed
        * so as to avoid memory leaks
        */
        PQclear(res);
    
        /*
        * fetch instances from the pg_database, the system catalog of
        * databases
        */
        res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
        {
            fprintf(stderr, "DECLARE CURSOR command failed
    ");
            PQclear(res);
            exit_nicely(conn);
        }
        PQclear(res);
    
        res = PQexec(conn, "FETCH ALL in myportal");
        if (PQresultStatus(res) != PGRES_TUPLES_OK)
        {
            fprintf(stderr, "FETCH ALL command didn't return tuples properly
    ");
            PQclear(res);
            exit_nicely(conn);
        }
    
        /* first, print out the attribute names */
        nFields = PQnfields(res);
        for (i = 0; i < nFields; i++)
            printf("%-15s", PQfname(res, i));
    
        printf("
    
    ");
    
        /* next, print out the instances */
        for (i = 0; i < PQntuples(res); i++)
        {
            for (j = 0; j < nFields; j++)
                printf("%-15s", PQgetvalue(res, i, j));
            printf("
    ");
        }
    
        PQclear(res);
    
        /* close the portal */
        res = PQexec(conn, "CLOSE myportal");
        PQclear(res);
    
        /* end the transaction */
        res = PQexec(conn, "END");
        PQclear(res);
    
        /* close the connection to the database and cleanup */
        PQfinish(conn);
    
    #ifdef DEBUG
        fclose(debug);
    #endif  /* DEBUG */
    
        return ;
    }
    --创建用户dbuser 
    --CREATE USER dbuser WITH PASSWORD 'password';
    
    --创建数据库
    --CREATE DATABASE exampledb OWNER dbuser;
    
    --将数据库的所有权限都赋予给用户dbuser
    --否则dbuser只能登陆,无法操作数据库
    --GRANT ALL PRIVILEGES ON DATABASE exampledb to dbuser;
    
    --数据库操作
    --CREATE TABLE user_tbl(name VARCHAR(20), signup_date DATE);
    
    --插入数据,注意字符串使用单引号,使用双引号会报错
    --INSERT INTO user_tbl(name, signup_date) VALUES('张三','2019-5-6');
    
    --选择记录,显示表的所有记录
    --SELECT * FROM user_tbl;
    
    --数据更新
    --UPDATE user_tbl set name = '李四' WHERE name = '张三';
    
    --删除数据
    --DELETE FROM user_tbl WHERE name = '李四';
    
    --添加栏位,新增列
    --ALTER TABLE user_tbl ADD email VARCHAR(40); 
    
    --更新结构,修改相关列的属性
    --ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL;
    
    --更改栏名
    --ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup;
    
    --删除栏
    --ALTER TABLE user_tbl DROP COLUMN email;
    
    --更改表名
    --ALTER TABLE user_tbl RENAME TO backup_tbl;
    
    SELECT * FROM backup_tbl;
  • 相关阅读:
    2019 安装SSDT
    添加删除服务
    POWER BI 报表服务器搭建配置
    解决POWER BI权限控制问题
    asp.net core 发布后显示异常错误信息的方法
    Microsoft SQL Server 查询连接数和关闭连接数
    不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况
    SQL Server在所有表中查找内容(在整个库搜索内容)
    IIS 在.NET MVC网站下创建多个子应用程序,子站访问根目录问题
    博客网址
  • 原文地址:https://www.cnblogs.com/luoyankuan/p/10813086.html
Copyright © 2011-2022 走看看