学习笔记之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.\n", 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\n");
        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\n");
        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\n");
        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("\n\n");

    /* 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("\n");
    }

    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;

 

posted @ 2019-05-05 14:57  炎芯随笔  阅读(329)  评论(0编辑  收藏  举报