-1

I found this code from: http://www.davenicholas.me.uk/blog/view_post/29/How-to-c-mysql-mac-osx

#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;
}

But when I build it I get the error:

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

and

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
Alecs
  • 2,256
  • 8
  • 32
  • 45
user1104856
  • 147
  • 3
  • 14

4 Answers4

3

I think the problem may be that you do return tables; when the connect or query calls fail. This means that you return a std::vector from main(), and main() should return an int.

Weston
  • 1,845
  • 13
  • 12
1

Move this code into a function that returns std::vector<std::string>, and call that function from main():

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;
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 4
    @user1104856 You need to learn the very basics of the language before you can hope to understand or use that code, sorry. – Konrad Rudolph Mar 06 '12 at 16:33
  • It still dosent work: IT SAYS warning: control reaches end of non-void function – user1104856 Mar 06 '12 at 16:35
  • 1
    @user1104856: With respect, my recommendation at this juncture would be to pick up a good C++ textbook. See http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – NPE Mar 06 '12 at 16:41
  • I will, but i did try to return tables, it says: EXC_BAD_ACCESS – user1104856 Mar 06 '12 at 16:43
1

This code is supposed to be in another function I think, definitely not in main().

ebutusov
  • 563
  • 2
  • 5
1

That's because the table variable is defined as a vector, but it's returned as the function's return value; but the function is declared to have int as return value. The best way to solve this problem is to move the parts which process table to another function and call that from main (and rewrite some parts of the code).

MMS
  • 408
  • 2
  • 5
  • 9
  • I try'd use the above code from aix, but i get : Program received signal: “EXC_BAD_ACCESS”. Could you please fix it? – user1104856 Mar 07 '12 at 01:09
  • I don't really know what the problem is, but I guess there is something wrong either with file access or mysql access. – MMS Mar 07 '12 at 15:04