pans
2016-12-30 eb68a1e3ad5fb24b4c674b8f2fd529b8302c7d97
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
#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;
 
DBuntil::DBuntil()
{
    if(mysql_init(&myCont)!=NULL){
        std::cout<<"init succeed"<<std::endl;
    }else
        std::cout<<"init failed"<<std::endl;
    
    if(mysql_real_connect(&myCont, host, user, pswd, db, 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) 
        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
    {
        std::cout<<"query sql failed!"<<std::endl;
    }
    return per->p_id;
}