目錄
一、準(zhǔn)備
二、讀取數(shù)據(jù)
三、修改數(shù)據(jù)
參考博客
一、準(zhǔn)備
mysql安裝及操作
首先在本地下載mysql數(shù)據(jù)庫,或者使用在線數(shù)據(jù)庫,在數(shù)據(jù)庫中創(chuàng)建一個(gè)表,往表里填入數(shù)據(jù),不然無法讀取到數(shù)據(jù)。運(yùn)行代碼前,將libmysql的靜態(tài)庫和動(dòng)態(tài)庫拷貝到代碼路徑,c++工程的包含路徑配置成mysql的include路徑。
二、讀取數(shù)據(jù)
?代碼需要修改的地方如下
#include <iostream>
#include <mysql.h>
#ifdef _DEBUG
#pragma comment(lib,"libmysql.lib")
#else
#pragma comment(lib,"libmysql.lib")
#endif
using namespace std;
int main()
{
MYSQL mysql;
mysql_init(&mysql); //初始化MYSQL變量
MYSQL_RES *result; //表數(shù)據(jù)存放結(jié)構(gòu)體
MYSQL_ROW row; //表的行數(shù)據(jù)
//主機(jī)IP 用戶名 密碼 數(shù)據(jù)庫名 端口 是都使用socket機(jī)制登陸 標(biāo)識(shí)位,默認(rèn)0
if (mysql_real_connect(&mysql, "127.0.0.1", "root", "614202", "newdata", 3306, NULL, 0)) {//連接到mysql
cout << "MySQL數(shù)據(jù)庫連接成功" << endl;
}
else {
cout << "數(shù)據(jù)庫連接失敗,請(qǐng)檢查參數(shù)信息\n";
}
if (!mysql_query(&mysql, "SELECT * FROM newtable")) //從表newtable中查數(shù)據(jù),若查詢成功返回0,失敗返回隨機(jī)數(shù)
{
cout << "查詢成功" << endl;
}
else {
cout << "查詢失敗\n";
}
result = mysql_store_result(&mysql); //將查詢到的結(jié)果集儲(chǔ)存到result中
int cloNum = mysql_num_fields(result); //返回結(jié)果集中的列數(shù)
int rowNum = mysql_num_rows(result); //返回結(jié)果集中的行數(shù)
std::cout <<"表的行列數(shù):"<< rowNum << " "<< cloNum << " \n";
while ((row = mysql_fetch_row(result))) //mysql_fetch_row 檢索結(jié)果集的下一行
{
for (int i = 0; i < cloNum; i++) //利用for循環(huán),輸出該行的每一列
{
cout << row[i] << "\t"; //row是MYSQL_ROW變量,可以當(dāng)做數(shù)組使用,i為列數(shù)
}
cout << endl;
}
mysql_free_result(result); //釋放結(jié)果集所占用的內(nèi)存
mysql_close(&mysql); //關(guān)閉與mysql的連接
return 0;
}
??文章來源:http://www.zghlxwxcb.cn/news/detail-607594.html
三、修改數(shù)據(jù)
#include <iostream>
#include <mysql.h>
#ifdef _DEBUG
#pragma comment(lib,"libmysql.lib")
#else
#pragma comment(lib,"libmysql.lib")
#endif
using namespace std;
int main()
{
MYSQL mysql;
mysql_init(&mysql); //初始化MYSQL變量
MYSQL_RES *result; //表數(shù)據(jù)存放結(jié)構(gòu)體
MYSQL_ROW row; //表的行數(shù)據(jù)
//主機(jī)IP 用戶名 密碼 數(shù)據(jù)庫名 端口 是都使用socket機(jī)制登陸 標(biāo)識(shí)位,默認(rèn)0
if (mysql_real_connect(&mysql, "127.0.0.1", "root", "614202", "newdata", 3306, NULL, 0)) {//連接到mysql
cout << "MySQL數(shù)據(jù)庫連接成功" << endl;
}
else {
cout << "數(shù)據(jù)庫連接失敗,請(qǐng)檢查參數(shù)信息\n";
}
if (!mysql_query(&mysql, "insert into newtable (id,name,tel) values(3,'jam',103)")) //向表newtable中插入數(shù)據(jù),若查詢成功返回0,失敗返回隨機(jī)數(shù)
{
cout << "插入成功" << endl;
}
else {
cout << "插入失敗\n";
}
if (!mysql_query(&mysql, "delete from newtable where id=3")) //從表newtable中刪除數(shù)據(jù),若查詢成功返回0,失敗返回隨機(jī)數(shù)
{
cout << "刪除成功" << endl;
}
else {
cout << "刪除失敗\n";
}
if (!mysql_query(&mysql, "update newtable set name='bob',tel=102 where id=2")) //更新表newtable中的數(shù)據(jù),修改id=2的數(shù)據(jù)
{
cout << "更新成功" << endl;
}
else {
cout << "更新失敗\n";
}
if (!mysql_query(&mysql, "select * from newtable")) //從表newtable中查數(shù)據(jù),若查詢成功返回0,失敗返回隨機(jī)數(shù)
{
cout << "查詢成功" << endl;
}
else {
cout << "查詢失敗\n";
}
result = mysql_store_result(&mysql); //將查詢到的結(jié)果集儲(chǔ)存到result中
int cloNum = mysql_num_fields(result); //返回結(jié)果集中的列數(shù)
int rowNum = mysql_num_rows(result); //返回結(jié)果集中的行數(shù)
std::cout <<"表的行列數(shù):"<< rowNum << " "<< cloNum << " \n";
while ((row = mysql_fetch_row(result))) //mysql_fetch_row 檢索結(jié)果集的下一行
{
for (int i = 0; i < cloNum; i++) //利用for循環(huán),輸出該行的每一列
{
cout << row[i] << "\t"; //row是MYSQL_ROW變量,可以當(dāng)做數(shù)組使用,i為列數(shù)
}
cout << endl;
}
mysql_free_result(result); //釋放結(jié)果集所占用的內(nèi)存
mysql_close(&mysql); //關(guān)閉與mysql的連接
return 0;
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-607594.html
到了這里,關(guān)于C++連接mysql數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!