Linux c MySql 插入数据
来源:互联网 发布:mac ps cs6 编辑:程序博客网 时间:2024/05/29 03:40
#include <stdio.h>#include <stdlib.h>#include <mysql.h> /* 1 id 2 type(1->tcp 2->UDP) 3 src_ip 4 dst_ip 5 src_port 6 dst_port 7 content 8 seqgcc -o inputMysqlTest1.o $(mysql_config --cflags) inputMysqlTest1.c $(mysql_config --libs) */int main(int argc, char** argv){ MYSQL *my_conn; my_conn=mysql_init(NULL); int res=0; if(!mysql_real_connect(my_conn,"localhost","root","","MyTest1",0,NULL,0)) //连接test数据库 { printf("Connect Error!n"); exit(1); } res = mysql_query(my_conn,"INSERT INTO netDataTest(type, src_ip, dst_ip, src_port, dst_port, content, seq) VALUES(1, '10.1.2.3', '192.168.1.101' , 80, 7000, 'hehe', 22 ) "); if (!res) { printf("Inserted %lu rows\n", (unsigned long)mysql_affected_rows(my_conn)); } else { fprintf(stderr, "Insert error %d: %s\n", mysql_errno(my_conn), mysql_error(my_conn)); } mysql_close(my_conn);return 0; }
如果要插入变量,就稍微复杂点。需要将sql语句复制到string里面(注意‘和“的使用)。
变量只能是字符串型,所以需要将int转为string. 这里用到的是std::stringstream。
stream << id;
string sid=stream.str();
不要忘了清空缓存,stream.str("");
#include <stdio.h>#include <stdlib.h>#include <mysql.h> #include <string> #include <sstream> using namespace std;/* 1 id 2 type(1->tcp 2->UDP) 3 src_ip 4 dst_ip 5 src_port 6 dst_port 7 content 8 seqg++ -o inputMysqlTest2.o $(mysql_config --cflags) inputMysqlTest2.cpp $(mysql_config --libs) */int main(int argc, char** argv){ MYSQL *my_conn; my_conn=mysql_init(NULL); int res=0; if(!mysql_real_connect(my_conn,"localhost","root","","MyTest1",0,NULL,0)) //连接test数据库 { printf("Connect Error!n"); exit(1); } int id =29;int type=1; string content = "ajsdaksdjsadaskdkasddaw"; string src_ip = "10.1.2.3"; string dst_ip = "100.12.2.322"; int src_port=80; int dst_port=7077; int seq = 98233; std::stringstream stream; string sid, stype,ssrc_port,sdst_port,sseq; stream << id; sid=stream.str(); stream.str(""); stream << type; stype=stream.str(); stream.str(""); stream << src_port; ssrc_port=stream.str(); stream.str(""); stream << dst_port; sdst_port=stream.str(); stream.str(""); stream << seq; sseq=stream.str(); stream.str(""); string str ="INSERT INTO netDataTest VALUES( NULL, '"+ stype +"', '"+ src_ip +"', '"+ dst_ip +"', '"+ ssrc_port +"', '"+ sdst_port +"', '"+ content +"', '"+ sseq +"');"; //res = mysql_query(my_conn, str.c_str()); res =mysql_real_query( my_conn , str.c_str() , str.length() ); if (!res) { printf("Inserted %lu rows\n", (unsigned long)mysql_affected_rows(my_conn)); } else { fprintf(stderr, "Insert error %d: %s\n", mysql_errno(my_conn), mysql_error(my_conn)); } mysql_close(my_conn);return 0; }
0 0
- Linux c MySql 插入数据
- linux C编程向mysql里插入中文数据乱码
- linux下批量插入数据到mysql
- linux mysql 插入中文数据乱码解决
- C/C++插入数据到Mysql数据库中
- 优化MySQL插入数据
- php插入mysql数据
- MySql循环插入数据
- mysql海量数据插入
- mysql批量插入数据
- MySQL插入数据
- sae mysql 插入数据
- mysql循环插入数据
- MySql循环插入数据
- mysql 循环插入数据
- Mysql插入数据错误
- mysql 插入数据加快
- mysql 插入数据问题
- 基于Levenberg-Marquardt训练算法的BP网络Python实现
- 利用基于原语的H.323协议栈开发VoIP系统
- Xcode 中代码编辑和删除的一些技巧
- SSH框架总结
- 使用.NET4.0精简版时,应该区分引用
- Linux c MySql 插入数据
- iOS6和iOS7代码的适配(2)——status bar
- lua和c的交互
- jquery 滑动效果
- 阿里妈妈还是阿里后妈
- CLIstCtrl更改行颜色!
- ECMALL敏感词过滤功能插件(用户注册、咨询、评论)
- Apriori算法第二篇----详细分析和代码实现
- (小记一下) argparse打印help信息