pans
2016-12-30 903e6871a283e464942bb27541d452dc41eb6c06


git-svn-id: http://192.168.1.226/svn/proxy@65 454eff88-639b-444f-9e54-f578c98de674
2个文件已修改
90 ■■■■■ 已修改文件
RtspFace/demo/src/db/DBuntil.cpp 68 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
RtspFace/demo/src/db/DBuntil.h 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
RtspFace/demo/src/db/DBuntil.cpp
@@ -3,32 +3,29 @@
#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;
DBuntil::DBuntil()
DBuntil::() {}
DBuntil::DBuntil(my_db mydb)
{
    if(mysql_init(&myCont)!=NULL){
    if(mysql_init(&myCont)!=NULL) {
        std::cout<<"init succeed"<<std::endl;
    }else
    } else
        std::cout<<"init failed"<<std::endl;
    if(mysql_real_connect(&myCont, host, user, pswd, db, port, NULL, 0) != NULL){
    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
    } else
        std::cout<<"mysql_real_connect failed"<<std::endl;
}
DBuntil::~DBuntil()
{
    if (result != NULL)
    if (result != NULL)
        mysql_free_result(result);
    mysql_close(&myCont);
@@ -36,36 +33,45 @@
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);//查询
    if (!res)
    {
    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))//获取具体的数据
            {
        if (result) {
            while (sql_row = mysql_fetch_row(result)) { //获取具体的数据
                per->p_id = atoi( sql_row[0]);
                per->name = sql_row[1];
            }
        }
    }
    else
    {
    } else {
        std::cout<<"query sql failed!"<<std::endl;
        return false;
    }
    return per->p_id;
    return true;
}
int 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;
}
RtspFace/demo/src/db/DBuntil.h
@@ -1,18 +1,27 @@
#ifndef _DBUNTIL_H_
#define _DBUNTIL_H_
struct person
{
struct person {
    int p_id;
    char *name;
    int f_id;
    //ͼƬ
};
struct my_db {
    char user[];
    char pswd[];
    char host[];
    char db[];
    unsigned int port;
}
class DBuntil
{
public:
    DBuntil();
    DBuntil(my_db mydb);
    ~DBuntil();
    person db_rearch(int f_id);
@@ -20,17 +29,12 @@
private:
    int db_add();
    int db_add(int f_id,person *per);
    int db_update();
    int db_select(int f_id,person *per);
    char *sql;
    char sql[1024];
    int res;
    static const char user[];
    static const char pswd[];
    static const char host[];
    static const char db[];
    static unsigned int port;
};