How can I assign the field value to a float variable in the C ++ program if the field returned by MySQL mysql_row is float?

Source: Internet
Author: User

Cy163 note:In this article, "mysql_row pointer is a simple string array. All data types are converted to strings and sent to the client ." It makes sense. In additionArticleLearn how to useAtoi ()AndAtof ()Convert int and float values in the mysql_row string to integer and floating point values.

I,In yourProgramUsing the database is a good note. If you already have an available MySQL server, you can use the following method in VC to connect to the database.
1. Find the include folder under the MySQL (win32) installation directory and add it to the VC header file directory list;
(Vc6-> options-> directories-> Add to this directory)
(Vc2005-> tool-> select-> project and solution-> VC ++ directory-> display the following content directory-> include files-> Add to this directory) 2. Find the Lib folder under the MySQL (win32) installation directory and add it to the VC library file directory list;
(Vc6-> options-> directories-> Add to this directory)
(Vc2005-> tool-> select-> Project settlement solution-> VC ++ directory-> display the following content directory-> library file-> Add to this directory, note: Lib \ debug or Lib \ OPT)
3. Create a project, as shown in the following figure.Code:
// Mysql. cpp: defines the entry point for the console application.

# Include "stdafx. H"
# Include <stdio. h>
# Include <Winsock. h>
# Include <mysql. h>
# Include <windows. h>
# Pragma comment (Lib, "libmysql. lib ")
Int main (INT argc, char * argv [])
{
Unsigned short Port = 3306;
Char * IPaddress = "192.168.31.56 ";
Char * username = "root ";
Char * Password = "";
Char * dbname = "sas_1_2_0 ";
Printf ("start ...");

MySQL * ssock;
// Char execsql [500];
Ssock = (MySQL *) malloc (sizeof (MySQL ));
// In some versions, you can view mysql. h and readme
Mysql_init (ssock );
If (ssock = NULL)
{
Printf ("eror: MySQL ssock init error .");
Return false;
}

// Connect to the specified database
Ssock = mysql_real_connect (ssock, IPaddress, username, password, null, port, null, 0 );
If (! Ssock)
{
Printf ("conn fail ...");
// Memcpy (EEE, mysql_error (ssock), 20 );
// Fprintf (stderr, "failed to connect to database: Error: % s", mysql_error (ssock ));

// Printf ("% C", eee );
Unsigned int mtint = mysql_errno (ssock );
// Printf ("% d ");
Return false;
}
If (mysql_select_db (ssock, dbname )! = 0)
{
Printf ("select dB error .");
Return false;
}
Printf ("version = % d", mysql_get_server_version (ssock ));
// Exec my execsql string
// Sprintf (execsql, "create table girls (name char (10), age INT )");
// Mysql_real_query (ssock, execsql, strlen (execsql ));
Mysql_close (ssock );
Printf ("end ...");
Return true;
}

II , How to use C/C ++ programming to connect to the MySQL database
To perform a query, follow these steps. First, execute a query and save the result,
The result is a subset. Here is a small example:
# Include <stdio. h>
# Include
# Include <mysql. h>
MySQL;
Mysql_res * res;
Mysql_row row;
Void exiterr (INT exitcode)
{
Fprintf (stderr, "% s \ n", mysql_error (& MySQL ));
Exit (exitcode );
}
Int main ()
{
Uint I = 0;
If (! (Mysql_connect (& MySQL, "host", "username", "password ")))
Exiterr (1 );
If (mysql_select_db (& MySQL, "payroll "))
Exiterr (2 );
If (mysql_query (& MySQL, "Select name, rate from emp_master "))
Exiterr (3 );
If (! (RES = mysql_store_result (& MySQL )))
Exiterr (4 );
While (ROW = mysql_fetch_row (RES ))){
For (I = 0; I <mysql_num_fields (RES); I ++)
Printf ("% s \ n", row [I]);
}
Mysql_free_result (RES );
Mysql_close (& MySQL );
}
Mysql_query The function sends the query to the server. If the query is successful, the mysql_store_result function is called to allocate a mysql_res structure and obtain a result set from the server again.
You can use Mysql_fetch_row Function to view data. In this way, you will get Mysql_row The Pointer Points to a row in the data. The mysql_row pointer is a simple string array. All data types are converted to strings and sent to the client.
Mysql_num_fields The function will tell you how many columns are returned. You can continue to callMysql_fetch_row Until it returns a null pointer to get each row in the query.
Note that in this example, we do not check the empty pointer column. If you do not use a table with non-empty columns, you must check whether the columns of a special row are empty. Once you have used a result set, you must release it. This is done through mysql_free_result.
Last call Mysql_close To close the connection between you and the database.
You do not need to call Mysql_fetch_row The number of rows in the returned result set. This
Int mysql_num_rows (mysql_res * result.
Change to the row returned by the next mysql_fetch_row call. You can use void mysql_data_seek (mysql_res * res, uint offset) to change to any row. For more information, you can use these additional functions to find more information about a query and obtain the information from the server.
If you execute an update, insert, or delete query, you can use int mysql_affected_rows to find out how many rows of data are affected.
If binary data exists in your database, it is useful to know the Data Length. Unsigned int * mysql_fetch_lengths (mysql_res * MySQL) returns an integer array that indicates the length of each column in The result set.
When you insert a table with the auto_increment column, you can use int mysql_insert_id (MySQL * MySQL) to find the ID of the generated row.
======================================

Int main ()
{
MySQL;
Mysql_res * res;
Mysql_field * FD;
Mysql_row row;
Int ID [10000];
Double result [10000] [8];
Vector <string> V;

If (mysql_init (& MySQL) = NULL)
{
STD: cout <"init Mysql Data status fail" <STD: Endl;
Return false;
}
Else
{
STD: cout <"init Mysql Data Status Success" <STD: Endl;
}
 
If (null = mysql_real_connect (& MySQL, "localhost", "root", "", "wsnss", mysql_port, null, 0 ))
{
STD: cout <"connect database fail" <STD: Endl <mysql_error (& MySQL) <STD: Endl ;;
Return false;
}
Else {
STD: cout <"connect database success" <STD: Endl;
}

Char szsqltext [500] = "";
Int J = 0;

Sprintf (szsqltext, "% s", "select * From data_receive ");
If (mysql_query (& MySQL, szsqltext ))
// Perform data retrieval
{
// An error occurred while executing the SQL statement
Cout <"query error" <Endl;
Mysql_close (& MySQL );
Return false;
}
Else
{
Res = mysql_store_result (& MySQL );
Int I;
While (ROW = mysql_fetch_row (RES ))){
Id [J] = atoi (row [0]);
For (I = 1; I <mysql_num_fields (RES); I ++)
{
Result [J] [I-1] = atof (row [I]);
}
J ++;
}
}


For (INT I = 0; I <10000; I ++)
{
If (I> = J)
Break;
Char STR [10000];
Stringstream SS;

SS <"insert into data_receive (ID, data1, data2, data3, data4, data5, data6, data7, data8) values ('"<ID [I] <"', '"<
Result [I] [0] <"','" <result [I] [1] <"', '"<result [I] [2] <"', '"<
Result [I] [3] <"','" <result [I] [4] <"', '"<result [I] [5] <"', '"<
Result [I] [6] <"','" <result [I] [7] <"');";
String S = ss. STR ();
V. push_back (s );
}
For (vector <string >:: iterator iter = V. Begin (); iter! = V. End (); ++ ITER)
{

If (mysql_query (& MySQL, (* ITER). c_str ())! = 0)
{
STD: cout <"execute insert syntax fail" <
STD: Endl <mysql_error (& MySQL) <Endl;
Mysql_close (& MySQL );
Return 1;
}
}
Mysql_free_result (RES );
Mysql_close (& MySQL );
Return 0;
}

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.