C++ C++,向量&;mysql
我从以下位置找到此代码:C++ C++,向量&;mysql,c++,mysql,vector,C++,Mysql,Vector,我从以下位置找到此代码: #包括 #包括 #包括 #包括 #包括 #包括 int main() { std::向量表; MYSQL_RES*结果; MYSQL_行; MYSQL*连接,MYSQL; int状态; connection=mysql\u real\u connect(&mysql,“localhost”,“username”,“password”,“database”,0,0,0); if(连接==NULL) { std::cout将此代码移动到返回std::vector的函数中,并
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
std::向量表;
MYSQL_RES*结果;
MYSQL_行;
MYSQL*连接,MYSQL;
int状态;
connection=mysql\u real\u connect(&mysql,“localhost”,“username”,“password”,“database”,0,0,0);
if(连接==NULL)
{
std::cout将此代码移动到返回std::vector
的函数中,并从main()
调用该函数:
std::vector get_tables()
{
std::向量表;
MYSQL_RES*结果;
MYSQL_行;
MYSQL*连接,MYSQL;
int状态;
connection=mysql\u real\u connect(&mysql,“localhost”,“username”,“password”,“database”,0,0,0);
...
返回表;
}
int main()
{
std::vector tables=get_tables();
//用“桌子”做些有用的事情
返回0;
}
我想这段代码应该在另一个函数中,肯定不是在main()中。我想问题可能是当连接或查询调用失败时,您确实返回了表;
。这意味着您从main()
和main()返回了std::vector
)
应该返回一个int,这是因为表
变量被定义为向量
,但它作为函数的返回值返回;但函数被声明为具有int
作为返回值。
解决此问题的最佳方法是将处理table
的部分移动到另一个函数,并从main调用该函数(并重写部分代码).return tables;
main无法向其调用者返回std::vector
。复制粘贴编码可能适用于Java或PHP之类的语言,但如果您在C++中尝试它,您将遭到破坏。我知道,我需要任何帮助。。您试图用此代码做什么?我正在尝试连接到mysql并使用C++@user110读取数据库4856抱歉,在您希望理解或使用该代码之前,您需要学习该语言的基本知识。该代码仍然有效:它显示警告:控件达到非无效的结尾function@user1104856在这一点上,我的建议是拿起一本好的C++教材。我会的,但我确实尝试着返回表格,它说:EXC_BAD_ACCESS我尝试使用上面来自aix的代码,但我得到:程序收到信号:“EXC_BAD_ACCESS”。你能修复它吗?我真的不知道问题出在哪里,但我猜文件访问或mysql访问都有问题。请给我一个例子,或者修复它……我想选择最佳答案
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> tables;
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL *connection, mysql;
int state;
connection = mysql_real_connect(&mysql,"localhost","username","password","database",0,0,0);
if (connection == NULL)
{
std::cout << mysql_error(&mysql) << std::endl;
return tables;
}
state = mysql_query(connection, "SHOW TABLES");
if (state !=0)
{
std::cout << mysql_error(connection) << std::endl;
return tables;
}
result = mysql_store_result(connection);
std::cout << "tables: " << mysql_num_rows(result) << std::endl;
while ( ( row=mysql_fetch_row(result)) != NULL )
{
tables.push_back(row[0]);
}
mysql_free_result(result);
mysql_close(connection);
return 0;
}
error: cannot convert 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >' to 'int' in return
error: cannot convert 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >' to 'int' in return
std::vector<std::string> get_tables()
{
std::vector<std::string> tables;
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL *connection, mysql;
int state;
connection = mysql_real_connect(&mysql,"localhost","username","password","database",0,0,0);
...
return tables;
}
int main()
{
std::vector<std::string> tables = get_tables();
// do something useful with `tables'
return 0;
}