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