#include "DBuntil.h"
|
#include <mysql.h>
|
#include <cstdio>
|
#include <iostream>
|
|
|
MYSQL myCont;
|
MYSQL_RES *result;
|
MYSQL_ROW sql_row;
|
|
DBuntil::DBuntil(){}
|
|
DBuntil::DBuntil(my_db mydb)
|
{
|
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)
|
mysql_free_result(result);
|
|
mysql_close(&myCont);
|
}
|
|
*person DBuntil::db_rearch(int f_id)
|
{
|
person p= {0,""};
|
db_select(f_id,&p);
|
//对结构体赋值
|
return &p;
|
}
|
|
bool DBuntil::db_select(int f_id,person* per)
|
{
|
|
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))
|
{
|
//获取具体的数据
|
per->p_id = atoi( sql_row[0]);
|
per->name = sql_row[1];
|
}
|
}
|
}
|
else
|
{
|
std::cout<<"query sql failed!"<<std::endl;
|
return false;
|
}
|
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;
|
}
|