c语言连接mysql——insert的代码实现

//mysql_insert
//gcc mysql_insert.c -o test -I /usr/include/mysql -L /usr/lib -lmysqlclient

#include <stdio.h>
#include <mysql/mysql.h>
#include <string.h>

#define HOST "localhost"
#define USERNAME "SERVER"
#define PASSWORD "SERVER_PASSWORD"
#define DATABASE "remote_control"

int main()
{
char device_id[12] = "12345678901x";
char device_mac[20] = "12345678901234567890";
char device_nikname[20] = "12345678901234567890";
char device_online_status[1] = "1";
char device_switch_status[1] = "1";
char dstr[107];
char *insert_device_sql = &dstr[0];

strcpy (dstr, "insert into devices values(‘");
strcat (dstr, device_id);
strcat (dstr, "‘,‘");
strcat (dstr, device_mac);
strcat (dstr, "‘,‘");
strcat (dstr, device_nikname);
strcat (dstr, "‘,‘");
strcat (dstr, device_online_status);
strcat (dstr, "‘,‘");
strcat (dstr, device_switch_status);
strcat (dstr, "‘)");
//printf("insert_device_sql =%d\n", (int)strlen(dstr));
//char *insert_device_sql = "insert into devices values(‘‘,‘‘,‘‘,‘‘,‘‘)";

char user_id[12] = "123456789012";
char user_account[11] = "12345678901";
char user_password[8] = "12345678";
char user_online_status[1] = "1";
char ustr[82];
char *insert_user_sql = &ustr[0];

strcpy (ustr, "insert into users values(‘");
strcat (ustr, user_id);
strcat (ustr, "‘,‘");
strcat (ustr, user_account);
strcat (ustr, "‘,‘");
strcat (ustr, user_password);
strcat (ustr, "‘,‘");
strcat (ustr, user_online_status);
strcat (ustr, "‘)");
//printf("insert_user_sql= %d\n", (int)strlen(ustr));
//char *insert_user_sql = "insert into user values(‘‘,,‘‘,‘‘)";

//char *sql = insert_device_sql;
char *sql = insert_user_sql;

MYSQL my_connection;
mysql_init(&my_connection);/*初始化mysql连接my_connection*/

if (!mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, CLIENT_FOUND_ROWS))
{/*连接失败*/
printf("数据库连接失败: %s\n", mysql_error(&my_connection));
return 0;
}

mysql_query(&my_connection, "set names utf8");/*设置编码为utf8*/

int res;
res = mysql_real_query(&my_connection, sql, (unsigned int)strlen(sql));
if (res)
{/*执行失败*/
printf("sql語句执行失败: %s\n", mysql_error(&my_connection));
mysql_close(&my_connection);
return 0;
}

printf("数据插入成功!\n");
printf("%d行数据受到影响!\n", (int)mysql_affected_rows(&my_connection));

mysql_close(&my_connection);

return 0;
}

时间: 03-11

c语言连接mysql——insert的代码实现的相关文章

c语言连接mysql——select的代码实现

//mysql_select//gcc mysql_select.c -o test -I /usr/include/mysql -L /usr/lib -lmysqlclient #include <stdio.h>#include <string.h>#include <mysql/mysql.h> #define HOST "localhost"#define USERNAME "SERVER"#define PASSWOR

Ubuntu下C语言连接MySQL

最近写了哟个程序需要用C语言连接MySQL,是基于Ubuntu的,我就写了如下的代码(其中包括了UDP协议部分) 事实上我们就是通过系统自带的头文件通过SQL语句对数据库进行操作,这应该对熟悉数据库语言的人就非常简单了 附上可用的代码: #include <stdio.h> /* These are the usual header files */ #include <string.h> #include <unistd.h> /* for close() */ #i

C语言连接MySQL数据库(课程设计总结)

刚结束课程设计,也预示着假期马上就要到来了.本次课程设计并不算难,无非就是让做一个XXX系统,实现用户的注册.登录.菜单管理.超级用户等等一些功能,到现在为止已经做过好几个了,所以基本流程都熟悉了!我觉的最值得总结的地方就是:C语言与数据库连接,这块内容!因为之前都是用文件实现的. ★平台 这次课程设计主要用到: ● VC ++ 6.0 ● mysql-5.0.18-win32 ● Navicat for MySQL(MySQL 图形化的工具) ★配置 在写代码之前首先要让编译器知道MySQL数

C语言连接mysql数据库

操作系统:win7/64 编译软件:VS2010 数据库:5.7.11 从C语言连接mysql数据库包含两个步骤: 1 初始化连接句柄结构 2 实际创建连接 测试代码1: #include "stdafx.h" #include <WinSock2.h> /*socket通信,系统头文件*/ #include <windows.h> #include <stdio.h> #include "mysql.h" #pragma com

Python连接MySQL的实例代码

Python连接MySQL的实例代码 MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/ 下载解压缩后放到%Python_HOME%/Lib/site-packages目录中,python会自动找到此包. MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0. 其他: 1. 平台及版本 linux 内核2.6,gcc 3.4.4,glibc 2

Java连接MySQl数据库实现代码

1. 获取数据库连接和查询代码 package connectionmysql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ConnectionMysql { //数据库连接用户名 private String userName

JAVA使用JDBC连接MySQL数据库(3)-代码部分

1 /**************连接数据库部分********************/ 2 public static Connection conn(){ 3 Connection conn = null; 4 String driver = "com.mysql.jdbc.Driver"; 5 String url = "jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8"; 6 String u

linux-c语言连接mysql

#include <mysql/mysql.h> #include <stdio.h> #include<stdlib.h> int main() { MYSQL *conn; MYSQL_RES *res; MYSQL_ROW row; char *server = "localhost"; char *user = "root"; char *password = "123456"; char *datab

R语言连接Mysql数据库

install.packages("DBI") install.packages("RMySQL") library(DBI) library(RMySQL) con <- dbConnect(MySQL(),host="XXXXX",dbname="stocku",user="root",password="XXXX") dbListTables(con) dbReadTable