linux 下 用c语言创建mysql数据库笔记(二)
-------两个简单的例子,供参考比较
《例一》
#include <stdio.h>
#include <stdlib.h>
#include "/usr/include/mysql/mysql.h"
int main(){
MYSQL my_connection;
int res;
mysql_init(&my_connection);
if(mysql_real_connect(&my_connection,"localhost","root","123","data",0,NULL,CLIENT_FOUND_ROWS)){
printf("connection success/n");
res= mysql_query(&my_connection,"select * from ip");
if(!res){
printf("%lu",(unsigned long)mysql_affected_rows(&my_connection));
}
else{
fprintf(stderr,"insert error %d:%s/n",mysql_errno(&my_connection), mysql_error(&my_connection));
}
mysql_close(&my_connection);
}
else{
fprintf(stderr,"connection Failed/n");
if(mysql_errno(&my_connection)){
fprintf(stderr,"insert error %d:%s/n",mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
编译:
gcc -o mysql.out mysql.c -L /usr/lib/mysql/ -l mysqlclient
《例二》
#include<stdio.h>
#include<stdlib.h>
#include"/usr/include/mysql/mysql.h"
int main(int argc,char**argv)
{
MYSQL *sql_p;
MYSQL_RES *result_p;
MYSQL_ROW row_p;
int res;
sql_p=mysql_init(NULL);
if(sql_p==NULL){
printf("Init error!/n");
exit(1);
}
sql_p=mysql_real_connect(sql_p,"localhost","root","123",
"data",0,NULL,0);
if(!sql_p){
fprintf(stderr,"%d:%s/n",mysql_errno(sql_p),mysql_error(sql_p));
exit(1);
}
res=mysql_query(sql_p,"CREATE TABLE hello(hello char(20))");
if(res){
printf("Creat table error!/n");
exit(1);
}
res=mysql_query(sql_p,"INSERT INTO hello(hello) VALUES('Hello Mysql')");
if(res){
printf("Insert error!/n");
exit(1);
}
res=mysql_query(sql_p,"SELECT hello FROM hello");
if(res){
printf("Select error!/n");
exit(1);
}
result_p=mysql_use_result(sql_p);
if(!result_p){
printf("mysql_use error!/n");
exit(1);
}
row_p=mysql_fetch_row(result_p);
mysql_close(sql_p);
return 0;
}
编译:
gcc -o mysql.out mysql.c -L /usr/lib/mysql/ -l mysqlclient
《例一》
#include <stdio.h>
#include <stdlib.h>
#include "/usr/include/mysql/mysql.h"
int main(){
MYSQL my_connection;
int res;
mysql_init(&my_connection);
if(mysql_real_connect(&my_connection,"localhost","root","123","data",0,NULL,CLIENT_FOUND_ROWS)){
printf("connection success/n");
res= mysql_query(&my_connection,"select * from ip");
if(!res){
printf("%lu",(unsigned long)mysql_affected_rows(&my_connection));
}
else{
fprintf(stderr,"insert error %d:%s/n",mysql_errno(&my_connection), mysql_error(&my_connection));
}
mysql_close(&my_connection);
}
else{
fprintf(stderr,"connection Failed/n");
if(mysql_errno(&my_connection)){
fprintf(stderr,"insert error %d:%s/n",mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
编译:
gcc -o mysql.out mysql.c -L /usr/lib/mysql/ -l mysqlclient
《例二》
#include<stdio.h>
#include<stdlib.h>
#include"/usr/include/mysql/mysql.h"
int main(int argc,char**argv)
{
MYSQL *sql_p;
MYSQL_RES *result_p;
MYSQL_ROW row_p;
int res;
sql_p=mysql_init(NULL);
if(sql_p==NULL){
printf("Init error!/n");
exit(1);
}
sql_p=mysql_real_connect(sql_p,"localhost","root","123",
"data",0,NULL,0);
if(!sql_p){
fprintf(stderr,"%d:%s/n",mysql_errno(sql_p),mysql_error(sql_p));
exit(1);
}
res=mysql_query(sql_p,"CREATE TABLE hello(hello char(20))");
if(res){
printf("Creat table error!/n");
exit(1);
}
res=mysql_query(sql_p,"INSERT INTO hello(hello) VALUES('Hello Mysql')");
if(res){
printf("Insert error!/n");
exit(1);
}
res=mysql_query(sql_p,"SELECT hello FROM hello");
if(res){
printf("Select error!/n");
exit(1);
}
result_p=mysql_use_result(sql_p);
if(!result_p){
printf("mysql_use error!/n");
exit(1);
}
row_p=mysql_fetch_row(result_p);
mysql_close(sql_p);
return 0;
}
编译:
gcc -o mysql.out mysql.c -L /usr/lib/mysql/ -l mysqlclient