C++Mysql汎用コード
9003 ワード
bool myUnit::ConnectDB()
{
MYSQL *mysql = NULL;
mysql = mysql_init(&m_mysql);
if (!mysql){
errorIntoMySQL();
return FALSE;
}
mysql_options(&m_mysql, MYSQL_SET_CHARSET_NAME, "gbk");
mysql = mysql_real_connect(&m_mysql, HOST, USER, PASSW, DBNAME, PORT, NULL, 0);
if (mysql == NULL){
errorIntoMySQL();
return FALSE;
}
SpeakString("Database connected successful");
return TRUE;
}
データベースの作成
// , , ,
bool myUnit::createDatabase(std::string& dbname){
std::string queryStr = "create database if not exists ";
queryStr += dbname;
if (0 == mysql_query(&m_mysql, queryStr.c_str())){
queryStr = "use ";
queryStr += dbname;
if (0 == mysql_query(&m_mysql, queryStr.c_str())){
return true;
}
}
errorIntoMySQL();
return false;
}
テーブルの作成
// , , sql
bool myUnit::createdbTable(const std::string& query){
if (0 == mysql_query(&m_mysql, query.c_str())){
return true;
}
errorIntoMySQL();
return false;
}
データベースの初期化時にライブラリとテーブルを作成
myUnit::myUnit()
, 、
std::string database ="facelib";
createDatabase(database);
std::string tableName = "face";
std::string tableSql = "create table " + tableName + " (" +
"name varchar(50) not null," +
"feature varchar(10240)," +
"pictureName varchar(1024)," +
"primary key (name)" +
"); ";
createdbTable(tableSql);
}
検索文、名前を入力して検索またはすべてselect
//
bool myUnit::SelectDB(bool b, std::string m_name, std::vector<:vector> > &data){
std::string query;
if (!b){
if (m_name.length() == 0){
AfxMessageBox(" !");
return FALSE;
}
query = "select * from `face` where name = '" + m_name + "';";
}
else{
query = "select * from `face`;";
}
//
if (0 != mysql_query(&m_mysql, query.c_str())){
errorIntoMySQL();
return false;
}
result = mysql_store_result(&m_mysql);
int row = mysql_num_rows(result);
int field = mysql_num_fields(result);
MYSQL_ROW line = NULL;
line = mysql_fetch_row(result);
std::string temp;
while (NULL != line){
std::vector<:string> linedata;
for (int i = 0; i
挿入文
bool myUnit::InsertDB(std::string m_name){
if (m_name.length() == 0){
SpeakString(" !");
return FALSE;
}
std::string str = "insert into `face` values('" + m_name + "','" + "NULL" + "','" + "NULL" + "');";
if (mysql_query(&m_mysql, str.c_str())){
errorIntoMySQL();
return FALSE;
}
return TRUE;
}
削除
//
bool myUnit::DeleteDB(std::string m_name){
//UpdateData(TRUE);
if (m_name.length() == 0){
SpeakString(" !");
return FALSE;
}
std::string query = "delete from `face` where name = '" + m_name + "';";
if (mysql_query(&m_mysql, query.c_str())){
errorIntoMySQL();
return FALSE;
}
return TRUE;
}
データの削除
//
bool myUnit::getDatafromDB(std::vector<:vector> >& data){
std::string queryStr = "select * from `face`;";
if (0 != mysql_query(&m_mysql, queryStr.c_str())){
errorIntoMySQL();
return false;
}
result = mysql_store_result(&m_mysql);
int row = mysql_num_rows(result);
int field = mysql_num_fields(result);
MYSQL_ROW line = NULL;
line = mysql_fetch_row(result);
std::string temp;
while (NULL != line){
std::vector<:string> linedata;
for (int i = 0; i
ヘッダファイル
#pragma once
//#include "face_tai_sdklib.h"
//#include
#include "speakInterface.h"
#include "resource.h"
#include
#include
#include
#include
#include
#define HOST "127.0.0.1"
#define USER "root"
#define PASSW "root"
#define DBNAME "facelib"
#define PORT 3306
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libmysql.lib")
// FaceLibManagerDlg
//#define pUnit ( &(myUnit::instance()) )
class myUnit {
public:
static myUnit& instance();
static bool MatchFeaturesFromMySQL(std::string strFile1, std::string& nameOfPeope, std::string& picName, std::string& addr,
std::string& id, std::string& tel, std::string& brith, std::string& gender, std::string& score, std::string& level, int& Ret);
static bool createDatabase(std::string& dbname);
static bool createdbTable(const std::string& query);
static void errorIntoMySQL();
static bool getDatafromDB(std::vector<:vector> >& data);
static bool ConnectDB();
static bool SelectDB(bool b, std::string m_name, std::vector<:vector> > &data);
static bool InsertDB(std::string m_name, std::string m_pictureName, std::string m_addr, std::string m_id, std::string m_tel,
std::string m_brith, std::string m_gender, std::string m_score, std::string m_level);
static bool InsertDB(std::string m_name, std::string m_pictureName);
static bool InsertDB(std::string m_name);
static bool DeleteDB(std::string m_name);
static void freeMSQL();
static void SavePicInMySQL(char* strFile1);
static void faceInit();
static void faceExit();
public:
static int errorNum;
static const char* errorInfo;
static MYSQL_RES *result;
static MYSQL m_mysql; //
static MYSQL_RES* m_res; //
static MYSQL_ROW m_row; //
static int hCtx;
static int m_nRet;
static std::vector<:vector> > m_vec;
static std::mutex m_mutex;
};
表の添削
#クエリー
selectはデータを調べる必要があります.データfromテーブル名//テーブル全体のデータをクエリーする必要があります.
select*fromテーブル名//テーブル全体を羅列
select*fromテーブル名where salary>5000(「条件クエリー」salary>50は5000以上の給与を支払うメンバーを表す)/テーブル全体が条件を満たすデータをクエリーします.複数の条件があり、各条件にandで接続できます.
select*fromテーブル名where列名like"%小%";//列に「小」の文字が入っているすべてのデータ情報をクエリーします.
select count(*)fromテーブル名//クエリーテーブル内のデータの合計数
select avg(カラム名)fromテーブル名where(条件)/条件を満たすカラム内のデータの平均値のクエリー
select max(カラム名)fromテーブル名where(条件)/クエリー条件を満たすカラム内のデータの最大値
select min(カラム名)fromテーブル名where(条件)/条件を満たすカラムのデータの最小値のクエリー
select sum(カラム名)fromテーブル名where(条件)/クエリー条件を満たすカラム内のデータの合計
#増加
対応挿入:INSERT into表名(列名、列名、列名、列名注意カンマ区切り)value('張為剣',2190.6,21198890007867);//テーブル名カラム名が1つずつ対応していますが、NULLを挿入して空を挿入するには、そのカラムが空であるかどうかを考慮する必要があります.一般的な主カラムは空ではありません.
順次挿入:insert into表名values(NULL、「張三」、「男」、20、「18889009876」);括弧内のデータは順次表に入れて、NULLで占拠することを入力しないで同様にこの列が空であるかどうかを考慮する必要があります
#変更
updateテーブル名set age=age+1;//このテーブルのすべてのageを1に追加します.
updateテーブル名set age(列名)=age+1 where id=7//条件修正、条件id=7を満たす行のage列を変更して1つ加算
updateテーブル名set name='張匯美'where id=4;//修正番号4のメンバーname列の名前は「張匯美」
updateテーブル名set age=90,name=CONCAT(name,'(老人)')where age>=80 and sex='女';//変更して増加し、条件を満たす年齢が80以上の女性のage列を90に変更し、名前の後に「老人」の文字列//CONCAT(str 1,str 2,...)を増加する.接続文字列接続str 1、str 2等に用いる
#削除#さくじょ
delete fromテーブル名;//テーブル内のすべてのデータを削除し、テーブルを削除しません.
delete fromテーブル名where id=8(条件を満たすデータを削除)/idが8の行全体を削除
テーブルに対する操作
表の変更:alter table文作成後の表の変更
1.列alter tableテーブル名add列列列列データ型[after挿入位置]を追加する.
表の最後にaddress:alter table students add address char(60)を追加します.
ageという名前の列の後に列birthday:alter table students add birthday date after ageを挿入します.
2.列alter table表名change列名称列新名新データ型を変更する.
表tel列をphone:alter table students change tel phone char(12)default"-";
name列のデータ型をchar(9):alter table students change name char(9)not nullに変更します.
3.列alter table表名drop列名を削除する.
age列の削除:alter table students drop age;
4.テーブルの名前の変更基本形式:alter tableテーブル名rename新しいテーブル名;
studentsテーブルの名前を変更するにはtemp:alter table students rename temp;
5.削除表基本形式:drop table表名;
studentsテーブルの削除:drop table students;
6.データベースdrop databaseデータベース名を削除する.
lcoaデータベースの削除:drop database lcoa;
≪データベース・データ型|Database Data Type|oem_src≫:数値タイプ
整数:tinyint(1バイト)、smallint(2バイト)、mediumint(3バイト)、int(4バイト)、bigint(8バイト)浮動小数点数:float、double、real、decimal日付と時間:date、time、datetime、timestamp、year
文字列タイプ文字列:char、varchar(柔軟な格納文字列)varchar:値長を1-2バイトで格納し、列長<=255は1バイトで保存し、その他の場合は2バイトで保存します.例えばvarchar(10)は11バイトの記憶領域を占有し、varchar(500)は502バイトの記憶領域を占有する.
テキスト:tinytext、text、mediumtext、longtext
バイナリ(画像、音楽などを格納するために使用可能):tinyblob、blob、mediumblob、longblob