2、C程序开发

2.1、连接数据库

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "libpq-fe.h"
  4. int
  5. main(int argc, char **argv){
  6. const char *conninfo;
  7. PGconn *conn;
  8. if (argc > 1){
  9. conninfo = argv[1];
  10. }else{
  11. conninfo = "dbname = postgres";
  12. }
  13. conn = PQconnectdb(conninfo);
  14. if (PQstatus(conn) != CONNECTION_OK){
  15. fprintf(stderr, "连接数据库失败: %s",PQerrorMessage(conn));
  16. }else{
  17. printf("连接数据库成功!\n");
  18. }
  19. PQfinish(conn);
  20. return 0;
  21. }

编译

  1. gcc -c -I /usr/local/install/tbase_pgxz/include/ conn.c
  2. gcc -o conn conn.o -L /usr/local/install/tbase_pgxz/lib/ -lpq

运行

  1. ./conn "host=172.16.0.3 dbname=postgres port=11000"
  2. 连接数据库成功!
  1. ./conn "host=172.16.0.3 dbname=postgres port=15432 user=tbase"
  2. 连接数据库成功!

2.2、建立数据表

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "libpq-fe.h"
  4. int
  5. main(int argc, char **argv){
  6. const char *conninfo;
  7. PGconn *conn;
  8. PGresult *res;
  9. const char *sql = "create table tbase(id int,nickname text) distribute by shard(id) to group default_group";
  10. if (argc > 1){
  11. conninfo = argv[1];
  12. }else{
  13. conninfo = "dbname = postgres";
  14. }
  15. conn = PQconnectdb(conninfo);
  16. if (PQstatus(conn) != CONNECTION_OK){
  17. fprintf(stderr, "连接数据库失败: %s",PQerrorMessage(conn));
  18. }else{
  19. printf("连接数据库成功!\n");
  20. }
  21. res = PQexec(conn,sql);
  22. if(PQresultStatus(res) != PGRES_COMMAND_OK){
  23. fprintf(stderr, "建立数据表失败: %s",PQresultErrorMessage(res));
  24. }else{
  25. printf("建立数据表成功!\n");
  26. }
  27. PQclear(res);
  28. PQfinish(conn);
  29. return 0;
  30. }

编译

  1. gcc -c -I /usr/local/install/tbase_pgxz/include/ createtable.c
  2. gcc -o createtable createtable.o -L /usr/local/install/tbase_pgxz/lib/ -lpq

运行

  1. ./createtable "port=11000 dbname=postgres"
  2. 连接数据库成功!
  3. 建立数据表成功!

2.3、插入数据

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "libpq-fe.h"
  4. int
  5. main(int argc, char **argv){
  6. const char *conninfo;
  7. PGconn *conn;
  8. PGresult *res;
  9. const char *sql = "INSERT INTO tbase (id,nickname) values(1,'tbase'),(2,'pgxz')";
  10. if (argc > 1){
  11. conninfo = argv[1];
  12. }else{
  13. conninfo = "dbname = postgres";
  14. }
  15. conn = PQconnectdb(conninfo);
  16. if (PQstatus(conn) != CONNECTION_OK){
  17. fprintf(stderr, "连接数据库失败: %s",PQerrorMessage(conn));
  18. }else{
  19. printf("连接数据库成功!\n");
  20. }
  21. res = PQexec(conn,sql);
  22. if(PQresultStatus(res) != PGRES_COMMAND_OK){
  23. fprintf(stderr, "插入数据失败: %s",PQresultErrorMessage(res));
  24. }else{
  25. printf("插入数据成功!\n");
  26. }
  27. PQclear(res);
  28. PQfinish(conn);
  29. return 0;
  30. }

编译

  1. gcc -c -I /usr/local/install/tbase_pgxz/include/ insert.c
  2. gcc -o insert insert.o -L /usr/local/install/tbase_pgxz/lib/ -lpq

运行

  1. ./insert "dbname=postgres port=15432"

2.4、查询数据

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "libpq-fe.h"
  4. int
  5. main(int argc, char **argv){
  6. const char *conninfo;
  7. PGconn *conn;
  8. PGresult *res;
  9. const char *sql = "select * from tbase";
  10. if (argc > 1){
  11. conninfo = argv[1];
  12. }else{
  13. conninfo = "dbname = postgres";
  14. }
  15. conn = PQconnectdb(conninfo);
  16. if (PQstatus(conn) != CONNECTION_OK){
  17. fprintf(stderr, "连接数据库失败: %s",PQerrorMessage(conn));
  18. }else{
  19. printf("连接数据库成功!\n");
  20. }
  21. res = PQexec(conn,sql);
  22. if(PQresultStatus(res) != PGRES_TUPLES_OK){
  23. fprintf(stderr, "插入数据失败: %s",PQresultErrorMessage(res));
  24. }else{
  25. printf("查询数据成功!\n");
  26. int rownum = PQntuples(res) ;
  27. int colnum = PQnfields(res);
  28. for(int j = 0;j< colnum; ++j){
  29. printf("%s\t",PQfname(res,j));
  30. }
  31. printf("\n");
  32. for(int i = 0;i< rownum; ++i){
  33. for(int j = 0;j< colnum; ++j){
  34. printf("%s\t",PQgetvalue(res,i,j));
  35. }
  36. printf("\n");
  37. }
  38. }
  39. PQclear(res);
  40. PQfinish(conn);
  41. return 0;
  42. }

编译

  1. gcc -std=c99 -c -I /usr/local/install/tbase_pgxz/include/ select.c
  2. gcc -o select select.o -L /usr/local/install/tbase_pgxz/lib/ -lpq

运行

  1. ./select "dbname=postgres port=15432"
  2. 连接数据库成功!
  3. 查询数据成功!
  4. id nickname
  5. 1 tbase
  6. 2 pgxz

2.5、流数据COPY入表

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "libpq-fe.h"
  5. int
  6. main(int argc, char **argv){
  7. const char *conninfo;
  8. PGconn *conn;
  9. PGresult *res;
  10. const char *buffer = "1,tbase\n2,pgxz\n3,Tbase牛";
  11. if (argc > 1){
  12. conninfo = argv[1];
  13. }else{
  14. conninfo = "dbname = postgres";
  15. }
  16. conn = PQconnectdb(conninfo);
  17. if (PQstatus(conn) != CONNECTION_OK){
  18. fprintf(stderr, "连接数据库失败: %s",PQerrorMessage(conn));
  19. }else{
  20. printf("连接数据库成功!\n");
  21. }
  22. res=PQexec(conn,"COPY tbase FROM STDIN DELIMITER ',';");
  23. if(PQresultStatus(res) != PGRES_COPY_IN){
  24. fprintf(stderr, "copy数据出错1: %s",PQresultErrorMessage(res));
  25. }else{
  26. int len = strlen(buffer);
  27. if(PQputCopyData(conn,buffer,len) == 1){
  28. if(PQputCopyEnd(conn,NULL) == 1){
  29. res = PQgetResult(conn);
  30. if(PQresultStatus(res) == PGRES_COMMAND_OK){
  31. printf("copy数据成功!\n");
  32. }else{
  33. fprintf(stderr, "copy数据出错2: %s",PQerrorMessage(conn));
  34. }
  35. }else{
  36. fprintf(stderr, "copy数据出错3: %s",PQerrorMessage(conn));
  37. }
  38. }else{
  39. fprintf(stderr, "copy数据出错4: %s",PQerrorMessage(conn));
  40. }
  41. }
  42. PQclear(res);
  43. PQfinish(conn);
  44. return 0;
  45. }

编译

  1. gcc -c -I /usr/local/install/tbase_pgxz/include/ copy.c
  2. gcc -o copy copy.o -L /usr/local/install/tbase_pgxz/lib/ -lpq

执行

  1. ./copy "dbname=postgres port=15432"
  2. 连接数据库成功!
  3. copy数据成功!

3、shell脚本开发

  1. #!/bin/sh
  2. if [ $# -ne 0 ]
  3. then
  4. echo "usage: $0 exec_sql"
  5. exit 1
  6. fi
  7. exec_sql=$1
  8. masters=`psql -h 172.16.0.29 -d postgres -p 15432 -t -c "select string_agg(node_host, ' ') from (select * from pgxc_node where node_type = 'D' order by node_name) t"`
  9. port_list=`psql -h 172.16.0.29 -d postgres -p 15432 -t -c "select string_agg(node_port::text, ' ') from (select * from pgxc_node where node_type = 'D' order by node_name) t"`
  10. node_cnt=`psql -h 172.16.0.29 -d postgres -p 15432 -t -c "select count(*) from pgxc_node where node_type = 'D'"`
  11. masters=($masters)
  12. ports=($port_list)
  13. echo $node_cnt
  14. flag=0
  15. for((i=0;i<$node_cnt;i++));
  16. do
  17. seq=$(($i+1))
  18. master=${masters[$i]}
  19. port=${ports[$i]}
  20. echo $master
  21. echo $port
  22. psql -h $master -p $port postgres -c "$exec_sql"
  23. done