Programming multiplayer game-use MySQL ++ to operate MySQL Databases

Source: Internet
Author: User
ArticleDirectory
    • Compile MySQL ++
    •  
    • Use MySQL ++

This article briefly introduces how to use MySQL ++ to operate MySQL databases.
MySQL ++ is an officially released C ++ language API designed for MySQL. Its role is to make work easier and easier. You can learn more and download it to the MySQL ++ database. Before using the MySQL ++ database, make sure that the MySQL database server has been installed successfully. Because the MySQL ++ compilation process requires the include directory and lib directory of the MySQL database.

The MySQL ++ version used in this article is: mysql00000000-3.1.0.tar.gz

Compile MySQL ++

Download the package to MySQL ++ and decompress it to the local directory. Use vs2008 to open the vc2008 \ mysql ++. sln file in the decompressed directory. Configure the project MySQL to include the include directory under the Local MySQL installation directory. The library directory contains the lib directory under the Local MySQL installation directory, as shown below:

 

Use MySQL ++

In the ApplicationProgramWhen using the MySQL ++ library, you need to correctly configure the include and Lib inclusion paths. Before running the program, make sure that the database server has been successfully started.

Establish a connection with the database

The name of the connected database is gamedata, which is located on the local machine. The username is root and the password is blank.

//-> Create a connection to the databaseConnection con ("Gamedata","127.0.0.1","Root","");
Show table content

Database Operations are executed by calling query objects. If the current query object has returned results, you should obtain the returned results; otherwise, you can directly execute the SQL statement.

 Void Displaytable (connection & Con ){  //  -> Create a query object that is bound to our connection Query query = Con. Query ();  //  -> Assign the query to that object Query < "  Select * From playerdata  "  ;  //  -> Store the results from the query Storequeryresult res = Query. Store ();  //  -> Display the results to the console  // -> Show the field headings  Cout. SETF (IOs: Left); cout <SETW ( 10 ) < "  Username  " <SETW ( 10 ) < "  Password  " <SETW ( 10 ) < "  Age  " <Endl; storequeryresult: iterator _ it = Res. Begin ();  //  The result class has a read-only random access iterator      For (; _ It! = Res. End (); _ It ++ ) {Row & Amp; ROW = * _ It; cout <SETW ( 10 ) <Row [ "  Username  "  ] <SETW ( 10 ) <Row [ "  Password  "  ] <SETW ( 10 ) <Row [ "  Age  " ] < Endl ;}} 
Modify a row in a table
VoidUpdaterowbyusername (connection & con,Const Char*Username) {query Query=Con. Query (); query<"Update playerdata SET Password = '000000' where username ='"<Username <"';"; Query.exe cute ();}
Insert a row to the table
 Void Insertrow (connection & con, Const   Char * Username, Const  Char * Password, Int  Age) {query Query = Con. Query (); query < "  Insert into playerdata values (0 ,'  " <Username < "  ','  " <Password < "  ',  " <Age <"  );  "  ; Query.exe cute ();} 

The source code of this article can be downloaded from here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.