- 1、mysql是怎麼實現的
- 2、MySQL數據庫管理工具有哪些
- 3、實例講解如何使用C++操作MySQL數據庫類
MySQL是一個關係型數據庫管理系統,由瑞典MySQL AB 公司開發,目前屬於 Oracle 旗下產品。MySQL 是最流行的關係型數據庫管理系統之一,在 WEB 應用方面,MySQL是最好的 RDBMS (Relational Database Management System,關係數據庫管理系統) 應用軟件。
MySQL是一種關係數據庫管理系統,關係數據庫將數據保存在不同的表中,而不是將所有數據放在一個大倉庫內,這樣就增加了速度並提高了靈活性。
MySQL所使用的 SQL 語言是用於訪問數據庫的最常用標準化語言。MySQL 軟件採用了雙授權政策,分為社區版和商業版,由於其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,一般中小型網站的開發都選擇 MySQL 作為網站數據庫。
由於其社區版的性能卓越,搭配 PHP 和 Apache 可組成良好的開發環境。
由於MySQL是開放源碼軟件,對於一般的個人使用者和中小型企業來說,MySQL提供的功能綽綽有餘,可以大大降低開發成本。
Linux作為操作系統,Apache 或Nginx作為 Web 服務器,MySQL 作為數據庫,PHP/Perl/Python作為服務器端腳本解釋器。由於這四個軟件都是免費或開放源碼軟件(FLOSS),因此使用這種方式不用花一分錢(除開人工成本)就可以建立起一個穩定、免費的網站系統,被業界稱為“LAMP“或“LNMP”組合。
1、phpMyAdmin
phpMyAdmin是最常用的MySQL維護工具,是一個用PHP開發的基於Web方式架構在網站主機上的MySQL管理工具,支持中文,管理數據庫非常方便。不足之處在於對大數據庫的備份和恢復不方便。
2、MySQLDumper
MySQLDumper使用PHP開發的MySQL數據庫備份恢復程序,解決了使用PHP進行大數據庫備份和恢復的問題,數百兆的數據庫都可以方便的備份恢復,不用擔心網速太慢導致中間中斷的問題,非常方便易用。這個軟件是德國人開發的,還沒有中文語言包。
3、Navicat
Navicat是一個桌面版MySQL數據庫管理和開發工具。和微軟SQLServer的管理器很像,易學易用。Navicat使用圖形化的用戶界面,可以讓用戶使用和管理更為輕鬆。支持中文,有免費版本提供。
4、MySQL GUI Tools
MySQL GUI Tools是MySQL官方提供的圖形化管理工具,功能很強大,值得推薦,可惜的是沒有中文界面。
5、MySQL ODBC Connector
MySQL官方提供的ODBC接口程序,系統安裝了這個程序之後,就可以通過ODBC來訪問MySQL,這樣就可以實現SQLServer、Access和MySQL之間的數據轉換,還可以支持ASP訪問MySQL數據庫。
/* * project: * 通用模塊 ( 用 c++ 處理 mysql 數據庫類,像ADO ) * * description: * * 通過DataBase,RecordSet,Record,Field類,實現對mysql數據庫的操作 * 包括連接、修改、添加、刪除、查詢等等,像ADO一樣操作數據庫,使 * 用方便 * * ( the end of this file have one sample, * welcom to use… ) * * * file:zlb_mysql.h * * author: @ zlb * * time:2005-12-12 * * * –*/ #ifndef ZLB_MYSQL_H #define ZLB_MYSQL_H #include “mysql.h” #include iostream #include vector #include string using namespace std; namespace zlb_mysql{ /* * 字段操作 */ class Field { public : /* 字段名稱 */ vectorstring m_name; /* 字段類型 */ vectorenum_field_types m_type; public : Field(); ~Field(); /* 是否是數字 */ bool IsNum(int num); /* 是否是數字 */ bool IsNum(string num); /* 是否是日期 */ bool IsDate(int num); /* 是否是日期 */ bool IsDate(string num); /* 是否是字符 */ bool IsChar(int num); /* 是否是字符 */ bool IsChar(string num); /* 是否為二進制數據 */ bool IsBlob(int num); /* 是否為二進制數據 */ bool IsBlob(string num); /* 得到指定字段的序號 */ int GetField_NO(string field_name); }; /* * 1 單條記錄 * 2 [int ]操作 [“”]操作 */ class Record { public: /* 結果集 */ vectorstring m_rs; /* 字段信息 佔用4字節的內存 當記錄數很大是回產生性能問題 */ Field *m_field; public : Record(){}; Record(Field* m_f); ~Record(); void SetData(string value); /* [“”]操作 */ string operator[](string s); string operator[](int num); /* null值判斷 */ bool IsNull(int num); bool IsNull(string s); /* 用 value tab value 的形式 返回結果 */ string GetTabText(); }; /* * 1 記錄集合 * 2 [int ]操作 [“”]操作 * 3 表結構操作 * 4 數據的插入修改 */ class RecordSet { private : /* 記錄集 */ vectorRecord m_s; /* 游標位置*/ unsigned long pos; /* 記錄數 */ int m_recordcount; /* 字段數 */ int m_field_num; /* 字段信息 */ Field m_field; MYSQL_RES * res ; MYSQL_FIELD * fd ; MYSQL_ROW row; MYSQL* m_Data ; public : RecordSet(); RecordSet(MYSQL *hSQL); ~RecordSet(); /* 處理返回多行的查詢,返回影響的行數 */ int ExecuteSQL(const char *SQL); /* 得到記錄數目 */ int GetRecordCount(); /* 得到字段數目 */ int GetFieldNum(); /* 向下移動游標 */ long MoveNext(); /* 移動游標 */ long Move(long length); /* 移動游標到開始位置 */ bool MoveFirst(); /* 移動游標到結束位置 */ bool MoveLast(); /* 獲取當前游標位置 */ unsigned long GetCurrentPos()const; /* 獲取當前游標的對應字段數據 */ bool GetCurrentFieldValue(const char * sFieldName,char *sValue); bool GetCurrentFieldValue(const int iFieldNum,char *sValue); /* 獲取游標的對應字段數據 */ bool GetFieldValue(long index,const char * sFieldName,char *sValue); bool GetFieldValue(long index,int iFieldNum,char *sValue); /* 是否到達游標尾部 */ bool IsEof(); /* 返回字段 */ Field* GetField(); /* 返回字段名 */ const char * GetFieldName(int iNum); /* 返回字段類型 */ const int GetFieldType(char * sName); const int GetFieldType(int iNum); /* 返回指定序號的記錄 */ Record operator[](int num); }; /* * 1 負責數據庫的連接關閉 * 2 執行sql 語句(不返回結果) * 3 處理事務 */ class DataBase { public : DataBase(); ~DataBase(); private : /* msyql 連接句柄 */ MYSQL* m_Data; public : /* 返回句柄 */ MYSQL * GetMysql(); /* 連接數據庫 */ int Connect(string host, string user, string passwd, string db, unsigned int port, unsigned long client_flag); /* 關閉數據庫連接 */ void DisConnect(); /* 執行非返回結果查詢 */ int ExecQuery(string sql); /* 測試mysql服務器是否存活 */ int Ping(); /* 關閉mysql 服務器 */ int ShutDown(); /* 主要功能:重新啟動mysql 服務器 */ int ReBoot(); /* * 說明:事務支持InnoDB or BDB表類型 */ /* 主要功能:開始事務 */ int Start_Transaction(); /* 主要功能:提交事務 */ int Commit(); /* 主要功能:回滾事務 */ int Rollback(); /* 得到客戶信息 */ const char * Get_client_info(); /* 主要功能:得到客戶版本信息 */ const unsigned long Get_client_version(); /* 主要功能:得到主機信息 */ const char * Get_host_info(); /* 主要功能:得到服務器信息 */ const char * Get_server_info(); /*主要功能:得到服務器版本信息*/ const unsigned long Get_server_version(); /*主要功能:得到 當前連接的默認字符集*/ const char * Get_character_set_name(); /* 主要功能返回單值查詢 */ char * ExecQueryGetSingValue(string sql); /* 得到系統時間 */ const char * GetSysTime(); /* 建立新數據庫 */ int Create_db(string name); /* 刪除制定的數據庫*/ int Drop_db(string name); }; }; #endif //ZLB_MYSQL_H /* * project: * 通用模塊 ( 用 c++ 處理 mysql 數據庫類,像ADO ) * * description: * * 通過DataBase,RecordSet,Record,Field類,實現對mysql數據庫的操作 * 包括連接、修改、添加、刪除、查詢等等,像ADO一樣操作數據庫,使 * 用方便 * * ( the end of this file have one sample, * welcom to use… ) * * * file:zlb_mysql.cpp * * author: @ zlb * * time:2005-12-12 * * * –*/ #include “stdafx.h” #include “zlb_mysql.h” namespace zlb_mysql{ /* +++++++++++++++++++++++++++++++++++++++++++++++++++ */ /* * 字段操作 */ Field::Field(){} Field::~Field(){} /* * 是否是數字 */ bool Field::IsNum(int num) { if(IS_NUM(m_type[num])) return true; else return false; } /* * 是否是數字 */ bool Field::IsNum(string num) { if(IS_NUM(m_type[GetField_NO(num)])) return true; else return false; } /* * 是否是日期 */ bool Field::IsDate(int num) { if( FIELD_TYPE_DATE == m_type[num] || FIELD_TYPE_DATETIME == m_type[num] ) return true; else return false; } /* 是否是日期 */ bool Field::IsDate(string num) { int temp; temp=GetField_NO(num); if(FIELD_TYPE_DATE == m_type[temp] || FIELD_TYPE_DATETIME == m_type[temp] ) return true; else return false; } /* * 是否是字符 */ bool Field::IsChar(int num) { if(m_type[num]==FIELD_TYPE_STRING || m_type[num]==FIELD_TYPE_VAR_STRING || m_type[num]==FIELD_TYPE_CHAR ) return true; else return false; } /* * 是否是字符 */ bool Field::IsChar(string num) { int temp; temp=this-GetField_NO (num); if(m_type[temp]==FIELD_TYPE_STRING || m_type[temp]==FIELD_TYPE_VAR_STRING || m_type[temp]==FIELD_TYPE_CHAR ) return true; else return false; } /* * 是否為二進制數據 */ bool Field::IsBlob(int num) { if(IS_BLOB(m_type[num])) return true; else return false; } /* * 是否為二進制數據 */ bool Field::IsBlob(string num) { if(IS_BLOB(m_type[GetField_NO(num)])) return true; else return false; } /* * 得到指定字段的序號 */ int Field::GetField_NO(string field_name) { for(unsigned int i=0;im_name.size ();i++) { if(!m_name[i].compare (field_name)) return i; } return -1; } /*—————————————————–*/ /* +++++++++++++++++++++++++++++++++++++++++++++++++++ */ /* * 1 單條記錄 * 2 [int ]操作 [“”]操作 */ Record::Record(Field * m_f) { m_field =m_f; } Record::~Record(){}; void Record::SetData(string value) { m_rs.push_back (value); } /* [“”]操作 */ string Record::operator[](string s) { return m_rs[m_field-GetField_NO(s)]; } string Record::operator[](int num) { return m_rs[num]; } /* null值判斷 */ bool Record::IsNull(int num) { if(“” == m_rs[num].c_str ()) return true; else return false; } bool Record::IsNull(string s) { if(“” == m_rs[m_field-GetField_NO(s)].c_str()) return true; else return false; } /* 主要-功能:用 value tab value 的形式 返回結果 */ string Record::GetTabText() { string temp; for(unsigned int i=0 ;im_rs.size();i++) { temp+=m_rs[i]; if(im_rs.size ()-1) temp+=”\t”; } return temp; } /*—————————————————–*/ /* +++++++++++++++++++++++++++++++++++++++++++++++++++ */ /* * 1 記錄集合 * 2 [int ]操作 [“”]操作 * 3 表結構操作 * 4 數據的插入修改 */ RecordSet::RecordSet() { res = NULL; row = NULL; pos = 0; } RecordSet::RecordSet(MYSQL *hSQL) { res = NULL; row = NULL; m_Data = hSQL; pos = 0; } RecordSet::~RecordSet() { } /* * 處理返回多行的查詢,返回影響的行數 * 成功返回行數,失敗返回-1 */ int RecordSet::ExecuteSQL(const char *SQL) { if ( !mysql_real_query(m_Data,SQL,strlen(SQL))) { //保存查詢結果 res = mysql_store_result(m_Data ); //得到記錄數量 m_recordcount = (int)mysql_num_rows(res) ; //得到字段數量 m_field_num = mysql_num_fields(res) ; for (int x = 0 ; fd = mysql_fetch_field(res); x++) { m_field.m_name.push_back(fd-name); m_field.m_type.push_back(fd-type); } //保存所有數據 while (row = mysql_fetch_row(res)) { Record temp(m_field); for (int k = 0 ; k m_field_num ; k++ ) { if(row[k]==NULL||(!strlen(row[k]))) { temp.SetData (“”); } else { temp.SetData(row[k]); } } //添加新記錄 m_s.push_back (temp); } mysql_free_result(res ) ; return m_s.size(); } return -1; } /* * 向下移動游標 * 返回移動後的游標位置 */ long RecordSet::MoveNext() { return (++pos); } /* 移動游標 */ long RecordSet::Move(long length) { int l = pos + length; if(l0) { pos = 0; return 0; }else { if(l = m_s.size()) { pos = m_s.size()-1; return pos; }else { pos = l; return pos; } } } /* 移動游標到開始位置 */ bool RecordSet::MoveFirst() { pos = 0; return true; } /* 移動游標到結束位置 */ bool RecordSet::MoveLast() { pos = m_s.size()-1; return true; } /* 獲取當前游標位置 */ unsigned long RecordSet::GetCurrentPos()const { return pos; } /* 獲取當前游標的對應字段數據 */ bool RecordSet::GetCurrentFieldValue(const char * sFieldName, char *sValue) { strcpy(sValue,m_s[pos][sFieldName].c_str()); return true; } bool RecordSet::GetCurrentFieldValue(const int iFieldNum,char *sValue) { strcpy(sValue,m_s[pos][iFieldNum].c_str()); return true; } /* 獲取游標的對應字段數據 */ bool RecordSet::GetFieldValue(long index,const char * sFieldName, char *sValue) { strcpy(sValue,m_s[index][sFieldName].c_str()); return true; } bool RecordSet::GetFieldValue(long index,int iFieldNum,char *sValue) { strcpy(sValue,m_s[index][iFieldNum].c_str()); return true; } /* 是否到達游標尾部 */ bool RecordSet::IsEof() { return (pos == m_s.size())?true:false; } /* * 得到記錄數目 */ int RecordSet::GetRecordCount() { return m_recordcount; } /* * 得到字段數目 */ int RecordSet::GetFieldNum() { return m_field_num; } /* * 返回字段 */ Field * RecordSet::GetField() { return m_field; } /* 返回字段名 */ const char * RecordSet::GetFieldName(int iNum) { return m_field.m_name.at(iNum).c_str(); } /* 返回字段類型 */ const int RecordSet::GetFieldType(char * sName) { int i = m_field.GetField_NO(sName); return m_field.m_type.at(i); } const int RecordSet::GetFieldType(int iNum) { return m_field.m_type.at(iNum); } /* * 返回指定序號的記錄 */ Record RecordSet::operator[](int num) { return m_s[num]; } /* ————————————————– */ /* +++++++++++++++++++++++++++++++++++++++++++++++++++ */ /* * 1 負責數據庫的連接關閉 * 2 執行sql 語句(不返回結果) * 3 處理事務 */ DataBase::DataBase() { m_Data = NULL; } DataBase::~DataBase() { if(NULL != m_Data) { DisConnect(); } } /* 返回句柄 */ MYSQL * DataBase::GetMysql() { return m_Data; } /* * 主要功能:連接數據庫 * 參數說明: * 1 host 主機ip地址或者時主機名稱 * 2 user 用戶名 * 3 passwd 密碼 * 4 db 欲連接的數據庫名稱 * 5 port 端口號 * 6 uinx 嵌套字 * 7 client_flag 客戶連接參數 * 返回值: 0成功 -1 失敗 */ int DataBase::Connect(string host, string user, string passwd, string db, unsigned int port, unsigned long client_flag) { if((m_Data = mysql_init(NULL)) mysql_real_connect( m_Data, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(),port , NULL, client_flag)) { //選擇制定的數據庫失敗 if ( mysql_select_db( m_Data, db.c_str () ) 0 ) { mysql_close( m_Data) ; return -1 ; } } else { //初始化mysql結構失敗 mysql_close( m_Data ); return -1 ; } //成功 return 0; } /* * 關閉數據庫連接 */ void DataBase::DisConnect( ) { mysql_close(m_Data) ; } /* * 主要功能: 執行非返回結果查詢 * 參數:sql 待執行的查詢語句 * 返回值; n為成功 表示受到影響的行數 -1 為執行失敗 */ int DataBase::ExecQuery(string sql) { if(!mysql_real_query(m_Data,sql.c_str (),(unsigned long)sql.length()) ) { //得到受影響的行數 return (int)mysql_affected_rows(m_Data) ; } else { //執行查詢失敗 return -1; } } /* * 主要功能:測試mysql服務器是否存活 * 返回值:0 表示成功 -1 失敗 */ int DataBase::Ping() { if(!mysql_ping(m_Data)) return 0; else return -1; } /* * 主要功能:關閉mysql 服務器 * 返回值;0成功 -1 失敗 */ int DataBase::ShutDown() { if(!mysql_shutdown(m_Data,SHUTDOWN_DEFAULT)) return 0; else return -1; } /* * 主要功能:重新啟動mysql 服務器 * 返回值;0表示成功 -1 表示失敗 */ int DataBase::ReBoot() { if(!mysql_reload(m_Data)) return 0; else return -1; } /* * 說明:事務支持InnoDB or BDB表類型 */ /* * 主要功能:開始事務 */ int DataBase::Start_Transaction() { if(!mysql_real_query(m_Data, “START TRANSACTION” , (unsigned long)strlen(“START TRANSACTION”) )) { return 0; } else //執行查詢失敗 return -1; } /* * 主要功能:提交事務 * 返回值:0 表示成功 -1 表示失敗 */ int DataBase::Commit() { if(!mysql_real_query( m_Data, “COMMIT”, (unsigned long)strlen(“COMMIT”) ) ) { return 0; } else //執行查詢失敗 return -1; } /* * 主要功能:回滾事務 * 返回值:0 表示成功 -1 表示失敗 */ int DataBase::Rollback() { if(!mysql_real_query(m_Data, “ROLLBACK”, (unsigned long)strlen(“ROLLBACK”) ) ) return 0; else //執行查詢失敗 return -1; } /* 得到客戶信息 */ const char * DataBase::Get_client_info() { return mysql_get_client_info(); } /*主要功能:得到客戶版本信息*/ const unsigned long DataBase::Get_client_version() { return mysql_get_client_version(); } /* 主要功能:得到主機信息 */ const char * DataBase::Get_host_info() { return mysql_get_host_info(m_Data); } /* 主要功能:得到服務器信息 */ const char * DataBase::Get_server_info() { return mysql_get_server_info( m_Data ); } /* 主要功能:得到服務器版本信息 */ const unsigned long DataBase::Get_server_version() { return mysql_get_server_version(m_Data); } /*主要功能:得到 當前連接的默認字符集*/ const char * DataBase::Get_character_set_name() { return mysql_character_set_name(m_Data); } /* * 主要功能返回單值查詢 */ char * DataBase::ExecQueryGetSingValue(string sql) { MYSQL_RES * res; MYSQL_ROW row ; char *p = NULL; if(!mysql_real_query( m_Data, sql.c_str(),(unsigned long)sql.length())) { //保存查詢結果 res = mysql_store_result( m_Data ) ; row = mysql_fetch_row( res ) ; p = ((row[0]==NULL)||(!strlen(row[0])))?”-1″:row[0]; mysql_free_result( res ) ; } else //執行查詢失敗 p = “-1”; return p; } /* * 得到系統時間 */ const char * DataBase::GetSysTime() { return ExecQueryGetSingValue(“select now()”); } /* * 主要功能:建立新數據庫 * 參數:name 為新數據庫的名稱 * 返回:0成功 -1 失敗 */ int DataBase::Create_db(string name) { string temp ; temp=”CREATE DATABASE “; temp+=name; if(!mysql_real_query( m_Data,temp.c_str () , (unsigned long)temp.length ()) ) return 0; else //執行查詢失敗 return -1; } /* * 主要功能:刪除制定的數據庫 * 參數:name 為欲刪除數據庫的名稱 * 返回:0成功 -1 失敗 */ int DataBase::Drop_db(string name) { string temp ; temp=”DROP DATABASE “; temp+=name; if(!mysql_real_query( m_Data,temp.c_str () , (unsigned long)temp.length ()) ) return 0; else //執行查詢失敗 return -1; } /*—————————————————–*/ }; /* * 使用例子 */ #include “zlb_mysql.h” using namespace std; void main() { zlb_mysql::DataBase zlb; //連接數據庫 if(-1 == zlb.Connect(“localhost”/*本地數據庫,可以是遠程 ip*/, “root”/*用戶名*/,”apple”/*密碼*/, “test”/*數據庫名*/, 0,0/*兩個標誌,mysql文檔有說明,一般為0*/)) { std::cout”connect failed “std::endl; } else { std::cout”connect success”std::endl; } //通過返回的數據庫句柄,建立記錄急,你可以通過返回的這個句柄建立多個記錄急 zlb_mysql::RecordSet rs(zlb.GetMysql()); rs.ExecuteSQL(“select * from testtable”);//這個語句大家都知道是什麼意思了 coutrs.GetRecordCount()/*返回的總的記錄數*/endl; coutrs.GetFieldNum()/*返回的總的字段數*/endl; coutrs[0].GetTabText()/*返回第一條記錄,你也可以rs[1].GetTabText() 如果你有多條記錄, */ endl; /*實現遍列,也可以使用後面的遍列方式*/ for(int i=0;irs.GetRecordCount();++i) { for(int j =0;jrs.GetFieldNum();++j) coutrs[i][j]; coutendl; } zlb_mysql::Field *fd = rs.GetField();/*你可以通過這樣的方式,獲取字段的信息*/ coutfd-GetField_NO(“Password”)/*返回我表裡的 Password 字段的位置,不 是記錄的位置*/ endl; coutrs[0][“Password”]endl;/*輸出第0行第Password列的值*/ coutrs[0][fd-GetField_NO(“Password”)]endl;/*你也可以這樣*/ coutrs.GetFieldName(0)/*獲取字段的名字*/endl; coutrs.GetFieldType(“UserName”)/*獲取字段的類型,是mysql里定義的*/endl; coutrs.GetCurrentPos()/*獲取當前記錄的位置*/endl; char s[50]; rs.GetCurrentFieldValue(1,s);/*獲取當前記錄對應字段的值*/ coutsendl; coutrs.Move(1)endl;/*移動游標,正數往前 負數往後*/ coutrs.GetCurrentPos()endl; rs.GetCurrentFieldValue(1,s); coutsendl; rs.MoveFirst();/*移動游標到最前*/ while(!rs.IsEof()/*判斷是否到達游標尾,實現遍列*/) { rs.GetCurrentFieldValue(“UserName”,s); couts”\t”; rs.GetCurrentFieldValue(“Password”,s); couts”\t
“; rs.MoveNext(); } rs.GetFieldValue(0,”UserName”,s);/*獲取指定行 的記錄值*/ couts”\t”; rs.GetFieldValue(0,”Password”,s); couts”\t
“; }
原創文章,作者:NL90R,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/127299.html