mysql out of memory needed 8164 bytes 等问题
2013-11-22 13:27
369 查看
c++ 连接mysql。
#include “mysql.h”
int main ()
{
MYSQL conn;
mysql_init(&conn);
if (mysql_real_connect(&conn,"172.16.66.1","xaf","111111","bai",0,NULL,0)
{
cout<<"success!"<<endl;
}
else
{
cerr<<"failed"<<endl;
if (mysql_errno(&conn))
{ cout<<mysql_errno(&conn)<<" "<<mysql_error(&conn);}
}
std::string sql="select * from ss ";
int sq=mysql_query(&conn,sql);
if (!sq){
res_ptr=mysql_store_result(&conn);
if(res_ptr)
{ while(sqlrow=mysql_fetch_row(res_ptr)){..} }
mysql_free_result(res_ptr); 这句话特别重要,
mysql_close(&conn);
}
g++ -g -c test.cpp
g++ -o test test.o -lmysqlclient
1.查询结果要释放。
以前我编程序只要能跑出来就好了,不需要考虑程序的持久性,但是自从遇到比较大的项目,数据量很大,要循环很多次查询数据库,程序甚至需要运行几天。这样我就遇到了这里所说的错误,mysql out of memory needed 8164 bytes. 而且我都不知到错在哪。
以后编程要知道收尾,每创建一个对象,每使用一次内存,想着是不是要在该释放的时候释放。
p2. errno:2003,error:can't connect to mysql on server "172..." (111) :
最终解决:因为我有多个线程,每个线程里创建MYSQL connect,建立连接,一开始没有对建立连接的语句加锁,后来加锁后,就可以了:
pthread_mutex_t m_mutex();
pthread_mutex_lock(&m_mutex);
mysql_init(&conn);
if (mysql_real_connect(&conn,"172.16.66.1","xaf","111111","bai",0,NULL,0)
{
cout<<"success!"<<endl;
}
else
{
cerr<<"failed"<<endl;
if (mysql_errno(&conn))
{ cout<<mysql_errno(&conn)<<" "<<mysql_error(&conn);}
}
pthread_mutex_unlock(&m_mutex);
p3. 多线程运行程序,如果不在每个线程函数结束加mysql_close(&conn) ;之前调试运行时,每次都没有释放连接,会出现error:too many connections.
#include “mysql.h”
int main ()
{
MYSQL conn;
mysql_init(&conn);
if (mysql_real_connect(&conn,"172.16.66.1","xaf","111111","bai",0,NULL,0)
{
cout<<"success!"<<endl;
}
else
{
cerr<<"failed"<<endl;
if (mysql_errno(&conn))
{ cout<<mysql_errno(&conn)<<" "<<mysql_error(&conn);}
}
std::string sql="select * from ss ";
int sq=mysql_query(&conn,sql);
if (!sq){
res_ptr=mysql_store_result(&conn);
if(res_ptr)
{ while(sqlrow=mysql_fetch_row(res_ptr)){..} }
mysql_free_result(res_ptr); 这句话特别重要,
mysql_close(&conn);
}
g++ -g -c test.cpp
g++ -o test test.o -lmysqlclient
1.查询结果要释放。
以前我编程序只要能跑出来就好了,不需要考虑程序的持久性,但是自从遇到比较大的项目,数据量很大,要循环很多次查询数据库,程序甚至需要运行几天。这样我就遇到了这里所说的错误,mysql out of memory needed 8164 bytes. 而且我都不知到错在哪。
以后编程要知道收尾,每创建一个对象,每使用一次内存,想着是不是要在该释放的时候释放。
p2. errno:2003,error:can't connect to mysql on server "172..." (111) :
最终解决:因为我有多个线程,每个线程里创建MYSQL connect,建立连接,一开始没有对建立连接的语句加锁,后来加锁后,就可以了:
pthread_mutex_t m_mutex();
pthread_mutex_lock(&m_mutex);
mysql_init(&conn);
if (mysql_real_connect(&conn,"172.16.66.1","xaf","111111","bai",0,NULL,0)
{
cout<<"success!"<<endl;
}
else
{
cerr<<"failed"<<endl;
if (mysql_errno(&conn))
{ cout<<mysql_errno(&conn)<<" "<<mysql_error(&conn);}
}
pthread_mutex_unlock(&m_mutex);
p3. 多线程运行程序,如果不在每个线程函数结束加mysql_close(&conn) ;之前调试运行时,每次都没有释放连接,会出现error:too many connections.
相关文章推荐
- mysql存储过程详解
- mysql Profiler性能
- 查看mysql数据库字符集,设置mysql字符集为utf8
- mysql环境变量设置
- mysql数据库插入中文时出现ERROR 1…
- mysql环境变量设置
- mysql数据库插入中文时出现ERROR 1…
- ubuntu系统下更改mysql数据目录的方法
- Mysql Error -1418
- MySQL存储过程
- MySQL数据库中字段设置了zerofill,页面上显示时整数前仍带有0以及如何查询字段注释
- Mysql 更改最大连接数
- C#连接Mysql数据库
- mysql远程访问\大小写\密码验证
- mysql字段类型简介
- ubuntu下操作mysql数据库
- ubuntu下安装mysql(转)
- mysql数据表的创建、查看、删除和…
- mysql分表,分区的区别和联系
- mysql快速复制表