linux c操作mysql_Linux C/C++ 操作MySQL

一、常用的基本操作

1.        登錄:mysql -h localhost -u root -p

-h:后面接的是主機名,表示你要連接到哪台主機的MySQL數據庫。localhost(127.0.0.1)也可以是SV 連接的主機IP.

-u:后面接的是用戶名,MySQL默認的就是root。

-p:指定需要使用密碼登陸MySQL數據庫,如果密碼為空,該參數可以省略。登陸密碼也可以直接輸在-p后。

2.       查看數據庫:show databases;

3.        創建數據庫:create database 數據庫名。

4.        進入數據庫:Use 數據庫名;

5.       創建表:create table myclass(表結構)

6.       查看表:describe 表名;

7.       插入數據:insert into myclass values("1230011","張三","20","1","山西");

8.       查看表中所有信息:select * from myclass

9.        查詢表中某一列的數據或者某幾列的數據:命令:select id,name from myclass;

10.     查詢特定條件的數據:select * from myclass where id="1230011";

11.     修改數據表中的數據:update myclass set name="張大仙" where id='1230011';

12.     刪除數據表中的數據: delete from myclass where name='張大仙';

13.     刪除數據表中的某一列:alter table myclass drop age

14.     修改列名:alter table myclass change name students_name char(20) not null;

15.     數據表中插入列:alter table myclass add age char(10) not null ;

16.    刪除表table1:drop table if exists table1;

17.    備份數據庫testdb:mysqldump -h 192.168.3.143 -u root -p pwd -x --default-character-set=gbk >C:\testdb.sql

18.    刪除數據庫testdb:drop database testdb;

19.    恢復testdb數據庫:mysql -u root -pleizhimin testdb

二、常用類型介紹

1. MYSQL

用於定義一個mysql對象,便於后續操作確定要操作的數據庫是哪一個。

MYSQL mysql;  //mysql標記對應某個數據庫1

2. MYSQL_ROW

用於定義一個行對象,其內容含有一行的數據。

MYSQL_ROW row;  //row[i]可用於輸出該行第i個字段的數據1

3. MYSQL_FIELD

用於定義一個存儲字段信息的對象。

MYSQL_FIELD *field;  //field->name存儲對應字段名稱

4. MYSQL_RES

用於定義一個存儲數據庫檢索信息結果的對象。

MYSQL_RES *result;

三、常用函數介紹

其實在操作mysql數據庫時,最常使用的函數有以下幾個:

1. MYSQL * mysql_init()

用於初始化一個MYSQL對象,來連接mysql服務端。

MYSQL *mysql_init( MYSQL *mysql ); //成功返回MySQL結構指針,失敗返回NULL

//example

MYSQL mysql;

mysql_init( &mysql );12345

2. mysql_real_connect()

用於連接數據庫

MYSQL *mysql_real_connect (

MYSQL *mysql,   //初始化的MYSQL對象,與mysql_init()對應

const char *host,   //主機地址

const char *user,   //用戶,例如:root

const char *passwd,   //數據庫的密碼

const char *db,   //要連接的數據庫,例如:student

unsigned int port,   //端口,可填0

const char *unix_socket,   //一般為NULL

unsigned long client_flag);  //一般為0

//成功返回MySQL結構指針,失敗返回NULL

//example

mysql_real_connect( &mysql, "localhost", "root",

"mypasswd", "student", 0, NULL, 0 );12345678910111213

3. mysql_query()

用於執行mysql命令。其參數應使用c風格字符串。

int  mysql_query( MYSQL *mysql, char * command );//成功返回0

//example

string command = "select * from info";

mysql_query( &mysql, command.c_str() );12345

4. mysql_store_result()

用於獲取mysql操作的檢索結果。

//成功返回MYSQL_RES指針,失敗返回NULL

MYSQL_RES *mysql_store_result(MYSQL *mysql);

//example

MYSQL_RES *result;

result = mysql_store_result( &mysql );12345

5. mysql_num_rows()

用於獲取結果集的行數。

mysql_num_rows( MYSQL_RES *result );1

6. mysql_num_fields()

用於獲取結果集的字段數。

mysql_num_fields( MYSQL_RES *result );

//example

int row_num;

row_num = mysq l_num_fields( result );12345

7. mysql_fetch_field()

用於獲取下一個字段的類型。

MYSQL_FIELD* mysql_fetch_field(MYSQL_RES *result);1

8. mysql_fetch_row()

從結果集中獲取下一行,結束返回NULL。

MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);

//example

MYSQL_ROW row;

while( row = mysql_fetch_row( result ), row != NULL ) {

for( int i = 0; i < num; i++ ) {

cout << row[i] << "\t\t";

}

cout << endl;

}12345678910

9. mysql_fetch_field_direct()

給定字段序號,返回字段類型,結束返回NULL。

MYSQL_FIELD* mysql_fetch_field_direct(MYSQL_RES *result, int i);

//example

int num = mysql_num_fields( result );  //返回字段個數

for( int i = 0; i < num; i++ ) {

field = mysql_fetch_field_direct( result, i );  //返回字段類型

cout << field->name << "\t\t";  //輸出字段名

}

10. mysql_close()

用於關閉連接。

mysql_close( MYSQL *mysql );

示例:

1 #include

2 #include

3 #include

4 #include

5 #include "mysql.h"

6 using namespacestd;7

8 classMysqlDB9 {10 private:11 MYSQL mysql;12 MYSQL_ROW row;13 MYSQL_RES *result;14 MYSQL_FIELD *field;15 public:16 MysqlDB()17 {18 if( mysql_init( &mysql ) ==NULL )19 {20 cout << "init error, line:" << __LINE__ <

31 if( !mysql_real_connect( &mysql, host.c_str(), user.c_str(), passwd.c_str(), database.c_str(), 0, NULL, 0) )32 {33 cout << "connect error, line:" << __LINE__ <

43 voidMysqlDB::add()44 {45 stringid, name, sex, birthday;46 do

47 {48 cout << "請輸入學生信息:\n";49 cin >> id >> name >> sex >>birthday;50 string sql = "insert into stu values('" + id + "', '" + name + "', '" + sex + "', '" + birthday + "');";51 cout << sql <>id;55 } while( id == "y");56 }57

58 voidMysqlDB::del()59 {60 stringid;61 do

62 {63 cout << "請輸入刪除學生信息的ID:\n";64 cin >>id;65 string sql = "delete from stu where id='" + id +"';";66 cout << sql <>id;70 } while( id == "y");71 }72

73 voidMysqlDB::update()74 {75 stringid, filed,value;76 do

77 {78 cout << "請輸入修改學生信息ID,字段,值:\n";79 cin >> id >> filed >>value;80 //update myclass set name="張大仙" where id='1230011';

81 string sql = "update stu set" + filed +"='" + value + "' where ID='" + id + "';";82 cout << sql <>id;86 } while( id == "y");87 }88

89 voidMysqlDB::print()90 {91 //string sql = "select * from info where name = '" + name + "';";//要有''

92 string sql = "select * from stu;";93 //成功返回0

94 mysql_query( &mysql, sql.c_str() );95 //獲取查詢查詢結果;成功返回result的指針,失敗返回NULL

96 result = mysql_store_result( &mysql );97 if( !result )98 {99 cout << "result error, line :" << __LINE__ <

103 intnum;104 num = mysql_num_fields( result ); //返回字段個數

105 for( int i = 0; i < num; i++)106 {107 field = mysql_fetch_field_direct( result, i ); //返回字段類型

108 cout << field->name << "\t\t"; //輸出字段名

109 }110 cout <

112 while( row = mysql_fetch_row( result ), row !=NULL )113 {114 for( int i = 0; i < num; i++)115 {116 cout << row[i] << "\t\t";117 }118 cout <

122 intmain()123 {124 MysqlDB db;125 db.connect( "localhost", "root", "cnp200@HW", "student");126 db.print();127

128 db.add();129 db.print();130

131 db.update();132 db.print();133

134 db.del();135 db.print();136

137 return 0;138 }

參考:

https://blog.csdn.net/liushall/article/details/81144963

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值