Command and Conquer: Renegade Official Forums - RDF feed
type: channel
description: C&C: Renegade --> Dying since 2003™, resurrected in 2024!
items: MySQL with SSGM?
items: Re: MySQL with SSGM?

MySQL with SSGM?
type: item
description: It's sad that the only reason I'm posting is because I have a problem. I'm new to making c++ SSGM plugins and MySQL with c++. I'm trying to make a simple "!postnote" command that will add a player's name and message to a table in the database (I know someone is going to say "Why don't you use fstream or another io function?" It's because I want to experiment with databases). At the moment, I've been able to make it connect, but whenever it tries to execute a query, the server crashes. It doesn't seem to have any trouble with connecting to MySQL, I mean, if I use the correct username and password, it works fine, and when it's the incorrect username and password, it tells me there was an error. I only seem to have a problem when I try to execute a query, at which point, the server crashes without adding anything to the database. I also get 7 warnings when I compile it, all coming from "cppconn/exception.h". I made a simple console application using the same stuff and it worked fine. I suppose my first question would be, is it possible to have MySQL in an SSGM plugin? Second would be, is there an obvious reason it is crashing my server? Toggle Spoiler It is the same as the example plugin, except for the following changes. Notes: I changed the "Treat Warnings As Errors" to no (/wx I think) and I removed /analyze because it was causing a warning. I'm using this for the MySQL client library and this for the c++ connector. Compiler: MSVC++ 2008 Express Linked Libraries: mysqlcppconn.lib libmysql.lib Server Folder DLLs: libmysql.dll MSVCP90.dll MSVCR90.dll mysqlcppconn.dll Edited Source: Toggle Spoiler class PostNoteCommand : public ChatCommandClass { void Triggered(int ID,const TokenClass &Text,int ChatType) { /* Text is a tokenized version of the command paramter. Text[1] would get the first word after the command. Text[0] would get everything after the command. Text(3) would get word 3 and everything after it. Text(2,4) would get words 2 through 4. */ try { std::string p…
creator: TechnoBulldog
date: 2010-07-17T00:48:17-00:00

Re: MySQL with SSGM?
type: item
description: TechnoBulldog wrote on Fri, 16 July 2010 20:48 It's sad that the only reason I'm posting is because I have a problem. I'm new to making c++ SSGM plugins and MySQL with c++. I'm trying to make a simple "!postnote" command that will add a player's name and message to a table in the database (I know someone is going to say "Why don't you use fstream or another io function?" It's because I want to experiment with databases). At the moment, I've been able to make it connect, but whenever it tries to execute a query, the server crashes. It doesn't seem to have any trouble with connecting to MySQL, I mean, if I use the correct username and password, it works fine, and when it's the incorrect username and password, it tells me there was an error. I only seem to have a problem when I try to execute a query, at which point, the server crashes without adding anything to the database. I also get 7 warnings when I compile it, all coming from "cppconn/exception.h". I made a simple console application using the same stuff and it worked fine. I suppose my first question would be, is it possible to have MySQL in an SSGM plugin? Second would be, is there an obvious reason it is crashing my server? Toggle Spoiler It is the same as the example plugin, except for the following changes. Notes: I changed the "Treat Warnings As Errors" to no (/wx I think) and I removed /analyze because it was causing a warning. I'm using this for the MySQL client library and this for the c++ connector. Compiler: MSVC++ 2008 Express Linked Libraries: mysqlcppconn.lib libmysql.lib Server Folder DLLs: libmysql.dll MSVCP90.dll MSVCR90.dll mysqlcppconn.dll Edited Source: Toggle Spoiler class PostNoteCommand : public ChatCommandClass { void Triggered(int ID,const TokenClass &Text,int ChatType) { /* Text is a tokenized version of the command paramter. Text[1] would get the first word after the command. Text[0] would get everything after the command. Text(3) would get word 3 and everything after it. Text(2,4) woul…
creator: reborn
date: 2010-07-17T04:47:40-00:00

Re: MySQL with SSGM?
type: item
description: Yes, I believe StrFormat would expect C strings for %s parameters and would crash if you gave it std::string instead. You should also convert Text[0] to a C string. Internally StrFormat simply uses sprintf (or an equivalent of) and expects a character array for the relevant parameter to a %s tag and passing anything other than a char array (c string) would cause a crash.
creator: danpaul88
date: 2010-07-17T08:44:37-00:00

Re: MySQL with SSGM?
type: item
description: Pull the sqlite stuff and stacker from jnz's code
creator: Hex
date: 2010-07-17T09:23:08-00:00

Re: MySQL with SSGM?
type: item
description: yes u can use C++ and mysql i actually infact have finished using the mysql C++ connetor to connect to a database, take a look at this artical http://lstigile.wordpress.com/2009/05/19/using-libmysqld-with-microsoft-visual-c -2008-express/
creator: Sladewill
date: 2010-07-17T10:45:10-00:00

Re: MySQL with SSGM?
type: item
description: Wow, thanks for the help! It's working now! I'm also wondering if there is a mysql_real_escape_string function for c++ like there is for PHP. This would help a lot. Also, does anyone have a clue why I get 6 warnings? Warnings: Toggle Spoiler C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(47) : warning C4251: 'sql::SQLException::sql_state' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'sql::SQLException' with [ _Elem=char, _Traits=std::char_traits, _Ax=std::allocator ] C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(79) : warning C4512: 'sql::SQLException' : assignment operator could not be generated C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(41) : see declaration of 'sql::SQLException' C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(88) : warning C4512: 'sql::MethodNotImplementedException' : assignment operator could not be generated C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(81) : see declaration of 'sql::MethodNotImplementedException' C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(97) : warning C4512: 'sql::InvalidArgumentException' : assignment operator could not be generated C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(90) : see declaration of 'sql::InvalidArgumentException' C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(106) : warning C4512: 'sql::InvalidInstanceException' : assignment operator could not be generated C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(99) : see declaration of 'sql::InvalidInstanceException' C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include\cppconn/exception.h(116) : warning C4512: 'sql::NonScrollableException' : assignment operator could not be generated C:\Program Files\MySQL\MySQL Connector C…
creator: TechnoBulldog
date: 2010-07-17T13:57:43-00:00

Re: MySQL with SSGM?
type: item
description: I'm glad you've got it working, welldone! Can I ask what it is you plan to do with this? What are you working on? Anything for rene that needs DB support sounds pretty interesting. What are your plans? /* google this, para phrasing, but from daniweb: mysql_real_escape_string is actually from the MySQL C API, rather than the developers of PHP. The PHP developers created addslashes(). char * S = new char[strlen(m_headerSubject.c_str())*3 +1]; mysql_real_escape_string(conn, S, m_headerSubject.c_str(), m_headerSubject.length()); m_headerSubject = m_headerSubject.assign(S); */
creator: reborn
date: 2010-07-17T18:08:46-00:00

Re: MySQL with SSGM?
type: item
description: reborn wrote on Sat, 17 July 2010 13:08 I'm glad you've got it working, welldone! Can I ask what it is you plan to do with this? What are you working on? Anything for rene that needs DB support sounds pretty interesting. What are your plans? /* google this, para phrasing, but from daniweb: mysql_real_escape_string is actually from the MySQL C API, rather than the developers of PHP. The PHP developers created addslashes(). char * S = new char[strlen(m_headerSubject.c_str())*3 +1]; mysql_real_escape_string(conn, S, m_headerSubject.c_str(), m_headerSubject.length()); m_headerSubject = m_headerSubject.assign(S); */ I did read something like that, but I can't find the function. I'm guessing it's in a file I haven't included. Originally, I had the idea to use a database because a friend of mine, Sonic (Sonic2876) said he was planning on opening up a few servers, his main focus being a build server. He said he was having issues making "!load" and "!save" in Lua, and I thought that I might be able to do it in c++. However, I've never gone very deep in c++ (my learning level is somewhere around intermediate) and I never got into fstream or any of those functions. I have recently been experimenting with MySQL and php, though, and the first thing I thought of was storing the objects in a database. I figured it would be cleaner and more efficient. However, I am nowhere near ready to make a build server, so I decided to experiment around first. The idea for this plugin came from Cloud, my main server and one I moderate on. DarkOrbit has a lua script for "!postnote" that I think writes to a file, but I'm not entirely sure. I thought that if you wrote it to a database, a simple php script could let you view all of the notes from in game players and clear it relatively easily. If you write it to a file, you have to manually connect the server and find the file, which sounds like a hassle. At the moment, I just need to find a way to prevent injection attacks (which work easier than …
creator: TechnoBulldog
date: 2010-07-17T18:26:36-00:00

Re: MySQL with SSGM?
type: item
description: mysql_real_escape_string is in the MySQL library, NOT the c++ connector.... to be honest I am not sure why there IS a c++ connector when you can use the mysql library directly in c++ anyway... The function you want is defined in the MySQL API which is installed alongside MySQL if you select the installation option to do so. You will also need to link to libmysql.dll, which is included with the API. However I doubt that you will have much success mixing and matching the c++ connector with the raw API functions and you may have to convert your existing code to use the API directly (which, to be honest, is more efficient anyway).
creator: danpaul88
date: 2010-07-17T20:00:22-00:00

Re: MySQL with SSGM?
type: item
description: danpaul88 wrote on Sat, 17 July 2010 15:00 mysql_real_escape_string is in the MySQL library, NOT the c++ connector.... to be honest I am not sure why there IS a c++ connector when you can use the mysql library directly in c++ anyway... The function you want is defined in the MySQL API which is installed alongside MySQL if you select the installation option to do so. You will also need to link to libmysql.dll, which is included with the API. However I doubt that you will have much success mixing and matching the c++ connector with the raw API functions and you may have to convert your existing code to use the API directly (which, to be honest, is more efficient anyway). I have the C API, but I'm not sure how to use it really. Is there a specific header file I need to include?
creator: TechnoBulldog
date: 2010-07-17T20:25:23-00:00

Re: MySQL with SSGM?
type: item
description: mysql.h should be the only header you need to include to use the library, just make sure to also add libmysql.dll to the linker input options.
creator: danpaul88
date: 2010-07-17T20:51:20-00:00

Re: MySQL with SSGM?
type: item
description: When I press build after including mysql.h, it has six errors in mysql_com.h, as well as the other six warnings. Toggle Spoiler 1>c:\program files\mysql\mysql connector c 6.0.2\include\mysql_com.h(268) : error C2146: syntax error : missing ';' before identifier 'fd' 1>c:\program files\mysql\mysql connector c 6.0.2\include\mysql_com.h(268) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\program files\mysql\mysql connector c 6.0.2\include\mysql_com.h(268) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\program files\mysql\mysql connector c 6.0.2\include\mysql_com.h(437) : error C2065: 'SOCKET' : undeclared identifier 1>c:\program files\mysql\mysql connector c 6.0.2\include\mysql_com.h(437) : error C2146: syntax error : missing ')' before identifier 's' 1>c:\program files\mysql\mysql connector c 6.0.2\include\mysql_com.h(438) : error C2059: syntax error : ')'
creator: TechnoBulldog
date: 2010-07-17T21:01:40-00:00

Re: MySQL with SSGM?
type: item
description: #include "windows.h" using namespace::std; They are what fixed it for me yesterday.
creator: Sladewill
date: 2010-07-18T10:15:29-00:00

Re: MySQL with SSGM?
type: item
description: Sladewill wrote on Sun, 18 July 2010 05:15 #include "windows.h" using namespace::std; They are what fixed it for me yesterday. Oh, yeah, that fixes it, I think. I think WIN32_LEAN_AND_MEAN was messing it up, as well as the order it was in. I can access the function now, but I'm trying to find a way to use the c++ connector instead of an open C connection. I have a feeling that's impossible (lol) but I'm going to keep looking for a way. Again, thanks for the help so far everyone! EDIT: I've found out that if you use mysql_escape_string (no real) it doesn't require an open connection, but it also doesn't use the current character set, or whatever that is. http://dev.mysql.com/doc/refman/5.1/en/mysql-escape-string.html Quote: You should use mysql_real_escape_string() instead! This function is identical to mysql_real_escape_string() except that mysql_real_escape_string() takes a connection handler as its first argument and escapes the string according to the current character set. mysql_escape_string() does not take a connection argument and does not respect the current character set.
creator: TechnoBulldog
date: 2010-07-18T13:58:31-00:00

Re: MySQL with SSGM?
type: item
description: Why are you against using the connection and query functions in the MySQL API rather than the ones in the C++ connector? Using them directly would be more efficient in the long run, since the C++ connector is doing that internally anyway but adding an extra level between your program and the API. MySQL* db_connection = mysql_init(NULL); mysql_real_connect ( db_connection, "localhost", "username", "password", "schema", NULL, NULL, NULL ); Connecting to a MySQL database using the API is that simple. Anyway, obviously it's up to you to decide which way you prefer but I am still a bit confused why anyone would use the c++ connector when the API works in c++ anyway...
creator: danpaul88
date: 2010-07-18T15:42:09-00:00

Re: MySQL with SSGM?
type: item
description: danpaul88 wrote on Sun, 18 July 2010 10:42 Why are you against using the connection and query functions in the MySQL API rather than the ones in the C++ connector? Using them directly would be more efficient in the long run, since the C++ connector is doing that internally anyway but adding an extra level between your program and the API. MySQL* db_connection = mysql_init(NULL); mysql_real_connect ( db_connection, "localhost", "username", "password", "schema", NULL, NULL, NULL ); Connecting to a MySQL database using the API is that simple. Anyway, obviously it's up to you to decide which way you prefer but I am still a bit confused why anyone would use the c++ connector when the API works in c++ anyway... Mostly because I'm stubborn and am not used to the C API. However, I'll look into it if it's as simple as you say lol. I'll look into it later, though, I'm a bit busy right now.
creator: TechnoBulldog
date: 2010-07-18T17:41:15-00:00

Re: MySQL with SSGM?
type: item
description: Is the plugin itself creating the database and tables and such, or are you manually creating that first, then allowing the plugin to use it? Is the plugin multi-threaded? Does it need to be? I don't know much about databases at all, really, let alone securing it. Would you need to make it so secure if the database was being access locally on the machine, and was not set-up for remote access to the DB?
creator: reborn
date: 2010-07-18T19:20:33-00:00

Re: MySQL with SSGM?
type: item
description: reborn wrote on Sun, 18 July 2010 14:20 Is the plugin itself creating the database and tables and such, or are you manually creating that first, then allowing the plugin to use it? Is the plugin multi-threaded? Does it need to be? I don't know much about databases at all, really, let alone securing it. Would you need to make it so secure if the database was being access locally on the machine, and was not set-up for remote access to the DB? No the plugin itself does not create the databases or tables. I'm not really sure what you mean by multi-threaded, but my guess is that it means to break it down into parts as not to cause lag? If that's the case, no, it doesn't, and I don't think it needs to. It's not a command that will be used a lot, the only thing it does is add something to a small table in a database. I was kinda planning on it only working on one machine, but there is a username and password needed to connect. The only thing I'm worried about is injections, which I don't know a lot about, but I believe this is what they are. Say you type !postnote <message> and the message gets sent to the database. The query would look something like: ("INSERT INTO messages(name, message) VALUES('%s', '%s')", pName.c_str(), M.c_str()) If someone typed !postnote hello the message stored in the database would say "hello". However, if someone was to type something like "!postnote '); DROP renegade;" the literal translation of the query would be: ("INSERT INTO messages(name, message) VALUES('hacker', ''); DROP renegade; ')" Although there would be an error with that code, the basic principal of that is that they could control your database. If the above query didn't have an error, "DROP renegade" would have deleted the entire database, and that obviously wouldn't be good. If I can secure the database to only work with this account on 127.0.0.1 and escape the string (like add a \ before the quotation marks and whatnot) that eliminates two of the main ways they can get in. Does…
creator: TechnoBulldog
date: 2010-07-18T19:52:42-00:00

Re: MySQL with SSGM?
type: item
description: Ah, I get it, thank you.
creator: reborn
date: 2010-07-18T20:02:51-00:00

Re: MySQL with SSGM?
type: item
description: what u can do is only allow alpha numerical digits to be used in a query which is a handy function we've used to prevent users from posting horrid names into saves which causes a server crash.
creator: Sladewill
date: 2010-07-18T20:24:57-00:00

Re: MySQL with SSGM?
type: item
description: Couldn't mysql_real_escape_string work? Then if I run it back out through php, I can just use strip stripslashes I think. Also, would it be more effecient to reconnect every time someone types !postnote or to have an open connection and only send a query when someone types it?
creator: TechnoBulldog
date: 2010-07-19T13:42:12-00:00