下输入的,将其改为在英文状态下输入
8. 操作实例
//mysql_linux_Num2
//mysql基本操作
//朱昌志
//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[111])
{
Point_Num=0;
int fscanf_ret=0;
float temppoint[3];
FILE *fp;
char * filename="/home/zhu/compress/mysql_linux_Num2/1.txt"; ///使用‘/‘而非‘//‘
if ((fp=fopen(filename,"r"))==NULL)
printf("can't open the file!");
else
{
while (!feof(fp))
{
fscanf_ret=fscanf(fp,"%f %f %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[111])
{
float a,b,c;
//char *sql=new char[50];
char sql[200];
int pointnum2=0;
MYSQL mysql;
MYSQL_ROW row;
MYSQL_RES *result;
mysql_init (&mysql);
//连接MYSQL数据库
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));
}
//创建数据库example
mysql_query(&mysql,"create database example");
//创建新用户
mysql_query(&mysql,"grant all on example.* to zhu@'localhost' identified by '123'");
//连接example数据库
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
{
//创建一个表并将点插入表中
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++) //不能在‘for’ 循环内部初始化
{
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);
}
//输出表中点的个数
mysql_query(&mysql,"select count(point_x) from point");
result=mysql_store_result(&mysql);
if((row=mysql_fetch_row(result)))
{
printf("排序前点的个数==>%s\n",row[0]); //因为row是MYSQL_ROW类型的,是字符型,所以此处是%s
}
//输出表中点的数据
mysql_query(&mysql,"select * from point");
result=mysql_store_result(&mysql);
printf("排序前表中的数据==>\n");
while((row=mysql_fetch_row(result)))
{
printf("%s %s %s\n",row[0],row[1],row[2]);
}
}
//按升序选择表中数据
mysql_query(&mysql,"select distinct * from point order by point_x asc");
result=mysql_store_result(&mysql);
//输出表中的数据
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 %s\n",row[0],row[1],row[2]);
printf("%s %s %s\n",row[0],row[1],row[2]);
pointnum2++;
}
printf("\n排序后点的个数是==>%d\n",pointnum2);
}
//mysql_query(&mysql,"DAY()");
//result=mysql_store_result(&mysql);
//row=mysql_fetch_row(result);
//printf("%d",row[0]);
//创建视图
printf("\n输出新建视图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 %s\n",row[0],row[1],row[2]);
}
//连接多个数据表
printf("\n连接point和temp_point两个表将他们的内容同时输出内容如下\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 %s\n",row[0],row[1],row[2]);
}
//删除表中数据和表
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[111];
Point_Num=ReadData(Point_Num,point);//从文件中读点并返回点的个数
DatabaseOperation(Point_Num,point);//数据库相关操作
return 0;
}