C++连接Postgresql数据库进行数据预筛选(1)


/*
 * PreSelect.cpp
 *
 *  Created on: Mar 14, 2018
 *      Author: zlf
 */

#include <iostream>
#include <pqxx/pqxx>
#include <time.h>

#include <vector>
#include <string>
#include <string.h>
#include <sstream>
#include <iostream>

using namespace std;
using namespace pqxx;


//=======================================================//
//=======================================================//
int main(int argc, char* argv[]) {

	char * sql_delete0;
	char * sql_delete1;
	char * sql_delete2;

	char * sql0;
	char * sql1;
	char * sql2;
	char * sql3;
	char * sql4;
	char * sql5;

	try {
		connection C0(
				"dbname=hello user=postgres password=123 \
      hostaddr=127.0.0.1 port=5432");
		if (C0.is_open()) {
			cout << "Opened database successfully: " << C0.dbname() << endl;
		} else {
			cout << "Can't open database" << endl;
			return 1;
		}
		/* Create SQL statement */
		sql_delete0 = "drop table if exists diagnosis2";
		sql_delete1 = "drop table if exists diagnosis3";
		sql_delete2 = "drop table if exists diagnosis4";

		sql0 = "select * into diagnosis2 from diagnosis1 where icd is not null";

		sql1 =
				"select count(patient_id), patient_id into diagnosis3 from diagnosis2  group by patient_id having count(patient_id)>3";

		sql2 =
				"select * into diagnosis4 from diagnosis2 where patient_id in (select patient_id from diagnosis3) order by patient_id, time_begin asc";

		sql4 = "create table if not exists diagnosis5 ("
				"patient_id character varying,"
				"visit_number character varying,"
				"sex integer,"
				"time_begin timestamp without time zone,"
				"time_end timestamp without time zone,"
				"icd character varying,"
				"name character varying );";

		sql5 = "delete from diagnosis5";

		/* Create a non-transactional object. */
		nontransaction N(C0);

		/* Execute SQL query */
		result R0(N.exec(sql_delete0));
		result R1(N.exec(sql_delete1));
		result R2(N.exec(sql_delete2));
		result R3(N.exec(sql0));
		result R4(N.exec(sql1));
		result R5(N.exec(sql2));
		result R6(N.exec(sql4));
		result R7(N.exec(sql5));


		/* List down all the records */
		int num = 0;
		bool show = 0;
		if (show == 1) {

			for (result::const_iterator c = R5.begin(); c != R5.end(); ++c) {

				cout << "PatientID = " << c[0].as<string>() << "  ";
				cout << "VisitNumber = " << c[1].as<string>() << "  ";
				cout << "Sex = " << c[2].as<int>() << "  ";
				cout << "time_begin = " << c[3].as<string>() << "  ";
				cout << "time_end = " << c[4].as<string>() << "  ";
				cout << "ICD = " << c[5].as<string>() << "  ";
				cout << "name = " << c[6].as<string>() << "  ";
				num = num + 1;
				cout << "num= " << num << endl;
				if (num > 5000) {
					break;
				}
			}

		}

		cout << "Operation done successfully" << endl;
		C0.disconnect();
	} catch (const std::exception &e) {
		cerr << e.what() << std::endl;
		return 1;
	}

	return 0;
}

/*

 g++ $(pkg-config --libs libpqxx) PreSelect.cpp -lpqxx -lpq -o PreSelect

 */





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值