[C++]MySQL数据库操作实例
生活随笔
收集整理的這篇文章主要介紹了
[C++]MySQL数据库操作实例
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
由于課程大實(shí)驗(yàn)需要使用c++操作MySQL數(shù)據(jù)庫(kù),經(jīng)過(guò)一番研究終于成功實(shí)現(xiàn)VS2008中與MySQL的連接。?
環(huán)境設(shè)置:安裝完MySQL之后,將安裝目錄中的include目錄下的libmysql.lib文件拷到VS2008安裝目錄中的VC\lib\下,然后在?項(xiàng)目-選項(xiàng)-c/c++-常規(guī)?中的附加包含目錄以及?鏈接器-常規(guī)?中的附加庫(kù)目錄中加入“c:\MySQL\include\”,并且在?鏈接器-輸入?中的附加依賴(lài)項(xiàng)內(nèi)添加“libmysql.lib”,這樣即可使編譯器找到mysql.h頭文件,并可在程序中使用c語(yǔ)言的mysql API來(lái)操作數(shù)據(jù)庫(kù)。(如果MySQL安裝目錄中無(wú)include目錄,可到MySQL官網(wǎng)下載并安裝MySQL connector for C,并修改include目錄路徑)#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <iostream>
using namespace std;
int main()
{
????????const char user[] = "root";???????????????? //username
????????const char pswd[] = "root";???????????????? //password
????????const char host[] = "localhost";????????//or"127.0.0.1"
????????const char table[] = "test";????????????????//database
????????unsigned int port = 3306;???????????????????? //server port????????????????
????????MYSQL myCont;
????????MYSQL_RES *result;
????????MYSQL_ROW sql_row;
????????MYSQL_FIELD *fd;
????????char column[32][32];
????????int res;
????????mysql_init(&myCont);
????????if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))
????????{
????????????????cout<<"connect succeed!"<<endl;
????????????????mysql_query(&myCont, "SET NAMES GBK"); //設(shè)置編碼格式,否則在cmd下無(wú)法顯示中文
????????????????res=mysql_query(&myCont,"select * from samples");//查詢(xún)
????????????????if(!res)
????????????????{
????????????????????????result=mysql_store_result(&myCont);//保存查詢(xún)到的數(shù)據(jù)到result
????????????????????????if(result)
????????????????????????{
????????????????????????????????int i,j;
????????????????????????????????cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl;
????????????????????????????????for(i=0;fd=mysql_fetch_field(result);i++)//獲取列名
????????????????????????????????{
????????????????????????????????????????strcpy(column[i],fd->name);
????????????????????????????????}
????????????????????????????????j=mysql_num_fields(result);
????????????????????????????????for(i=0;i<j;i++)
????????????????????????????????{
????????????????????????????????????????printf("%s\t",column[i]);
????????????????????????????????}
????????????????????????????????printf("\n");
????????????????????????????????while(sql_row=mysql_fetch_row(result))//獲取具體的數(shù)據(jù)
????????????????????????????????{
????????????????????????????????????????for(i=0;i<j;i++)
????????????????????????????????????????{
????????????????????????????????????????????????printf("%s\n",sql_row[i]);
????????????????????????????????????????}
????????????????????????????????????????printf("\n");
????????????????????????????????}
????????????????????????}
????????????????}
????????????????else
????????????????{
????????????????????????cout<<"query sql failed!"<<endl;
????????????????}
????????}
????????else
????????{
????????????????cout<<"connect failed!"<<endl;
????????}
????????if(result!=NULL) mysql_free_result(result);//釋放結(jié)果資源
????????mysql_close(&myCont);//斷開(kāi)連接
????????return 0;
}使用總結(jié):?
1.#include<mysql.h>之前一定要加上#include<windows.h>否則會(huì)產(chǎn)生如下錯(cuò)誤:>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(191) : error C2146: 語(yǔ)法錯(cuò)誤 : 缺少“;”(在標(biāo)識(shí)符“fd”的前面)
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(191) : error C4430: 缺少類(lèi)型說(shuō)明符 - 假定為 int。注意: C++ 不支持默認(rèn) int
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(191) : error C4430: 缺少類(lèi)型說(shuō)明符 - 假定為 int。注意: C++ 不支持默認(rèn) int
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(366) : error C2065: “SOCKET”: 未聲明的標(biāo)識(shí)符
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(366) : error C2146: 語(yǔ)法錯(cuò)誤 : 缺少“)”(在標(biāo)識(shí)符“s”的前面)
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(367) : error C2059: 語(yǔ)法錯(cuò)誤 : “)”
2.總結(jié)一下常用MySQL命令: 測(cè)試環(huán)境:MySQL 5.1.35
安裝MySQL之后,打開(kāi)MySQL Command Line Client,輸入root密碼,即可操作數(shù)據(jù)庫(kù)
//查看MySQL版本
mysql> select version();
//顯示所有數(shù)據(jù)庫(kù)
mysql> show databases;
//使用數(shù)據(jù)庫(kù)
mysql> use database_name;
//顯示所有數(shù)據(jù)表
mysql> show tables;
//顯示數(shù)據(jù)表結(jié)構(gòu)
mysql> describe table_name;
//創(chuàng)建數(shù)據(jù)庫(kù)
mysql> create database database_name;
//刪除數(shù)據(jù)庫(kù)
mysql> drop database database_name;
//創(chuàng)建數(shù)據(jù)表
mysql> use database_name;
mysql> create table table_name (字段名 VARCHAR(20), 字段名 CHAR(1));
//刪除數(shù)據(jù)表
mysql> drop table table_name;
//查詢(xún)記錄
mysql> select * from table_name;
//導(dǎo)入.sql文件
mysql> use database_name;
mysql> source c:/mysql.sql
//修改root密碼
mysql> UPDATE mysql.user SET password=PASSWORD('新密碼') WHERE User='root';
//退出
mysql> quit
3,編譯出錯(cuò)解決:[root@localhost test]# g++ -o my mysql.cpp -l mysqlclientmysql.cpp: In function ‘int main(int, char**)’:mysql.cpp:7: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換mysql.cpp:8: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換mysql.cpp:9: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換/usr/bin/ld: cannot find -lmysqlclientcollect2: ld 返回 1
加入-L指定庫(kù)所在路徑:[root@localhost test]# g++ -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -o mysql mysql.cpp?mysql.cpp: In function ‘int main(int, char**)’:mysql.cpp:7: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換mysql.cpp:8: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換mysql.cpp:9: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換[root@localhost test]# lshelloworld ?helloworld.cpp ?mysql ?mysql.cpp
相關(guān)操作參考:http://andrew913.iteye.com/blog/433280原文:http://www.cnblogs.com/lovebread/archive/2009/11/24/1609936.html
環(huán)境設(shè)置:安裝完MySQL之后,將安裝目錄中的include目錄下的libmysql.lib文件拷到VS2008安裝目錄中的VC\lib\下,然后在?項(xiàng)目-選項(xiàng)-c/c++-常規(guī)?中的附加包含目錄以及?鏈接器-常規(guī)?中的附加庫(kù)目錄中加入“c:\MySQL\include\”,并且在?鏈接器-輸入?中的附加依賴(lài)項(xiàng)內(nèi)添加“libmysql.lib”,這樣即可使編譯器找到mysql.h頭文件,并可在程序中使用c語(yǔ)言的mysql API來(lái)操作數(shù)據(jù)庫(kù)。(如果MySQL安裝目錄中無(wú)include目錄,可到MySQL官網(wǎng)下載并安裝MySQL connector for C,并修改include目錄路徑)#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <iostream>
using namespace std;
int main()
{
????????const char user[] = "root";???????????????? //username
????????const char pswd[] = "root";???????????????? //password
????????const char host[] = "localhost";????????//or"127.0.0.1"
????????const char table[] = "test";????????????????//database
????????unsigned int port = 3306;???????????????????? //server port????????????????
????????MYSQL myCont;
????????MYSQL_RES *result;
????????MYSQL_ROW sql_row;
????????MYSQL_FIELD *fd;
????????char column[32][32];
????????int res;
????????mysql_init(&myCont);
????????if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))
????????{
????????????????cout<<"connect succeed!"<<endl;
????????????????mysql_query(&myCont, "SET NAMES GBK"); //設(shè)置編碼格式,否則在cmd下無(wú)法顯示中文
????????????????res=mysql_query(&myCont,"select * from samples");//查詢(xún)
????????????????if(!res)
????????????????{
????????????????????????result=mysql_store_result(&myCont);//保存查詢(xún)到的數(shù)據(jù)到result
????????????????????????if(result)
????????????????????????{
????????????????????????????????int i,j;
????????????????????????????????cout<<"number of result: "<<(unsigned long)mysql_num_rows(result)<<endl;
????????????????????????????????for(i=0;fd=mysql_fetch_field(result);i++)//獲取列名
????????????????????????????????{
????????????????????????????????????????strcpy(column[i],fd->name);
????????????????????????????????}
????????????????????????????????j=mysql_num_fields(result);
????????????????????????????????for(i=0;i<j;i++)
????????????????????????????????{
????????????????????????????????????????printf("%s\t",column[i]);
????????????????????????????????}
????????????????????????????????printf("\n");
????????????????????????????????while(sql_row=mysql_fetch_row(result))//獲取具體的數(shù)據(jù)
????????????????????????????????{
????????????????????????????????????????for(i=0;i<j;i++)
????????????????????????????????????????{
????????????????????????????????????????????????printf("%s\n",sql_row[i]);
????????????????????????????????????????}
????????????????????????????????????????printf("\n");
????????????????????????????????}
????????????????????????}
????????????????}
????????????????else
????????????????{
????????????????????????cout<<"query sql failed!"<<endl;
????????????????}
????????}
????????else
????????{
????????????????cout<<"connect failed!"<<endl;
????????}
????????if(result!=NULL) mysql_free_result(result);//釋放結(jié)果資源
????????mysql_close(&myCont);//斷開(kāi)連接
????????return 0;
}使用總結(jié):?
1.#include<mysql.h>之前一定要加上#include<windows.h>否則會(huì)產(chǎn)生如下錯(cuò)誤:>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(191) : error C2146: 語(yǔ)法錯(cuò)誤 : 缺少“;”(在標(biāo)識(shí)符“fd”的前面)
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(191) : error C4430: 缺少類(lèi)型說(shuō)明符 - 假定為 int。注意: C++ 不支持默認(rèn) int
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(191) : error C4430: 缺少類(lèi)型說(shuō)明符 - 假定為 int。注意: C++ 不支持默認(rèn) int
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(366) : error C2065: “SOCKET”: 未聲明的標(biāo)識(shí)符
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(366) : error C2146: 語(yǔ)法錯(cuò)誤 : 缺少“)”(在標(biāo)識(shí)符“s”的前面)
>d:\my documents\visual studio 2008\projects\testmysql\testmysql\mysql\mysql_com.h(367) : error C2059: 語(yǔ)法錯(cuò)誤 : “)”
2.總結(jié)一下常用MySQL命令: 測(cè)試環(huán)境:MySQL 5.1.35
安裝MySQL之后,打開(kāi)MySQL Command Line Client,輸入root密碼,即可操作數(shù)據(jù)庫(kù)
//查看MySQL版本
mysql> select version();
//顯示所有數(shù)據(jù)庫(kù)
mysql> show databases;
//使用數(shù)據(jù)庫(kù)
mysql> use database_name;
//顯示所有數(shù)據(jù)表
mysql> show tables;
//顯示數(shù)據(jù)表結(jié)構(gòu)
mysql> describe table_name;
//創(chuàng)建數(shù)據(jù)庫(kù)
mysql> create database database_name;
//刪除數(shù)據(jù)庫(kù)
mysql> drop database database_name;
//創(chuàng)建數(shù)據(jù)表
mysql> use database_name;
mysql> create table table_name (字段名 VARCHAR(20), 字段名 CHAR(1));
//刪除數(shù)據(jù)表
mysql> drop table table_name;
//查詢(xún)記錄
mysql> select * from table_name;
//導(dǎo)入.sql文件
mysql> use database_name;
mysql> source c:/mysql.sql
//修改root密碼
mysql> UPDATE mysql.user SET password=PASSWORD('新密碼') WHERE User='root';
//退出
mysql> quit
3,編譯出錯(cuò)解決:[root@localhost test]# g++ -o my mysql.cpp -l mysqlclientmysql.cpp: In function ‘int main(int, char**)’:mysql.cpp:7: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換mysql.cpp:8: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換mysql.cpp:9: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換/usr/bin/ld: cannot find -lmysqlclientcollect2: ld 返回 1
加入-L指定庫(kù)所在路徑:[root@localhost test]# g++ -I/usr/include/mysql -L/usr/lib64/mysql -lmysqlclient -o mysql mysql.cpp?mysql.cpp: In function ‘int main(int, char**)’:mysql.cpp:7: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換mysql.cpp:8: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換mysql.cpp:9: 警告:不建議使用從字符串常量到‘char*’的轉(zhuǎn)換[root@localhost test]# lshelloworld ?helloworld.cpp ?mysql ?mysql.cpp
相關(guān)操作參考:http://andrew913.iteye.com/blog/433280原文:http://www.cnblogs.com/lovebread/archive/2009/11/24/1609936.html
轉(zhuǎn)載于:https://blog.51cto.com/ohyeahbbs/881562
總結(jié)
以上是生活随笔為你收集整理的[C++]MySQL数据库操作实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何解决padding标记在ie7、ie
- 下一篇: 继续努力奋斗,生活会更美好