Code implementation of C language connection Mysql--insert

Source: Internet
Author: User

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);/* Initialize the MySQL connection my_connection*/

if (!mysql_real_connect (&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, client_found_rows))
{/* Connection failed */
printf ("Database connection failed:%s\n", Mysql_error (&my_connection));
return 0;
}

mysql_query (&my_connection, "Set names UTF8");/* Set Encoding to utf8*/

int res;
res = mysql_real_query (&my_connection, SQL, (unsigned int) strlen (SQL));
if (res)
{/* Execution failed */
printf ("SQL syntax execution failed:%s\n", Mysql_error (&my_connection));
Mysql_close (&my_connection);
return 0;
}

printf ("Data insertion succeeded!\n");
printf ("%d rows of data are affected! \ n ", (int) mysql_affected_rows (&my_connection));

Mysql_close (&my_connection);

return 0;
}

Code implementation of C language connection Mysql--insert

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.