mysql insert muti row in one time
gcc -g *.c -lstdc++ -L/usr/lib/mysql -lmysqlclient -o test.exe
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string.h>
#define MYSQL_SERVER "144.111.182.212"
#define MYSQL_USERNAME "USER"
#define MYSQL_PASSWORD "USERPASS"
#define MYSQL_DATEBASE "sss"
static int MaxGroupId = 5000;
void show_error(MYSQL *mysql)
{
printf("Error(%d) [%s] \"%s\"", mysql_errno(mysql),
mysql_sqlstate(mysql),
mysql_error(mysql));
mysql_close(mysql);
exit(-1);
}
int ResetTable(int maxGroupID)
{
MaxGroupId = maxGroupID;
MYSQL *mysql;
const char *query;
mysql = mysql_init(NULL);
if (!mysql_real_connect(mysql, MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATEBASE, 0, NULL, 0))
{
show_error(mysql);;
exit(1);
}
query= "DELETE FROM mk_mlhg_group_id";
if (mysql_real_query(mysql, query, strlen(query)))
show_error(mysql);
char intString[10];
memset(intString, '\0', sizeof(intString));
char sql[99999];
// strcpy(sql, "");
// strcat(sql, "INSERT INTO mk_mlhg_group_id (group_id) VALUES (1)");
int i;
int count=0;
for(i=1; i<=MaxGroupId; i++)
{
if (count==0)
{
strcpy(sql, "");
sprintf(sql, "INSERT INTO mk_mlhg_group_id (group_id) VALUES (%d)", i);
} else
{
sprintf(intString, "%d", i);
strcat(sql, ",(");
strcat(sql, intString);
strcat(sql, ")");
}
count++;
if (count==1000)
{
//printf(" sql=%s\n", sql );
/* Affected rows with INSERT statement */
if (mysql_real_query(mysql, sql, strlen(sql)))
show_error(mysql);
// printf("Affected_rows after INSERT: %lu\n", (unsigned long) mysql_affected_rows(mysql));
count = 0;
strcpy(sql, "");
}
}
if (strlen(sql)> 1)
{
if (mysql_real_query(mysql, sql, strlen(sql)))
show_error(mysql);
}
printf("Reset table finish\n" );
// 切断
mysql_close(mysql);
}
int get_MLHG_group_id()
{
int num_fields;
int i, j, r, returnGroupID;
returnGroupID = -1;
MYSQL_FIELD *fields;
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
// 接続
mysql = mysql_init(NULL);
if (!mysql_real_connect(mysql, MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATEBASE, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(mysql));
exit(1);
}
if (mysql_query(mysql, " SELECT min(group_id) FROM mk_mlhg_group_id where pilot_id is NULL")) // ←Queryを記述
{
fprintf(stderr, "%s\n", mysql_error(mysql));
exit(1);
}
res = mysql_store_result(mysql);
r = mysql_num_rows(res);
for (i = 0; i < r; i++)
{
row = mysql_fetch_row(res);// ←フィールド名も含まれるらしいので、いきなり次列
if (row!= NULL && row[0] != NULL)
{
returnGroupID = atoi(row[0]);
}
else
{
returnGroupID = -1;
}
}
// 切断
mysql_close(mysql);
return returnGroupID;
}
from http://www.blueshop.com.tw/board/FUM20041006152735ZFS/BRD20030809120400WUK.html
//SQL Server INSERT 多筆資料
INSERT INTO Guestbook (name, fk_icon)VALUES('jay', 2), ('jolin', 1);
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string.h>
#define MYSQL_SERVER "144.111.182.212"
#define MYSQL_USERNAME "USER"
#define MYSQL_PASSWORD "USERPASS"
#define MYSQL_DATEBASE "sss"
static int MaxGroupId = 5000;
void show_error(MYSQL *mysql)
{
printf("Error(%d) [%s] \"%s\"", mysql_errno(mysql),
mysql_sqlstate(mysql),
mysql_error(mysql));
mysql_close(mysql);
exit(-1);
}
int ResetTable(int maxGroupID)
{
MaxGroupId = maxGroupID;
MYSQL *mysql;
const char *query;
mysql = mysql_init(NULL);
if (!mysql_real_connect(mysql, MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATEBASE, 0, NULL, 0))
{
show_error(mysql);;
exit(1);
}
query= "DELETE FROM mk_mlhg_group_id";
if (mysql_real_query(mysql, query, strlen(query)))
show_error(mysql);
char intString[10];
memset(intString, '\0', sizeof(intString));
char sql[99999];
// strcpy(sql, "");
// strcat(sql, "INSERT INTO mk_mlhg_group_id (group_id) VALUES (1)");
int i;
int count=0;
for(i=1; i<=MaxGroupId; i++)
{
if (count==0)
{
strcpy(sql, "");
sprintf(sql, "INSERT INTO mk_mlhg_group_id (group_id) VALUES (%d)", i);
} else
{
sprintf(intString, "%d", i);
strcat(sql, ",(");
strcat(sql, intString);
strcat(sql, ")");
}
count++;
if (count==1000)
{
//printf(" sql=%s\n", sql );
/* Affected rows with INSERT statement */
if (mysql_real_query(mysql, sql, strlen(sql)))
show_error(mysql);
// printf("Affected_rows after INSERT: %lu\n", (unsigned long) mysql_affected_rows(mysql));
count = 0;
strcpy(sql, "");
}
}
if (strlen(sql)> 1)
{
if (mysql_real_query(mysql, sql, strlen(sql)))
show_error(mysql);
}
printf("Reset table finish\n" );
// 切断
mysql_close(mysql);
}
int get_MLHG_group_id()
{
int num_fields;
int i, j, r, returnGroupID;
returnGroupID = -1;
MYSQL_FIELD *fields;
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
// 接続
mysql = mysql_init(NULL);
if (!mysql_real_connect(mysql, MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATEBASE, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(mysql));
exit(1);
}
if (mysql_query(mysql, " SELECT min(group_id) FROM mk_mlhg_group_id where pilot_id is NULL")) // ←Queryを記述
{
fprintf(stderr, "%s\n", mysql_error(mysql));
exit(1);
}
res = mysql_store_result(mysql);
r = mysql_num_rows(res);
for (i = 0; i < r; i++)
{
row = mysql_fetch_row(res);// ←フィールド名も含まれるらしいので、いきなり次列
if (row!= NULL && row[0] != NULL)
{
returnGroupID = atoi(row[0]);
}
else
{
returnGroupID = -1;
}
}
// 切断
mysql_close(mysql);
return returnGroupID;
}
from http://www.blueshop.com.tw/board/FUM20041006152735ZFS/BRD20030809120400WUK.html
//SQL Server INSERT 多筆資料
INSERT INTO Guestbook (name, fk_icon)VALUES('jay', 2), ('jolin', 1);
留言
張貼留言