C++MYSQL:创建表,数据更,删,改,查

使用mysql API自动创建表

当输入数据很大,达到千万级别的时候,插入、查找、更新都需要很大的消耗。把sql语句写在字符串中

	sql = "CREATE TABLE IF NOT EXISTS `t_image`  ( \
		`id` int AUTO_INCREMENT,\
		`name` varchar(1024),\
		`path` varchar(2046),\
		`size` int,\
		PRIMARY KEY(`id`)\
		) ";
	int re = mysql_query(&mysql, sql.c_str());//从字符串换成const char*
	if (re != 0)
	{
		cout << "mysql_query failed!" << mysql_error(&mysql) << endl;
	}
  • 注:在实际项目的表格创建中,最好要再加上三个字段

    state tinyint 		表的异常表示
    ctime timestamp		创建时间
    mtime timestamp		修改时间
    

    表格的创建例如:

    CREATE TABLE IF NOT EXISTS `student` (
    `id` int(11) unsigned  not null auto_INCREMENT COMMENT 'id'`name` varchar(11) not null default "" comment '',
    `state` tinyint not null default 0 comment '0-normal,1- delete',  --异常标识
    `ctime` timestamp not null default timestamp_current comment '',  --创建时间
    `mtime` timestamp not null default timestamp_currnte 			--修改时间
     on update timestamp_current comment '',
     primary key(`id`)
    )
    

插入数据

#include <string>
#include <sstream>		//字符串拼接

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

	stringstream ss;
	ss << "insert `t_image` (`name`,`path`,`size`)values('image";
	ss << i << ".jpg','d:/img/', 10240)";
	sql = ss.str();

	re = mysql_query(&mysql, sql.c_str());
	if (re == 0)
	{
		int count = mysql_affected_rows(&mysql);
		cout << "mysql_affected_rows " << count <<" id ="<< 		         
            mysql_insert_id(&mysql) << endl;
	}
	else
	{
		cout << "insert failed!" << mysql_error(&mysql) << endl;
	}
}
  • 有几个坑在这里

    1 首先是地址的时候习惯用"d:\img",但是C++字符串里’\'是转义字符,所以就变成了"\i"的形式,不能正常显示地址。如果使用d:\\img的话,\在mysql中又是有意义的字符,又会有问题,所以这里索性直接用正斜杠‘/‘

    2 字符串的拼接。用字符串流stringsteam来实现,需要包含头文件<sstream>

更新数据

sql = "update t_image set `name`='test3.png',size=2000 where id=1";
	re = mysql_query(&mysql, sql.c_str());
	if (re == 0)
	{
		int count = mysql_affected_rows(&mysql);
		cout << "update mysql_affected_rows " << count << endl;
	}
	else
	{
		cout << "update failed!" << mysql_error(&mysql) << endl;
	}
//-----------------------------------------------------------------
	//根据map自动生成 update sql语句
	map<string, string> kv;
	kv.insert(make_pair("name", "image_update001.png"));
	kv.insert(make_pair("size", "5000"));
	string where = " where id=2 ";
	string tmp = "";
	for (auto ptr = kv.begin(); ptr != kv.end(); ptr++)
	{
		tmp += "`";
		tmp += ptr->first;
		tmp += "`='";
		tmp += ptr->second;
		tmp += "',";
	}
	tmp += " id=id ";
	sql = "update t_image set ";
	sql += tmp;
	sql += where;

	re = mysql_query(&mysql, sql.c_str());
	if (re == 0)
	{
		int count = mysql_affected_rows(&mysql);
		cout << "update mysql_affected_rows " << count << endl;
	}
	else
	{
		cout << "update failed!" << mysql_error(&mysql) << endl;
	}

后边根据map自动生成需要字符串的拼接,如果涉及整型的话可以使用stringstream来做

删除数据

truncate Table清空表单,并且自增索引重新从0开始

delete from table并不会真正删除,不会清除自增,在事务里面是在聚簇索引里面做一个辨识。实际清理的话需要跟上语句:OPTIMIZE TABLE table_name

delete的速度比较慢,在数据比较大的时候会进行分表,然后可能删除整张表的速度要比delete的速度来得快。

	sql = "delete from t_image where id=12";
	re = mysql_query(&mysql, sql.c_str());
	if (re == 0)
	{
		int count = mysql_affected_rows(&mysql);
		cout << "delete mysql_affected_rows " << count << endl;
	}
	else
	{
		cout << "delete failed!" << mysql_error(&mysql) << endl;
	}


	//delete 不会实际删除空间,只做了标识
	sql = "delete from t_image";
	re = mysql_query(&mysql, sql.c_str());
	if (re == 0)
	{
		int count = mysql_affected_rows(&mysql);
		cout << "delete mysql_affected_rows " << count << endl;
	}
	else
	{
		cout << "delete failed!" << mysql_error(&mysql) << endl;
	}

	//实际清理了空间
	sql = "OPTIMIZE TABLE t_image";
	re = mysql_query(&mysql, sql.c_str());
  • delete不会清空空间,空间的整理还需要OPTIMIZE TABLE

  • 上面的操作不能清空自增索引,后续插入会在之前的id上面自增。如果想重置自增索引,暴力的话可以在一开始就使用truncate table

  • 有时候如果delete数据之后,想使得id正常排序。例如删除一些列数据之后,剩下来的id为“1,3,5”,想让他变回“1,2,3” 可以使用以下指令

    SET @i=0; 
    UPDATE `user` SET `id`=(@i:=@i+1); 
    ALTER TABLE `user` AUTO_INCREMENT=0; 
    

    注意事项:其中的ID是否被其他表引用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值