Enter it in English
8. Operate instances
// Mysql_linux_num2
// Basic MySQL operations
// Zhu Changzhi
// 2007-3-2
# Include <stdio. h>
# Include "/usr/local/MySQL/include/my_global.h"
# Include "/usr/local/MySQL/include/MySQL. H"
Typedef struct _ 3d_point
{
Float x, y, z;
} _ 3d_point;
Int readdata (INT point_num, _ 3d_point point [1, 111])
{
Point_num = 0;
Int fscanf_ret = 0;
Float temppoint [3];
File * FP;
Char * filename = "/home/Zhu/compress/mysql_linux_num2/1.txt"; // use '/' instead '//'
If (FP = fopen (filename, "R") = NULL)
Printf ("can't open the file! ");
Else
{
While (! Feof (FP ))
{
Fscanf_ret = fscanf (FP, "% F", & temppoint [0], & temppoint [1], & temppoint [2]);
If (fscanf_ret = 0 | fscanf_ret = EOF)
Break;
Point [point_num]. x = temppoint [0];
Point [point_num]. Y = temppoint [1];
Point [point_num]. z = temppoint [2];
Point_num ++;
}
}
Return point_num;
}
Void databaseoperation (INT point_num, _ 3d_point point [1, 111])
{
Float a, B, c;
// Char * SQL = new char [50];
Char SQL [200];
Int pointnum2 = 0;
MySQL;
Mysql_row row;
Mysql_res * result;
Mysql_init (& MySQL );
// Connect to the MySQL database
If (! (Mysql_real_connect (& MySQL, "localhost", "root", null, "MySQL", 0, null, 0 )))
{
Fprintf (stderr, "failed to connect to database: Error: % s \ n ",
Mysql_error (& MySQL ));
}
// Create a database example
Mysql_query (& MySQL, "create database example ");
// Create a new user
Mysql_query (& MySQL, "grant all on example. * to Zhu @ 'localhost' identified by '20140901 '");
// Connect to the example Database
If (! (Mysql_real_connect (& MySQL, "localhost", "zhu", "123", "example", 0, null, 0 )))
{
Fprintf (stderr, "failed to connect to database: Error: % s \ n ",
Mysql_error (& MySQL ));
}
Else
{
// Create a table and insert the vertex into the table
Int J = 0;
Mysql_query (& MySQL, "create table point (point_x float/* unique */, point_y float, point_z float )");
For (J; j <point_num; j ++) // It cannot be initialized inside the 'for' loop.
{
A = point [J]. X;
B = point [J]. Y;
C = point [J]. Z;
Sprintf (SQL, "insert into point values (% F, % F, % F)", A, B, C );
Mysql_query (& MySQL, SQL );
}
// Number of vertices in the output table
Mysql_query (& MySQL, "select count (point_x) from point ");
Result = mysql_store_result (& MySQL );
If (ROW = mysql_fetch_row (result )))
{
Printf ("Number of vertices before sorting => % s \ n", row [0]); // because row is of the mysql_row type and is of the bytes type, this is % s
}
// Output the vertex data in the table
Mysql_query (& MySQL, "select * from point ");
Result = mysql_store_result (& MySQL );
Printf ("data in the table before sorting ==>\ N ");
While (ROW = mysql_fetch_row (result )))
{
Printf ("% S % s \ n", row [0], row [1], row [2]);
}
}
// Select data in the table in ascending order
Mysql_query (& MySQL, "select distinct * from point order by point_x ASC ");
Result = mysql_store_result (& MySQL );
// Output table data
File * FP;
Char * filename = "/home/Zhu/compress/mysql_linux_num2/2.txt ";
If (FP = fopen (filename, "W") = NULL)
{
Printf ("cant open the file! \ N ");
}
Else
{
Printf ("\ n"> \ n ");
While (ROW = mysql_fetch_row (result )))
{
Fprintf (FP, "% S % s \ n", row [0], row [1], row [2]);
Printf ("% S % s \ n", row [0], row [1], row [2]);
Pointnum2 ++;
}
Printf ("the number of vertices after \ n sorting is ==> % d \ n", pointnum2 );
}
// Mysql_query (& MySQL, "Day ()");
// Result = mysql_store_result (& MySQL );
// ROW = mysql_fetch_row (result );
// Printf ("% d", row [0]);
// Create a view
Printf ("\ N output content in the new view myview ==>\ N ");
Mysql_query (& MySQL, "Create view myview (point_x, point_y, point_z) as select point_x, point_y, point_z from point where point_x> '31 '");
Result = mysql_store_result (& MySQL );
Mysql_query (& MySQL, "select * From myview order by point_x ");
Result = mysql_store_result (& MySQL );
While (ROW = mysql_fetch_row (result )))
{
Printf ("% S % s \ n", row [0], row [1], row [2]);
}
// Connect multiple data tables
Printf ("\ n connection point and temp_point tables output their content simultaneously as follows \ n ");
Mysql_query (& MySQL, "create table temp_point (point_x float/* unique */, point_y float, point_z float )");
Mysql_query (& MySQL, "insert into point values (1.0, 2.0, 3.0 )");
Mysql_query (& MySQL, "insert into point values (4.0, 5.0, 6.0 )");
Mysql_query (& MySQL, "insert into point values (7.0, 8.0, 9.0 )");
Mysql_query (& MySQL, "select * from point Union select * From temp_point order by point_x ");
Result = mysql_store_result (& MySQL );
While (ROW = mysql_fetch_row (result )))
{
Printf ("% S % s \ n", row [0], row [1], row [2]);
}
// Delete table data and table
Mysql_query (& MySQL, "delete from point ");
Mysql_query (& MySQL, "Drop table point ");
Mysql_query (& MySQL, "drop database example ");
}
Int main ()
{
Int point_num = 0;
_ 3d_point point [1, 111];
Point_num = readdata (point_num, point); // read the vertex from the file and return the number of vertices
Databaseoperation (point_num, point); // database operations
Return 0;
} Experience new blogs