Thread: Mysql

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    184

    Mysql

    Hello everybody,

    I have another problem(Of Course). I am testing out a short little program I found on a forum that connects to a MySql database and runs a query. From what I can tell, the syntax looks fairly correct, I just keep getting some build errors. I am running on XP, using .NET, and have downloaded and setup MYSQL. The database is there and I have been able to run queries from the command line. I added the lib and include directories. Apparently it is finding some of the files. Here is the code followed by the errors:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <c:/mysql/include/mysql.h>
    #define host "appsrvr"
    #define username "root"
    #define password "nbtis01"
    #define database "mysql"
    
    MYSQL *conn;
    
    int main()
    {
    	conn = mysql_init(NULL);
    
    	mysql_real_connect(conn,host,username,password,database,0,NULL,0);
    
    	MYSQL_RES *res_set;
    	MYSQL_ROW row;
    	unsigned int i;
    
    	mysql_query(conn,"SELECT user, host, password FROM user");
    
    	res_set = mysql_store_result(conn);
    
    	unsigned int numrows = mysql_num_rows(res_set);
    
    	while ((row=mysql_fetch_row(res_set)) != NULL)
    	{
    		for (i=0; i<mysql_num_fields(res_set); i++)
    		{
    			printf("%s\n", row[i] != NULL ? row[i]:"NULL");
    		}
    	}
    
    	mysql_close(conn);
    	return 0;
    }
    Here are the errors:
    Code:
    Compiling...
    SQL Practice.cpp
    c:\mysql\include\mysql_com.h(116) : error C2146: syntax error : missing ';' before identifier 'fd'
    c:\mysql\include\mysql_com.h(116) : error C2501: 'st_net::SOCKET' : missing storage-class or type specifiers
    c:\mysql\include\mysql_com.h(116) : error C2501: 'st_net::fd' : missing storage-class or type specifiers
    c:\mysql\include\mysql_com.h(180) : error C2065: 'SOCKET' : undeclared identifier
    c:\mysql\include\mysql_com.h(180) : error C2146: syntax error : missing ')' before identifier 's'
    c:\mysql\include\mysql_com.h(181) : error C2059: syntax error : ')'
    c:\Documents and Settings\kgoodrich.SCANNER\My Documents\Visual Studio .NET Projects\SQL Practice\SQL Practice.cpp(25) : warning C4244: 'initializing' : conversion from 'my_ulonglong' to 'unsigned int', possible loss of data

    Any ideas why those errors would pop up. I would not think that the syntax would be wrong, because I installed everything from the binary download off of mysql.com. Any ideas would be greatly appreciated.

    Thanks a mil,
    Kendal

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    mysql.h is missing the definition for SOCKET...do this:

    Code:
    #include <winsock.h>
    #include <c:/mysql/include/mysql.h>
    Also, you should add "C:/mysql/include" to your "include search path" - don't know how its done in .NET but in VC++ 6.0 you do Projects->Settings->C/C++->Category: Preprocessor, in the "Additional include directories:" you can add "c:/mysql/include".

    Then you'll be able to "#include <mysql.h>"

    gg

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    Thanks codeplug,

    That fixed that problem. I never would have figured out that I needed winsock.h on my own thanks. Now I am getting the following linker errors:

    Code:
    SQL Practice.obj : error LNK2019: unresolved external symbol _mysql_close@4 referenced in function _main
    SQL Practice.obj : error LNK2019: unresolved external symbol _mysql_num_fields@4 referenced in function _main
    SQL Practice.obj : error LNK2019: unresolved external symbol _mysql_fetch_row@4 referenced in function _main
    SQL Practice.obj : error LNK2019: unresolved external symbol _mysql_num_rows@4 referenced in function _main
    SQL Practice.obj : error LNK2019: unresolved external symbol _mysql_store_result@4 referenced in function _main
    SQL Practice.obj : error LNK2019: unresolved external symbol _mysql_query@8 referenced in function _main
    SQL Practice.obj : error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function _main
    SQL Practice.obj : error LNK2019: unresolved external symbol _mysql_init@4 referenced in function _main
    I have done a search and all of theses are exported in libmysql.def and libmysqld.def. mysql_close is also referenced in mysql.h, but I can't seem to figure out where these are defined at. Any suggestions???

    Thanks again,
    Kendal

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You need to link in C:\mysql\lib\opt\libmySQL.lib

    In VC++ 6.0, you do by going to Project->Settings->Link->(General)->Object/Library modules: and append " C:\mysql\lib\opt\libmySQL.lib" in that edit box.

    You also have to have C:\mysql\lib\opt\libmySQL.DLL in the same directory as the executable.

    gg

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    The library is included in the linker options. On .NET I go to project properties and Linker settings and add library directories. The library directory that has been added includes libmysql.lib and libmysql.dll among other .libs. I also have in the C++ settings the include file directory added in to the options. In the .NET environment, in the project options, you have a couple of different settings. There is a C++ tab and a Linker tab among others. Under the C++ tab and the Linker tab, they have a bunch of subsections like General, Input, Advanced, Command Line, etc. The only way I could get the program to compile was to add libmysql.lib in to the Additional Dependencies option of the Input subtab of the Linker tab. I was thinking, I shouldn't have to do that if the Directory containing the library is included in the "Additional Library Directories" option of the General subtab of the Linker tab, Should I??????????????? It works, I just think that there is a better, proper way to do it. This might be it, I just don't know. What do you think?????????????

    Thanks,
    Kendal

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I love this thread, it solved all my problems with MySQL, lol! Anyways, this is the way I did the directories thing under C++.NET:

    - Tools -> Projects -> VC++ Directories
    - Choose "Include Files" under the "Show Directories For:" combo box.
    - Add "C:\mysql\include"
    - Choose "Library Files" under the same combo box
    - Add "C:\mysql\lib\opt"
    - Click OK

    And you're done! All you have to do in your project is:
    #include <mysql.h>
    .... and it will automatically link the files for you. No hassle at all.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    I don't have a TOOLS->PROJECTS->C++ DIRECTORIES
    The way I have to do it and did it is under PROJECT menu->PROPERTIES.
    It then give you a split window with a directory structure on the left hand side. Under C++->GENERAL , I add c:\mysql\include.
    Under LINKER->GENERAL, I add c:\mysql\lib\opt. After I added those and #include <mysql.h>, it gave me unresolved external symbol errors. The only way I could get it to work is by going into the PROJECT menu->PROPERTIES and selecting LINKER->INPUT. In there there is an option of ADDITIONAL DEPENDENCIES, there I add libmysql.lib and it works. I would figure that by including the lib and include directories in the correct paths, I would not have to explicitly add libmysql.lib to the dependencies. It's got me????

    Thanks again everyone,
    Kendal

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Originally posted by gvector1
    On .NET I go to project properties and Linker settings and add library directories.
    So you have .NET. You should have a screen that looks like this:

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    648

    Forgot the pic lol:

    :

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    And btw, I quickly made some C++ warpper classes for the MySQL C API. They're just a few classes, easy to use. Why did I make them? Because the MySQL++ (the C++ API for it) doesn't compile under C++.NET at all and it was ........ing me off. So, I made my own to facilitate the use of MySQL. C++ hence classes, not functions.

    The point? They're available upon request. Just a friendly thing.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    Your right, I do have an option screen like that. I thought you were saying that there was a project option under the tools menu. My bad...lol. Anyway, I tried it like that and did not have any luck. The only way I can get it to compile and work is add to the additional dependencies option. Here is a screen shot to show what option I am talking about.

    I am sending the screenshot as an attachment. I can't seem to figure out how you attached your image to the body of the thread. How did you do that????? Anyway the screenshot is attached. If you don't mind, I would like to take a look at your wrapper classes for the MYSQL API. What I am looking at right now is using ODBC or OLE DB. Just exploring options right now. I appreciate the info greatly.

    Thanks again,
    Kendal

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    184
    Nevermind about the image thing. I didn't realize that it would automatically do that with images. It didn't do that when I clicked preview. Anyway, disregard the picture question.

    Thanks,
    Kendal

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I'm on AIM a lot. Here's my screenname, I'll send you the files then: speedy5asdf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change detection in MySQL.
    By amitbora27 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-13-2009, 10:34 AM
  2. MySQL libraries
    By csonx_p in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2008, 02:23 AM
  3. About C++ and MySQL or oether free database
    By xxxrugby in forum C++ Programming
    Replies: 18
    Last Post: 12-30-2005, 06:28 AM
  4. Get data from mysql and use in a system.
    By smaakage in forum Tech Board
    Replies: 3
    Last Post: 10-04-2005, 12:03 PM
  5. Get data from mysql and use in a system.
    By smaakage in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 01:25 PM