MySQL SQL statement C ++ variable numeric variable integer variable string variable

Source: Internet
Author: User

C ++ version example, encoding problem in another post: http://blog.csdn.net/lbd2008/article/details/8426778

String name = "test ";

Int age = 23;

String SQL _insert = "insert into mytest (name, age) values ('" + name + "'," + inttostr (AGE) + ");";

Inttostr () converts int type to string type. This method is self-developed, the digital variables mentioned on the Internet can be compiled and translated in vs2010 in the form of "+ age +". I hope you can give me more advice!

PS: When the variable to be inserted contains escape characters such as single quotes, it must be processed in advance. For example, if I'm happy, convert it to I'm happy, so that SQL statements do not encounter errors. Other transfer characters can be converted by yourself.

String inttostr (INT num ){
Stringstream SS;
SS. Clear ();
SS <num;
Return ss. STR ();
}

All code:

# Include <iostream> # include <string> # include <sstream> # include <libxl. h> # include <windows. h> # include "Winsock. H "# include" MySQL. H "using namespace STD; using namespace libxl; string inttostr (INT num) {stringstream SS; SS. clear (); SS <num; return ss. STR ();} int main (INT argc, char * argv []) {const char * Host = "localhost"; // host const char * user = "root "; // user name const char * passwd = "ABC123"; // password const char * DB = "Test"; // Database Name: Unsigned int Port = 3306; // MySQL port: const char * unix_socket = 0; unsigned long client_flag = 0; MySQL; mysql_res * result; mysql_field * field; mysql_row SQL _row; string name = "China"; int age = 12; string SQL _insert = "insert into mytest (name, age) values ('"+ name +"', "+ inttostr (AGE) +"); "; string SQL _select =" select * From mytest where age = "+ inttostr (AGE) + ""; cout <SQL _insert <Endl; cout <<SQL _select <Endl; mysql_library_init (0, null, null); // initialize MySQL C apimysql_init (& MySQL); mysql_options (& MySQL, mysql_set_charset_name, "GBK "); if (mysql_real_connect (& MySQL, host, user, passwd, DB, port, unix_socket, client_flag) {cout <"database connection OK! "<Endl;/* If (! Mysql_query (& MySQL, SQL _insert.c_str () {cout <"insert success !!!!!!!! "<Endl ;}else {cout <mysql_error (& MySQL) <Endl ;}*/INT res = mysql_query (& MySQL, SQL _select.c_str (); If (! Res) {// mysql_query returns 0 cout <"success! "<Endl; Result = mysql_store_result (& MySQL); If (result) {int I, j; cout <" Number of Result: "<(unsigned long) mysql_num_rows (result) <Endl; cout <Endl; // get the column name cout <"Num of Cols:" <mysql_num_fields (result) <Endl; cout <Endl; field = mysql_fetch_fields (result); for (I = 0; I <mysql_num_fields (result); I ++) {cout <field [I]. name <"" ;}cout <Endl; while (SQL _row = mysql_fetch_row (result) {for (I = 0; I <mysql_num_fields (result); I ++) {cout <SQL _row [I] <"" ;}cout <Endl ;}}else {cout <mysql_error (& MySQL) <Endl ;}} system ("pause"); Return 0 ;}

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.