Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C/++ connecting to mysql server problem.

Options
  • 22-02-2008 12:58pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭


    Hello, I am trying to follow the following tutorial on http://forums.mysql.com/read.php?45,49606,49606 to connect to a mysql server(although I am using 2003 not 6.0) and I am getting an error.
    Has anybody come across a way to fix:
    connect fatal error LNK1256: ALINK operation failed (80070005) : Access is denied.
    
    Is it maybe easier to download mysql++ for 2003 or 'easy way to use mysql++' and try to use one of them?
    Thanks.


Comments

  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Hmm, nobody know? :-)

    Do you know if mysqlpp.lib is the same as mysql++.lib? Or are they different?


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Yet another question, I am trying to follow this tutorial now as well;
    http://www.devarticles.com/c/a/Cplusplus/Building-a-Store-Application-With-MySQL-and-C/2/
    I download mysql++ run the scripts etc
    I 'extract the zip file to c:\mysql++. Copy c:\mysql++\mysql\lib\libmySQL.dll to c:\winnt\system32 and create a new console application in Visual C++.'

    It then asks me to 'add the following line to the additional include directories text box:

    c:\mysql++\include,c:\mysql++\mysql\include'

    Now, these folders don't exist, there is no include folder in mysql++ after it is extracted and certainly no mysql folder with an include folder in it. I instead link to the directory with the header files in mysql++ and the include folder in my mysql installation, is this wrong?

    I then follow this step 'Lastly, right click on the resource files node and add c:\mysql++\lib\mysql++.lib. ' This file does not exist, I add mysqlpp.lib instead from my mysql installation but I think that is wrong, they are probably different files.
    There are no library files in mysql++ when I extract it, run the perl script etc, is there something I am missing?
    DO I need to create this mysql++.lib somehow from teh extracted files and am not doing it right?


    Anyway, as of now, I run this code:
    #include <stdio.h>
    #define W32_LEAN_AND_MEAN
    #include <winsock2.h>
    #include "mysql.h"
    
    // change these to suit your setup
    #define TABLE_OF_INTEREST "some_table"
    #define SERVER_NAME "mysql_server"
    #define DB_USER "user"
    #define DB_USERPASS "pa55w0rd"
    #define DB_NAME "db_name"
    
    // prototypes
    void showTables(MYSQL*);
    void showContents(MYSQL*,const char*);
    
    int main(int argc, char* argv[])
    {
    MYSQL *hnd=NULL; // mysql connection handle
    const char *sinf=NULL; // mysql server information
    
    hnd = mysql_init(NULL);
    if (NULL == mysql_real_connect(hnd,SERVER_NAME,DB_USER,DB_USERPASS,DB_NAME,0,NULL,0))
    {
    fprintf(stderr,"Problem encountered connecting to the &#37;s database on %s.\n",DB_NAME,SERVER_NAME);
    }
    else
    {
    fprintf(stdout,"Connected to the %s database on %s as user '%s'.\n",DB_NAME,SERVER_NAME,DB_USER);
    sinf = mysql_get_server_info(hnd);
    
    if (sinf != NULL)
    {
    fprintf(stdout,"Got server information: '%s'\n",sinf);
    showTables(hnd);
    showContents(hnd,TABLE_OF_INTEREST);
    }
    else
    {
    fprintf(stderr,"Failed to retrieve the server information string.\n");
    }
    
    mysql_close(hnd);
    }
    
    return 0;
    }
    
    void showTables(MYSQL *handle)
    {
    MYSQL_RES *result=NULL; // result of asking the database for a listing of its tables
    MYSQL_ROW row; // one row from the result set
    
    result = mysql_list_tables(handle,NULL);
    row = mysql_fetch_row(result);
    fprintf(stdout,"Tables found:\n\n");
    while (row)
    {
    fprintf(stdout,"\t%s\n",row[0]);
    row = mysql_fetch_row(result);
    }
    mysql_free_result(result);
    
    fprintf(stdout,"\nEnd of tables\n");
    
    return;
    }
    
    void showContents
    (
    MYSQL *handle,
    const char *tbl
    )
    {
    MYSQL_RES *res=NULL; // result of querying for all rows in table
    MYSQL_ROW row; // one row returned
    char sql[1024], // sql statement used to get all rows
    commastr[2]; // to put commas in the output
    int i,numf=0; // number of fields returned from the query
    
    sprintf(sql,"select * from %s",tbl);
    fprintf(stdout,"Using sql statement: '%s' to extract all rows from the specified table.\n",sql);
    
    if (!mysql_query(handle,sql))
    {
    res = mysql_use_result(handle);
    if (res)
    {
    numf = mysql_num_fields(res);
    row = mysql_fetch_row(res);
    fprintf(stdout,"Rows returned:\n\n");
    while (row)
    {
    commastr[0]=commastr[1]=(char)NULL;
    for (i=0;i<numf;i++)
    {
    if (row[i] == NULL)
    {
    fprintf(stdout,"%sNULL",commastr);
    }
    else
    {
    fprintf(stdout,"%s%s",commastr,row[i]);
    }
    commastr[0]=',';
    }
    fprintf(stdout,"\n");
    
    row = mysql_fetch_row(res);
    }
    fprintf(stdout,"\nEnd of rows\n");
    
    mysql_free_result(res);
    }
    else
    {
    fprintf(stderr,"Failed to use the result acquired!\n");
    }
    }
    else
    {
    fprintf(stderr,"Failed to execute query. Ensure table is valid!\n");
    }
    
    return;
    }
    
    and I get the following errors.
    c:\mysql++\lib\exceptions.h(44): warning C4275: non dll-interface class 'exception' used as base for dll-interface class 'mysqlpp::Exception'
    c:\mysql++\lib\query.h(129): warning C4275: non dll-interface class 'std::ios_base' used as base for dll-interface class 'std::basic_ios<_Elem,_Traits>'
            with
            [
                _Elem=char,
                _Traits=std::char_traits<char>
            ]
    mysql error LNK2001: unresolved external symbol "void __stdcall mysql_close(struct st_mysql *)" (?mysql_close@@$$J14YGXPAUst_mysql@@@Z)
    mysql error LNK2001: unresolved external symbol "struct st_mysql * __stdcall mysql_real_connect(struct st_mysql *,char const *,char const *,char const *,char const *,unsigned int,char const *,unsigned long)" (?mysql_real_connect@@$$J232YGPAUst_mysql@@PAU1@PBD111I1K@Z)
    mysql error LNK2001: unresolved external symbol "struct st_mysql * __stdcall mysql_init(struct st_mysql *)" (?mysql_init@@$$J14YGPAUst_mysql@@PAU1@@Z)
    mysql fatal error LNK1120: 3 unresolved externals
    

    Although, this may be becasue I set it up wrong...


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Although, this may be becasue I set it up wrong...
    All those link errors imply that. "Unresolved external symbol" means the compiler is looking for code it can't find. So it's obviously not set up correctly.

    I had a look at the tarball on tangentsoft.net/mysql++ - there are VS solution files and a file called "README-Visual-C++.txt" that has complete instructions on how to build it.

    Did you try that? Might be worth it if not.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    I have a readme.txt, where did you see "README-Visual-C++.txt", I downloaded

    'mysql++-2.3.2.tar.gz (1.6 MB, 2007.07.11) — Library source code. If you aren’t sure which file to download, download this.'


    Are you looking at a different download?

    Thanks for the help!


  • Closed Accounts Posts: 413 ✭✭sobriquet


    I have a readme.txt, where did you see "README-Visual-C++.txt"

    In the 3.0.0 RC version - my bad. Still, the README.vc looks similar, did you go through that?


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    I did but I must have made a mistake, I'll start from scratch. I must have to build the source into library files, that didn't happen.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    One thing I didn't do, was make it an mfc project, I made it a win32 one, does that matter?

    Edit: I don't think it does, the example program looks like it works with win32.


  • Registered Users Posts: 2,426 ✭✭✭ressem


    Have you noticed the install.bat file contained in the mysql++ tarball?

    It's purpose amongst other things is to create the include folder and copy the header files into this folder. And yes, you are meant to build the lib/dlls from the source code that you downloaded.


    The code in your third post appears to be written to use the standard c header files and libraries as bundled with the server in C:\Program Files\MySQL\MySQL Server 5.0\include and C:\Program Files\MySQL\MySQL Server 5.0\lib\opt.
    So if I read the posts right, then you haven't had these files in your build and link paths causing you errors.

    I'd suggest getting the plain c version working, perhaps a simpler version to work out the build and link bugs
    http://www.ucl.ac.uk/is/mysql/c/

    And only when that's done, look at adding the mysql++ stuff.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    When I ran that .bat in the command line I wrote 'install.bat include', it said installed sucessfully but I noticed no changes.
    Oh, just searched my comp, found the headers and include folder in some other folder! Nice.

    Thanks for the advice, I'll give it a shot.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    And yes, you are meant to build the lib/dlls from the source code that you downloaded.
    Do you know how to do this? I must keep making some mistake...


  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    I get as far as this and I don't know what to enter:
    hmm.jpg

    I have a list of created .exes from after I started running the program:
    hmm2.jpg

    I don't know if it is one of these or something else..

    Can I just use any of these sample programs?
    I try and it says can not connect to mysql, which is an improvement I suppose.
    I still don't see any .lib files.


  • Registered Users Posts: 981 ✭✭✭fasty


    You don't see any .lib files because you're searching for *.exe

    There's a VS6 project called mysqlpp that seems to build the lib/dll files you need to link to.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Hehe, I am not that silly. There are no .lib files created after I ran mysqlpp that I can see.
    I am not referring to the search. :-)


  • Registered Users Posts: 981 ✭✭✭fasty


    It's not about being silly! :D 90% of problems like this are just simple things that you need someone else to point out!

    If no files were created when you compiled mysqlpp, then what was the error output.

    It should output a dll and lib called mysqlpp.lib/mysqlpp.dll.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Ah, I got it!
    Now they are created.
    Is there meant to be a mysql++.lib too?
    I can never find that thing, unless they keep referring to mysqlpp as both mysql++ and mysqlpp...

    Now, I am trying to run the sample programs and getting an werro saying can not connect to mysql, better than the other errors I was getting.


  • Registered Users Posts: 981 ✭✭✭fasty


    when referring to files and function names etc with MySQL++, they'll always be mysqlpp instead.

    For future reference, you can check the name of the output exe or dll from Visual Studio's project settings. I've included a VC6 screenshot since I didn't want to add MySQL libs to my VS2008 setup but they're all the same.

    Regarding the sample programs...

    Another dumb question, but are you telling them to connect to YOUR database? The simple1 sample requires a DB to be specified on the command line.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Ah, thanks for the screen shot and help.

    I thought it tried to connect to some sample database or something? I'm not sure how it works. I think I have to run exrun.bat and resetdb(to creat a sample database) or something but I am not doing it right, it fails saying "unknown mysql server host 'resetdb' <11004>". There is also some makefile thing that I am not quite following if I have to use it or not. Stupid me needs the stupid questions. :)


  • Registered Users Posts: 981 ✭✭✭fasty


    You should take a look through the sample code and various docs before going any further. I don't know much about MySQL++ to be honest. I downloaded it to help you out. I just use the C library when I'm dealing with MySQL connections.

    There's full documentation on the whole library and how to run the samples in the doc folder.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Ah ok cheers.

    If I could use the c library to connect that would be great, but I failed at that also. :)


  • Registered Users Posts: 981 ✭✭✭fasty


    Going back to the C library first as suggested earlier would be a good idea. The C++ version is just a wrapper around that.


  • Advertisement
  • Registered Users Posts: 2,426 ✭✭✭ressem


    Mysql seem to have removed some sample code, and VS workspaces that was distributed with the source in 4.x versions.

    There's a few gotchas that you find when using the c libraries under windows. First one is the order in which you have to add the include headers.

    #include "stdafx.h"
    #include "winsock2.h"
    #include "mysql.h"
    #include <stdio.h>

    Otherwise you get errors about SOCKET being undefined/redefined, and a load of misleading errors following.

    For the basic C project I basically ran through the procedure

    Create a project:
    File > New Project > Win32Console Application

    called mysqltesting

    Pasted the four headers

    #include "winsock2.h"
    #include "mysql.h"
    #include <stdio.h>

    under stdafx.h
    at the top of the file.

    Right clicked on the project name in the solution explorer.
    Configuration properties / C/C++ /General
    Under Additional Include Directories added "C:\Program Files\MySQL\MySQL Server 5.0\include"

    Under
    Configuration properties / Linker /Input /Additional Dependencies
    "C:\Program Files\MySQL\MySQL Server 5.0\lib\opt\libmysql.lib"

    Paste the code from the link above.

    Change the 4 char strings and the select to point to your own DB.

    And worked (using VS 2K5)

    ---

    If you're having issue you can't sort out how about downloading the windows source code for 4.1.22 and having a look at the simplest example mysql-4.1.22-win-src.zip\mysql-4.1.22\libmysqltest\

    where only the link to the libmysql.lib needed to be corrected to work.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Thanks a lot mate, will give it a shot.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,098 Mod ✭✭✭✭Tar.Aldarion


    Gotten it down to one error.
    mysqltest fatal error LNK1104: cannot open file 'C:\Program.obj'

    working on it...

    Edit: Ah, fixed it, it now connects to mysql. Thanks a bunch!
    Now I just have to write code and stuff to update mysql tables....


Advertisement