C 연결 코드
4634 단어 코드
#include <mysql.h>/* */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/* */
#define HOST "localhost"
#define USERNAME "ABitNo"
#define PASSWORD "ABitNo"
#define DATABASE "abitno"
/* 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 !
");
/* 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 !
");
/* */
mysql_close(&my_connection);
} else {/* */
/*mysql_affected_rows sql */
printf("%d !
", mysql_affected_rows(&my_connection));
/* */
mysql_close(&my_connection);
}
} else {
/* */
printf(" exe_sql !
");
}
}
/* 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)) {/*Connection success*/
printf(" query_sql !
");
/* 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 !
");
/* */
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
", row);
/* */
for (i = 0; field = mysql_fetch_field(res_ptr); i++)
printf("%s\t", field->name);
printf("
");
/* */
for (i = 1; i < row; i++) {
result_row = mysql_fetch_row(res_ptr);
for (j = 0; j < column; j++)
printf("%s\t", result_row[j]);
printf("
");
}
}
/* */
mysql_close(&my_connection);
}
}
}
int main(int argc, char *argv[]) {
/* */
char *exe = "insert into abitno values('ABitNo','http://ABitNo.LinPie.com');";
exe_sql(exe);
/* */
char *query = "select * from abitno;";
query_sql(query);
return 0;
}
http://abitno.me/linux-c-connect-mysql.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
vue 단일 페이지에 여러 개의 echarts 도표가 있을 때의 공용 코드 쓰기html에서: 데이터 처리는 말할 필요가 없다.응, 직접 그림을 그려: 공통 섹션: 이 페이지를 떠날 때 파괴: 추가 정보: Vue + Echarts 차트 표시 및 동적 렌더링 준비 작업 echarts 의존 설치 n...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.