From 0bad0e6b94c566f63bb97661850899f679fd1d12 Mon Sep 17 00:00:00 2001
From: pans <pans@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期三, 04 一月 2017 16:06:48 +0800
Subject: [PATCH] 

---
 RtspFace/demo/src/db/DBuntil.cpp |   77 +++++++++++++++++++++++++++-----------
 1 files changed, 55 insertions(+), 22 deletions(-)

diff --git a/RtspFace/demo/src/db/DBuntil.cpp b/RtspFace/demo/src/db/DBuntil.cpp
index 0610b9d..8f9f108 100644
--- a/RtspFace/demo/src/db/DBuntil.cpp
+++ b/RtspFace/demo/src/db/DBuntil.cpp
@@ -1,27 +1,35 @@
 #include "DBuntil.h"
 #include <mysql.h>
 #include <cstdio>
+#include <iostream>
 
-const char DBuntil::user[] = "root";         
-const char DBuntil::pswd[] = "Basic@2017";        
-const char DBuntil::host[] = "localhost";    
-const char DBuntil::db[] = "demo";       
-unsigned int DBuntil::port = 3306; 
 
 MYSQL myCont;
 MYSQL_RES *result;
 MYSQL_ROW sql_row;
-#pragma comment(lib,"D:\\Program Files\\mysql-5.7.17-winx64\\lib\\libmysql.lib") 
 
-DBuntil::DBuntil()
+DBuntil::DBuntil(){}
+
+DBuntil::DBuntil(my_db mydb)
 {
-	mysql_init(&myCont);
-	mysql_real_connect(&myCont, host, user, pswd, db, port, NULL, 0);
+	if(mysql_init(&myCont)!=NULL)
+	{
+		std::cout<<"init succeed"<<std::endl;
+	}
+	else
+		std::cout<<"init failed"<<std::endl;
+
+	if(mysql_real_connect(&myCont, mydb.host, mydb.user, mydb.pswd, mydb.db, mydb.port, NULL, 0) != NULL)
+	{
+		std::cout<<"mysql_real_connect succeed"<<std::endl;
+	}
+	else
+		std::cout<<"mysql_real_connect failed"<<std::endl;
 }
 
 DBuntil::~DBuntil()
 {
-	if (result != NULL) 
+	if (result != NULL)
 		mysql_free_result(result);
 
 	mysql_close(&myCont);
@@ -29,28 +37,27 @@
 
 person DBuntil::db_rearch(int f_id)
 {
-	person p={0,""};
+	person p= {0,""};
 	db_select(f_id,&p);
-	//对结构体赋值
+	//瀵圭粨鏋勪綋璧嬪��
 	return p;
 }
 
 int DBuntil::db_select(int f_id,person* per)
 {
-	//sql="select a.p_id,b.`name`,b.img from face_person a,user_info b where a.face_id = " + f_id + " AND a.p_id = b.pid ";
-	char c_sql[1024];
-	sprintf( c_sql, "select a.p_id,b.`name`,b.img from face_person a,user_info b where a.face_id = %d AND a.p_id = b.pid " , f_id ); 
-	
-	//sprintf( sql, "select a.p_id,b.`name`,b.img from face_person a,user_info b where a.face_id = %d AND a.p_id = b.pid " , f_id ); 
-	mysql_query(&myCont, "SET NAMES gbk"); //设置编码格式
-	res = mysql_query(&myCont,c_sql);//查询
+
+	sprintf( sql, "select a.p_id,b.`name`,b.img from face_person a,user_info b where a.face_id = %d AND a.p_id = b.pid " , f_id );
+
+	mysql_query(&myCont, "SET NAMES utf8"); //璁剧疆缂栫爜鏍煎紡
+	res = mysql_query(&myCont,sql);//鏌ヨ
 	if (!res)
 	{
 		result = mysql_store_result(&myCont);
 		if (result)
 		{
-			while (sql_row = mysql_fetch_row(result))//获取具体的数据
+			while (sql_row = mysql_fetch_row(result))
 			{
+				//鑾峰彇鍏蜂綋鐨勬暟鎹�
 				per->p_id = atoi( sql_row[0]);
 				per->name = sql_row[1];
 			}
@@ -58,7 +65,33 @@
 	}
 	else
 	{
-		//cout << "query sql failed!" << endl;
+		std::cout<<"query sql failed!"<<std::endl;
+		return false;
 	}
-	return per->p_id;
+	return true;
 }
+
+bool DBuntil::db_add(int f_id,person *per)
+{
+	//
+	sprintf( sql, "INSERT INTO user_info(NAME, img) VALUES('%s', NULL)" , per->name );
+	res = mysql_query(&myCont,sql);
+	if(!res)
+	{
+		sprintf( sql, "INSERT INTO face_person(p_id, face_id) VALUES (LAST_INSERT_ID(), %d)" , f_id );
+		res = mysql_query(&myCont,sql);
+		if(!res)
+		{
+			return true;
+		}
+	}
+	return false;
+}
+
+bool DBuntil::db_register(int f_id,person *per)
+{
+	if(db_add(f_id,per)){
+		return true;
+	}
+	return false;
+}
\ No newline at end of file

--
Gitblit v1.8.0