c connection to MySQL instance

Source: Internet
Author: User
MySQL today to the new colleagues to do MySQL training, the use of an example:
Connect database test username root password ABC
In the inside there is a username table, describe username results are as follows
Mysql> describe username;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| ID | Int (10) | YES | | NULL | |
| Username | varchar (32) | | PRI | | |
+----------+-------------+------+-----+---------+-------+

Mysql> SELECT * from username;
+------+----------+
| ID | Username |
+------+----------+
| 1 | SFS |
+------+----------+
1 row in Set (0.00 sec)


The purpose of this article is to find the Execute SELECT ID from username statement and print the results
Code:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MySQL MySQL;
Main ()
{
Char host[32]= "localhost";
Char user[32]= "root";
Char passwd[32]= "ABC";
Char dbname[32]= "Test";

if (Mysql_init (&mysql) = NULL)
{
Syslog (log_user| Log_info, "inital MySQL handle Errorn");
return 1;
}
if (Mysql_real_connect (&mysql,host,user,passwd,dbname,0,null,0) = NULL)
{
Syslog (log_user| Log_info, "Failed to connect to Database:error:%sn", Mysql_error (&mysql));
return 1;
}
else Syslog (log_user| Log_info, "Connect to Database:n");
Find_ps ();
Db_close ();
return 0;
}
int Db_close ()
{
Mysql_close (&mysql);
return 0;
}


int Find_ps ()
{
Mysql_row M_row;
Mysql_res *m_res;
Char sql[1024],username[32];
int Res=1;
int *id;
sprintf (SQL, "SELECT ID from username");
if (mysql_query (&mysql,sql)!= 0)
{
Syslog (log_user| Log_info, "Select Ps_info Error:%sn", Mysql_error (&mysql));
return res;
}
M_res = Mysql_store_result (&mysql);
if (m_res==null)
{
Syslog (log_user| Log_info, "Select username Error:%sn", Mysql_error (&mysql));
res = 3;
return res;
}
if (M_row = Mysql_fetch_row (m_res))
{
printf ("M_row=%dn", Atoi (m_row[0));
res = 0;
}
Mysql_free_result (M_res);
return res;
}

Compile command
Gcc-g Connect_db.c-l/usr/lib/mysql-lmysqlclient-lz
Attention:
If/tmp/cctgmms21.o:in function ' main ':
/TMP/CCTGMMS21.O (. text+0x11): Undefined reference to ' mysql_init '
Then the parameter increases-l/usr/lib/mysql-lmysqlclient
If
USR/LIB/MYSQL/LIBMYSQLCLIENT.A (MY_COMPRESS.O): in function ' my_uncompress ':
MY_COMPRESS.O (. Text+0xaa): Undefined reference to ' uncompress '
Then increase the-LZ parameter


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.