From 7f63f973141f973faff7896087697f5150332864 Mon Sep 17 00:00:00 2001 From: pans <pans@454eff88-639b-444f-9e54-f578c98de674> Date: 星期五, 30 十二月 2016 12:32:02 +0800 Subject: [PATCH] --- RtspFace/demo/src/db/main.cpp | 24 ++++++ RtspFace/demo/src/db/DBuntil.cpp | 64 ++++++++++++++++ RtspFace/demo/src/db/DBuntil.h | 37 +++++++++ RtspFace/demo/src/db/demo.cpp | 82 ++++++++++++++++++++ 4 files changed, 207 insertions(+), 0 deletions(-) diff --git a/RtspFace/demo/src/db/DBuntil.cpp b/RtspFace/demo/src/db/DBuntil.cpp new file mode 100644 index 0000000..3787225 --- /dev/null +++ b/RtspFace/demo/src/db/DBuntil.cpp @@ -0,0 +1,64 @@ +#include "DBuntil.h" +#include <mysql.h> +#include <cstdio> + +const char DBuntil::user[] = "root"; +const char DBuntil::pswd[] = ""; +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() +{ + mysql_init(&myCont); + mysql_real_connect(&myCont, host, user, pswd, db, port, NULL, 0); +} + +DBuntil::~DBuntil() +{ + if (result != NULL) + mysql_free_result(result); + + mysql_close(&myCont); +} + +person DBuntil::db_rearch(int f_id) +{ + 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);//查询 + if (!res) + { + result = mysql_store_result(&myCont); + if (result) + { + while (sql_row = mysql_fetch_row(result))//获取具体的数据 + { + per->p_id = atoi( sql_row[0]); + per->name = sql_row[1]; + } + } + } + else + { + //cout << "query sql failed!" << endl; + } + return per->p_id; +} diff --git a/RtspFace/demo/src/db/DBuntil.h b/RtspFace/demo/src/db/DBuntil.h new file mode 100644 index 0000000..f3ac9df --- /dev/null +++ b/RtspFace/demo/src/db/DBuntil.h @@ -0,0 +1,37 @@ +#ifndef _DBUNTIL_H_ +#define _DBUNTIL_H_ + +struct person +{ + int p_id; + char *name; + //图片 +}; + +class DBuntil +{ + +public: + DBuntil(); + ~DBuntil(); + person db_rearch(int f_id); + + int db_register(int f_id,person p); + +private: + + int db_add(); + int db_update(); + int db_select(int f_id,person *per); + + char *sql; + int res; + static const char user[]; + static const char pswd[]; + static const char host[]; + static const char db[]; + static unsigned int port; +}; + + +#endif \ No newline at end of file diff --git a/RtspFace/demo/src/db/demo.cpp b/RtspFace/demo/src/db/demo.cpp new file mode 100644 index 0000000..9daea23 --- /dev/null +++ b/RtspFace/demo/src/db/demo.cpp @@ -0,0 +1,82 @@ +#include <Windows.h> +#include <mysql.h> +#include <string> +#include <iostream> + +using namespace std; +#pragma comment(lib,"D:\\Program Files\\mysql-5.7.17-winx64\\lib\\libmysql.lib") + +int main() +{ + + const char user[] = "root"; + const char pswd[] = ""; + const char host[] = "localhost"; + const char db[] = "demo"; + unsigned int port = 3306; + MYSQL myCont; + MYSQL_RES *result; + MYSQL_ROW sql_row; + int res; + char *sql; + mysql_init(&myCont); + + + if (mysql_real_connect(&myCont, host, user, pswd, db, port, NULL, 0) != NULL) + { + sql="INSERT INTO user_info (NAME, img) VALUES ('c', null)"; + res = mysql_query(&myCont,sql);//查询 + if (res) + { + cout << "INSERT sql1 failed!" << endl; + }else + { + sql="INSERT INTO face_person (p_id, face_id) VALUES (LAST_INSERT_ID(), 2)"; + res = mysql_query(&myCont,sql); + if (res) + { + cout << "INSERT sql2 failed!" << endl; + } + } + + printf("MySQL client version: %s\n", mysql_get_client_info()); + sql="select a.pid,a.name,a.img,b.face_id from user_info a,face_person b where a.pid = b.p_id"; + mysql_query(&myCont, "SET NAMES gbk"); //设置编码格式 + res = mysql_query(&myCont,sql);//查询 + cout<<res<< endl; + if (!res) + { + result = mysql_store_result(&myCont); + if (result) + { + while (sql_row = mysql_fetch_row(result))//获取具体的数据 + { + cout<<" pid:" << sql_row[0] << endl; + cout<<" name:" << sql_row[1] << endl; + //cout<<" img:" << sql_row[2] << endl; + cout<<" faceid:" << sql_row[3] << endl; + cout<< endl; + } + } + } + else + { + cout << "query sql failed!" << endl; + } + + } + else + { + cout << "connect failed!" << endl; + } + + + + if (result != NULL) + mysql_free_result(result); + + mysql_close(&myCont); + system("pause"); + return 0; + +} \ No newline at end of file diff --git a/RtspFace/demo/src/db/main.cpp b/RtspFace/demo/src/db/main.cpp new file mode 100644 index 0000000..c6ec69e --- /dev/null +++ b/RtspFace/demo/src/db/main.cpp @@ -0,0 +1,24 @@ +#include <Windows.h> +#include <string> +#include <iostream> + +#include "DBuntil.h" + +using namespace std; + + +int main(int argc, char *argv[]) +{ + int i=i; + DBuntil dbu = DBuntil(); + person p={}; + p = dbu.db_rearch(i); + if(p.p_id != NULL) + { + cout<<p.p_id<<endl; + cout<<p.name<<endl; + }else + cout<<"person is null"<<endl; + system("pause"); + return 0; +} \ No newline at end of file -- Gitblit v1.8.0