linux 下 用c语言创建mysql数据库笔记(二)
来源:互联网 发布:孔浩的java视频 编辑:程序博客网 时间:2024/05/21 17:32
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 * fromip");
if(!res){
printf("%lu",(unsignedlong)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,"connectionFailed/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/ -lmysqlclient
《例二》
#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_ROWrow_p;
intres;
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('HelloMysql')");
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);
return0;
}
编译:
gcc -o mysql.out mysql.c -L /usr/lib/mysql/ -lmysqlclient
《例一》
#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 * fromip");
if(!res){
printf("%lu",(unsignedlong)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,"connectionFailed/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/ -lmysqlclient
《例二》
#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_ROWrow_p;
intres;
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('HelloMysql')");
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);
return0;
}
编译:
gcc -o mysql.out mysql.c -L /usr/lib/mysql/ -lmysqlclient
- linux 下 用c语言创建mysql数据库笔记(二)
- linux 下 用c语言创建mysql数据库笔记(一)
- linux下c语言进行mysql数据库操作---笔记
- linux下mysql数据库C语言编程
- linux下c语言连接mysql数据库
- linux下c语言查询mysql数据库
- linux下c语言操作mysql数据库!
- linux下C语言操作MySQL数据库
- Linux 下 C语言连接MYSQL数据库
- linux下c语言操作mysql数据库
- Linux下用C语言API连接MySQL数据库
- Linux下用C语言API连接MySQL数据库
- Linux下用C语言访问MySQL数据库的程序
- Linux下用C语言API连接MySQL数据库
- Linux下用C语言连接MySQL数据库
- Linux centos 7下: 用 C 语言连接 MySQL数据库
- (《Linux环境下C语言编程指南》学习笔记二)C语言中进程的创建(fork和vfork函数的异同)
- (Mysql 二)Linux C语言显示mysql数据库中某个表的数据
- 硬盘安装Linux和从Grub命令行启动操作系统
- 股海沉浮:人民币升值对哪些行业利好
- 周鸿祎:要替自己正名先要颠覆曾经的商业思维
- WinMain函数参数介绍
- 关于open,fopen的参数,白话文版!
- linux 下 用c语言创建mysql数据库笔记(二)
- 条款46:需要类型转换时请为模板定义非成员函数
- php时间戳与日期互转
- October, 21
- 中国会变成一个世界IT工厂么?
- jAVA程序人员必须知道的。
- 开始→运行→输入的命令集锦
- Linux中tty pty pts 概念区别
- 091021周三