C Language Connection MySQL database

Source: Internet
Author: User

Operating system: WIN7/64

Compiling software: VS2010

Database: 5.7.11

Connecting the MySQL database from the C language consists of two steps:

1 initializing the connection handle structure

2 actually creating the connection

Test Code 1:

#include"stdafx.h"#include<WinSock2.h>/*socket communication, System header file*/#include<windows.h>#include<stdio.h>#include"mysql.h"#pragmaComment (lib, "Libmysql.lib")intMain () { MYSQL *Conn; Char*server ="localhost"; Char*user ="Root"; Char*password ="Root";//The password you set for "******"    Char*database ="Hyx"; Conn = Mysql_init (NULL); if(!mysql_real_connect (conn, server, user, password, database,0Null0)) printf ("Connection failed:%s", MYSQL_ERROR (conn)); Elseprintf ("Connection Successful! ");    GetChar (); return 0;}

Test result 1:

Problems encountered:

Error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
Error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
Error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main
Error LNK2019: unresolved external symbol [email protected], the symbol is referenced in function _main

Workaround:

The reason for this is that the system I use is Win7x64,mysql 64-bit Lib is also a 64-bit interface. So the workaround is as follows:
. Project, properties, Configuration Manager
Active solution platform, dropdown new, a new check box appears, select X64 in the type select New platform

Recompile successful ~ ~ ~

Test Code 2:

#include "stdafx.h" #include <WinSock2.h>/*socket Communication, System header file */#include <windows.h> #include <stdio.h> #include "mysql.h" #pragma comment (lib, "Libmysql.lib") int main () {    mysql my_connection;    char *server = "localhost";    Char *user = "root";    Char *password = "root";//"******" for you set the password    char *database = "Hyx";    Mysql_init (&my_connection);    if (!mysql_real_connect (&my_connection, server, user, password, database, 0, NULL, 0))        printf ("Connection failed:%sn", MySQL _error (&my_connection));    else        printf ("Connection succeeded! ");    GetChar ();    return 0;}

Test Result 2:

Question: The test code 1 and the test code 2 only the two lines marked black, but a connection was successful, a connection failed, what is the reason?

vs shortcut Keys
F12 Go To Definition
CTRL + "-" Return from definition
F7 Compile
Ctrl+f5 Run
F5 Execute to Next Breakpoint
F10 Debugging (Step-by-step)
F11 Debug (statement-by-sentence)



C Language Connection MySQL database

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.