pans
2017-01-10 21d2e4cd4e3ec5ec6f7f32ed5426b069a303adfb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "DBuntil.h"
#include <mysql.h>
#include <cstdio>
#include <iostream>
 
DBuntil::DBuntil()
{
}
 
DBuntil::~DBuntil()
{
    if (result != NULL)
        mysql_free_result(result);
 
    mysql_close(&myCont);
}
 
bool DBuntil::db_init(my_db mydb)
{
    std::cout<<"db_init(my_db mydb) start"<<std::endl;
 
    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;
        std::cout<<"db_init(my_db mydb) end"<<std::endl;
        return true;
    }
    else
    {
        std::cout<<"mysql_real_connect failed"<<std::endl;
    }
    std::cout<<"db_init(my_db mydb) end"<<std::endl;
    return false;
}
 
bool DBuntil::db_search(person* per)
{
    std::cout<<"db_search start"<<std::endl;
    //db_init()
    if(db_select(per))
    {
        return true;
    }
    return false;
}
 
bool DBuntil::db_select(person* per)
{
    std::cout<<"db_select start"<<std::endl;
    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 " ,per->f_id );
    std::cout<<"==========start============"<<std::endl;
    std::cout<<"per->f_id="<<per->f_id<<std::endl;
    std::cout<<sql<<std::endl;
    std::cout<<"==========end=============="<<std::endl;
    if(!mysql_query(&myCont, "SET NAMES utf8"))//设置编码格式
    {
        std::cout<<"SET NAMES utf8"<<std::endl;
    }
    else
        std::cout<<"SET NAMES utf8 failed"<<std::endl;
    res = mysql_query(&myCont,sql);//查询
    if (!res)
    {
        result = mysql_store_result(&myCont);
        if (result != NULL)
        {
            while (sql_row = mysql_fetch_row(result))
            {
                //获取具体的数据
                per->p_id = atoi( sql_row[0]);
                per->name = sql_row[1];
 
                std::cout<<"per->p_id="<<per->p_id<<std::endl;
                std::cout<<"per->name="<<per->name<<std::endl;
                std::cout<<"per->f_id="<<per->f_id<<std::endl;
            }
        }
        else
            std::cout<<"result is null?"<<std::endl;
    }
    else
    {
        std::cout<<"query sql failed!"<<std::endl;
        return false;
    }
    return true;
}
 
bool DBuntil::db_add(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)" , per->f_id );
        res = mysql_query(&myCont,sql);
        if(!res)
        {
            return true;
        }
    }
    return false;
}
 
bool DBuntil::db_register(person *per)
{
    if(db_add(per))
    {
        return true;
    }
    return false;
}