level 8
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <mysql/mysql.h>/*注意要包含这个头文件*/
#pragma comment(lib,"libmysql")
/*定义了一些数据库连接需要的宏*/
#define HOST "localhost"
#define USERNAME "root"
#define PASSWORD "123456"
#define DATABASE "test"
/*这个函数用来执行传入的sql语句*/
void exe_sql(char* sql) {
MYSQL my_connection; /*这是一个数据库连接*/
int res; /*执行sql语句后的返回标志*/
/*初始化mysql连接my_connection*/
mysql_init(&my_connection);
/*这里就是用了mysql.h里的一个函数,用我们之前定义的那些宏建立mysql连接,并
返回一个值,返回不为空证明连接是成功的*/
if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD,
DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) {/*连接成功*/
printf("数据库执行exe_sql连接成功!n");
/*这句话是设置查询编码为utf8,这样支持中文*/
mysql_query(&my_connection, "set names utf8");
/*下面这句话就是用mysql_query函数来执行我们刚刚传入的sql语句,
这会返回一个int值,如果为0,证明语句执行成功*/
res = mysql_query(&my_connection, sql);
if (res) {/*现在就代表执行失败了*/
printf("Error: mysql_query !\n");
/*不要忘了关闭连接*/
mysql_close(&my_connection);
} else {/*现在就代表执行成功了*/
/*mysql_affected_rows会返回执行sql后影响的行数*/
printf("%d 行受到影响!\n",
mysql_affected_rows(&my_connection));
/*不要忘了关闭连接*/
mysql_close(&my_connection);
}
} else {
/*数据库连接失败*/
printf("数据库执行exe_sql连接失败!\n");
}
}
/*这个函数用来执行传入的sql语句,并打印出查询结果*/
void query_sql(char* sql) {
MYSQL my_connection; /*这是一个数据库连接*/
int res; /*执行sql语句后的返回标志*/
MYSQL_RES *res_ptr; /*指向查询结果的指针*/
MYSQL_FIELD *field; /*字段结构指针*/
MYSQL_ROW result_row; /*按行返回的查询信息*/
int row, column; /*查询返回的行数和列数*/
int i, j; /*只是控制循环的两个变量*/
/*初始化mysql连接my_connection*/
mysql_init(&my_connection);
/*这里就是用了mysql.h里的一个函数,用我们之前定义的那些宏建立mysql连接,并
返回一个值,返回不为空证明连接是成功的*/
if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD,
DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) {/*连接成功*/
printf("数据库查询query_sql连接成功!\n");
/*这句话是设置查询编码为utf8,这样支持中文*/
mysql_query(&my_connection, "set names utf8");
*下面这句话就是用mysql_query函数来执行我们刚刚传入的sql语句,
这会返回一个int值,如果为0,证明语句执行成功*/
res = mysql_query(&my_connection, sql);
if (res) { /*现在就代表执行失败了*/
printf("Error: mysql_query !\n");
/*不要忘了关闭连接*/
mysql_close(&my_connection);
} else { /*现在就代表执行成功了*/
/*将查询的结果给res_ptr*/
res_ptr = mysql_store_result(&my_connection);
/*如果结果不为空,就把结果print*/
if (res_ptr) {
/*取得结果的行数和*/
column = mysql_num_fields(res_ptr);
row = mysql_num_rows(res_ptr) + 1;
printf("查询到 %lu 行 \n", row);
/*输出结果的字段名*/
for (i = 0; field = mysql_fetch_field(res_ptr); i++)
printf("%st", field->name);
printf("\n");
/*按行输出结果*/
for (i = 1; i < row; i++) {
result_row = mysql_fetch_row(res_ptr);
for (j = 0; j < column; j++)
printf("%st", result_row[j]);
printf("\n");
}
}
/*不要忘了关闭连接*/
mysql_close(&my_connection);
}
}
}
int main(int argc, char *argv[]) {
/*测试下向里面插入数据*/
char *query;
char *exe = "insert into student values('lala','hahhahah!');";
exe_sql(exe);
*测试下查询*/
query="select * from student;";
query_sql(query);
return 0;
}
2012年08月29日 11点08分
2
level 8
⒈从有序数列和无序数列{a2,a3,…,an}开始进行排序; ⒉处理第i个元素时(i=2,3,…,n),数列{a1,a2,…,ai-1}是已有序的,而数列{ai,ai+1,…,an}是无序的。用ai与ai-1,a i-2,…,a1进行比较,找出合适的位置将ai插入; ⒊重复第二步,共进行n-i次插入处理,数列全部有序。 void insertSort(Type* arr,long len)/*InsertSort algorithm*/ { long i=0,j=0;/*iterator value*/ Type tmpData; assertF(arr!=NULL,"In InsertSort sort,arr is NULL\n"); for(i=1;i<len;i++) { j=i; tmpData=*(arr + i); while(j > 0 && tmpData < arr[j-1]) { arr[j]=arr[j-1]; j--; } arr[j]=tmpData; } }
2012年08月29日 11点08分
5
level 8
1 #include <mysql.h>/*注意要包含这个头文件*/
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 /*定义了一些数据库连接需要的宏*/
7 #define HOST "localhost"
8 #define USERNAME "ABitNo"
9 #define PASSWORD "ABitNo"
10 #define DATABASE "abitno"
11
12 /*这个函数用来执行传入的sql语句*/
13 void exe_sql(char* sql) {
14
15 MYSQL my_connection; /*这是一个数据库连接*/
16 int res; /*执行sql语句后的返回标志*/
17
18 /*初始化mysql连接my_connection*/
19 mysql_init(&my_connection);
20
21 /*这里就是用了mysql.h里的一个函数,用我们之前定义的那些宏建立mysql连接,并
22 返回一个值,返回不为空证明连接是成功的*/
23 if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE,
24 0, NULL, CLIENT_FOUND_ROWS)) {/*连接成功*/
25
26 printf("数据库执行exe_sql连接成功!\n");
27
28 /*这句话是设置查询编码为utf8,这样支持中文*/
29 mysql_query(&my_connection, "set names utf8");
30
2012年08月29日 11点08分
6
level 8
31 /*下面这句话就是用mysql_query函数来执行我们刚刚传入的sql语句,
32 这会返回一个int值,如果为0,证明语句执行成功*/
33 res = mysql_query(&my_connection, sql);
34
35 if (res) {/*现在就代表执行失败了*/
36 printf("Error: mysql_query !\n");
37 /*不要忘了关闭连接*/
38 mysql_close(&my_connection);
39 } else {/*现在就代表执行成功了*/
40 /*mysql_affected_rows会返回执行sql后影响的行数*/
41 printf("%d 行受到影响!\n\n", mysql_affected_rows(&my_connection));
42 /*不要忘了关闭连接*/
43 mysql_close(&my_connection);
44 }
45
46 } else {
47 /*数据库连接失败*/
48 printf("数据库执行exe_sql连接失败!\n");
49 }
50 }
51
52 /*这个函数用来执行传入的sql语句,并打印出查询结果*/
53 void query_sql(char* sql) {
54 MYSQL my_connection; /*这是一个数据库连接*/
55 int res; /*执行sql语句后的返回标志*/
56 MYSQL_RES *res_ptr; /*指向查询结果的指针*/
57 MYSQL_FIELD *field; /*字段结构指针*/
58 MYSQL_ROW result_row; /*按行返回的查询信息*/
59
2012年08月29日 11点08分
8