c++连接mysql教程

Ubuntu c++ 连接mysql

 
 1 安装mysql数据库和C/C++ API
  直接在终端输入:sudo apt-get install mysql-server libmysql++-dev
  2 将lib文件拷到公用文件夹
  sudo cp /usr/lib/mysql/* /usr/lib/
  3 测试mysql
  vi test.cpp
  在文件中输入:
  #include<mysql/mysql.h>
  #include<iostream>
  using namespace std;
  int main()
  {
  MYSQL mysql;
  mysql_init(&mysql);
  cout<<"mysql is running"<<endl;
  return 0;
  }
g++ -lmysqlclient test.cpp -o test
运行:./test
一些常用操作

create table t1 (id int,name varchar(30));

t1表插入数据:
 test1.cpp
 #include <iostream>
 #include <mysql/mysql.h>
 #include <string>
 using namespace std;
 main() 
 {
   MYSQL mysql;
   mysql_init(&mysql);
   mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
  string sql = "insert into t1 (id, name) values (1, 'java1');";
  mysql_query(&mysql, sql.c_str());
  mysql_close(&mysql);
 }

g++ -lmysqlclient test1.cpp -o test1

./test1

 

更新mysql数据;
test2.cpp
 #include <iostream>
 #include <mysql/mysql.h>
 #include <string>
 using namespace std;
 main()
 {
   MYSQL mysql;
   mysql_init(&mysql);
   mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
   string sql = "insert into t1 (id,name) values (2, 'java2'),(3, 'java3');";
   mysql_query(&mysql, sql.c_str());
   sql = "update t1 set name = 'java33' where id = 3;";
   mysql_query(&mysql, sql.c_str());
   mysql_close(&mysql);
 }

g++ -lmysqlclient test2.cpp -o test2

./test2

 


 mysql 存储过程:
 mysql>delimiter //
    >create procedure p01()
    >begin
    >insert into t1 (id,name) values (66, 'java66');
    >end;
    >//
应用存储过程:

vi test3.cpp
#include <stream>
#include <mysql/mysql.h>
#include <string>

using namespace std;

main()
{
  MYSQL mysql;
  mysql_init(&mysql);
  mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
  string sql = "call p01();";
  mysql_query(&mysql, sql.c_str());
  mysql_close(&mysql);
}

 

g++ -lmysqlclient test3.cpp -o test3

./test3

 

delimiter; 返回mysql ;结束

触发器:
新建t2表:
mysql>delimiter //
   >create trigger tr1 after insert on t1 for each row
   >begin
   >insert into t2 (id,name) values (new.id,new.name);
   >end;
   >//
   >delete from t1 where id = 66;
   >//
   >delimeter ;

 vi test4.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
  MYSQL mysql;
  mysql_init(&mysql);
  mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
  string sql = "call p01();";
  mysql_query(&mysql, sql.c_str());
  mysql_close(&mysql);
}

 

g++ -lmysqlclient test4.cpp -o test4

./test4

mysql>delimeter ;

 

   >select * from t1;
   >select * from t2;
   >存在数据(66, 'java66')


查看表中数据的总数:

vi test5.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
  MYSQL mysql;
  MYSQL_RES *result = NULL;
  mysql_init(&mysql);
  mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
  string sql = "select id,name from t1;";
  mysql_query(&mysql, sql.c_str());
  result = mysql_store_result(&mysql);
  int rowcount = mysql_num_rows(result);
  cout << rowcount << endl;
  mysql_close(&mysql);
}

 

g++ -lmysqlclient test5.cpp -o test5

./test5

 

查处字段的总数:

vi test6.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
  MYSQL mysql;
  MYSQL_RES *result = NULL;
  mysql_init(&mysql);
  mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
  string str = "select id,name from t1;";
  mysql_query(&mysql, sql.c_str());
  result = mysql_store_result(&mysql);
  int rowcount = mysql_num_rows(result);
  cout << rowcount << endl;
  int fieldcount = mysql_num_fields(result);
  cout << fieldcount << endl;
  mysql_close(&mysql);
}

 

g++ -lmysqlclient test6.cpp -o test6

./test6

 

列出具体字段名:

vi test7.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
  MYSQL mysql;
  MYSQL_RES *result = NULL;
  MYSQL_FIELD *field = NULL;
  mysql_init(&mysql);
  mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
  string str = "select id,name from t1;";
  mysql_query(&mysql, sql.c_str());
  result = mysql_store_result(&mysql);
  int rowcount = mysql_num_rows(result);
  cout << rowcount << endl;
  int fieldcount = mysql_num_fields(result);
  cout << fieldcount << endl;
  for(int i = 0; i < fieldcount; i++)
  {
   field = mysql_fetch_field_direct(result,i);
   cout << field->name << "\t\t";
  }
  cout << endl;
  mysql_close(&mysql);
}

 g++ -lmysqlclient test7.cpp -o test7

./test7


显示表中所有数据:

vi test8.cpp
#include <iostream>
#include <string>
#include <mysql/mysql.h>

using namespace std;

main()
{
  MYSQL mysql;
  MYSQL_RES *result = NULL;
  MYSQL_FIELD *field = NULL;
  mysql_init(&mysql);
  mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
  string str = "select id,name from t1;";
  mysql_query(&mysql, sql.c_str());
  result = mysql_store_result(&mysql);
  int rowcount = mysql_num_rows(result);
  cout << rowcount << endl;
  int fieldcount = mysql_num_fields(result);
  cout << fieldcount << endl;
  for(int i = 0; i < fieldcount; i++)
  {
   field = mysql_fetch_field_direct(result,i);
   cout << field->name << "\t\t";
  }
  cout << endl;
  MYSQL_ROW row = NULL;
  row = mysql_fetch_row(result);
  while(NULL != row)
  {
   for(int i=1; i<fieldcount; i++)
   {
    cout << row[i] << "\t\t";
   }
   cout << endl;
   row = mysql_fetch_row(result);
  }
  mysql_close(&mysql);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值