Windows课程设计如何连接数据库?求大神解答。
Windows课程设计如何连接数据库?求大神解答。 #include <stdio.h>#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <mysql/mysql.h>
typedef struct {
size_t test_id, t1, t2, t3;
} test_data_t;
void print_mysql_res(MYSQL_RES *mysql_res) {
MYSQL_ROW row;
unsigned int num_fields = mysql_num_fields(mysql_res);
MYSQL_FIELD *field = mysql_fetch_fields(mysql_res);
for(size_t i = 0; i < num_fields; ++i) {
printf("%-10s", field.name);
}
printf("\n");
while((row = mysql_fetch_row(mysql_res))) {
for(size_t i = 0; i < num_fields; ++i) {
printf("%-10s", row);
}
printf("\n");
}
}
void get_test_data(MYSQL *mysql, test_data_t *data) {
MYSQL_ROW row;
mysql_query(mysql, "select * from test_table");
MYSQL_RES *mysql_res = mysql_store_result(mysql);
unsigned int num_fields = mysql_num_fields(mysql_res);
size_t *array[] = {&data->test_id, &data->t1, &data->t2, &data->t3};
if((row = mysql_fetch_row(mysql_res))) {
for(size_t i = 0; i < num_fields; ++i) {
sscanf(row, "%lu", array);
}
}
mysql_free_result(mysql_res);
}
void set_test_data(MYSQL *mysql, const test_data_t *data) {
char buff;
snprintf(buff, 1024, "update test_table set test_id = %lu, t1 = %lu, t2 = %lu, t3 = %lu where test_id = 0", \
data->test_id, data->t1, data->t2, data->t3);
mysql_query(mysql, buff);
}
void task1(void) {
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "name1", "password1", "test_database", 0, NULL, 0);
for(size_t i = 0; i < 1000; ++i) {
test_data_t data;
usleep(200);
mysql_query(&mysql, "lock tables test_table write");
get_test_data(&mysql, &data);
++data.t1;
set_test_data(&mysql, &data);
mysql_query(&mysql, "unlock tables");
}
mysql_close(&mysql);
exit(0);
}
void task2(void) {
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "name2", "password2", "test_database", 0, NULL, 0);
for(size_t i = 0; i < 1000; ++i) {
test_data_t data;
mysql_query(&mysql, "lock tables test_table write");
get_test_data(&mysql, &data);
usleep(10);
++data.t2;
set_test_data(&mysql, &data);
mysql_query(&mysql, "unlock tables");
}
mysql_close(&mysql);
exit(0);
}
void task3(void) {
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "name3", "password3", "test_database", 0, NULL, 0);
for(size_t i = 0; i < 1000; ++i) {
test_data_t data;
mysql_query(&mysql, "lock tables test_table write");
get_test_data(&mysql, &data);
++data.t3;
set_test_data(&mysql, &data);
usleep(200);
mysql_query(&mysql, "unlock tables");
}
mysql_close(&mysql);
exit(0);
}
int main(void) {
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "Bdj0=3)MpqsF", NULL, 0, NULL, 0);
mysql_query(&mysql, "create database if not exists test_database");
mysql_query(&mysql, "drop table if exists test_database.test_table");
mysql_query(&mysql, "create table if not exists test_database.test_table(test_id integer, \
t1 integer, t2 integer, t3 integer)");
mysql_query(&mysql, "insert into test_database.test_table(test_id, t1, t2, t3) select 0, 0, 0, 0 from dual \
where not exists(select * from test_database.test_table)");
mysql_query(&mysql, "create user if not exists 'name1'@'localhost' identified by 'password1'");
mysql_query(&mysql, "create user if not exists 'name2'@'localhost' identified by 'password2'");
mysql_query(&mysql, "create user if not exists 'name3'@'localhost' identified by 'password3'");
mysql_query(&mysql, "grant all on *.* to 'name1'@'localhost'");
mysql_query(&mysql, "grant all on *.* to 'name2'@'localhost'");
mysql_query(&mysql, "grant all on *.* to 'name3'@'localhost'");
void (*data)(void) = {task1, task2, task3};
for(size_t i = 0; i < 3; ++i) {
if(!fork()) data();
}
int child_stat;
for(size_t i = 0; i < 3; ++i)wait(&child_stat);
mysql_query(&mysql, "select * from test_database.test_table");
MYSQL_RES *mysql_res = mysql_store_result(&mysql);
print_mysql_res(mysql_res);
mysql_free_result(mysql_res);
mysql_close(&mysql);
return 0;
}
页:
[1]