today on the Internet to find some information about the C language connection MySQL, now do a summary.
First of all, your own computer to install MySQL, the specific practice is to open the terminal ("Ctrl + Alt + t"), and then enter the command "sudo apt-get install MySQL", and then double-click the TAB key 2 To view information about MySQL in your current software source.
[Email protected]:~$ sudo apt-get install mysqlmysql-client mysql-mmm-monitor mysql-source-5.6mysql-client-5.5 mysql-mmm-tools mysqltclmysql-client-5.6 mysql-proxy mysql-testsuitemysql-client-core-5.5 mysql-server mysql-testsuite-5.5mysql-client-core-5.6 mysql-server-5.5 mysql-testsuite-5.6mysql-common mysql-server-5.6 mysqltunermysql-common-5.6 mysql-server-core-5.5 mysql-utilitiesmysql-mmm-agent mysql-server-core-5.6 Mysql-workbenchmysql-mmm-common mysql-source-5.5 mysql-workbench-data[email protected]:~$ sudo apt-get Install MySQL
then choose to install MySQL server and MySQL client
This is not enough, we also need to install some more files, enter the command "
sudo apt-get install Libmysql", and then double-click the
Tab key 2 , the purpose is to view their current software source in the information about MySQL.
[Email protected]:~$ sudo apt-get install libmysqllibmysql++3 libmysqlcppconn7 libmysql++- Doclibmysql6.4-cil libmysqlcppconn-dev libmysqld-piclibmysql-cil-dev libmysqld-dev libmysql-javalibmysqlclient18 Libmysql++-dev libmysql-ocamllibmysqlclient-dev Libmysql-diff-perl libmysql-ocaml-dev[email protected]:~$ sudo apt-get install Libmysql
then we choose to install the libmysqlclient18 (choose Install Libmysqlclient-dev also can)
[Email protected]:~$ sudo apt-get install libmysqlclient18 [sudo] password for Zhiniaobu:
By now, the preparatory work has been completed. Why do I like to double-press 2 tab, because some methods of online query is not suitable for themselves, because of their own operating system, software sources of software may be different from the author of the web and data too old external factors, so we have to combine their actual situation to operate, The TAB key automatically complements the command to help us see the list we need.
Then write a simple C language program to connect to MySQL (if you don't have a compiler, you can install it yourself).
#include <stdio.h> #include <mysql.h>int main (void) { MySQL *conn_ptr = mysql_init (NULL);//Initialize MySQL structure if (!conn_ptr) { printf ("Allocation failed \ n"); return 0; } host IP user password to connect the database name conn_ptr = Mysql_real_connect (conn_ptr, "localhost", "root", "MySQL", "Ceshi", 0,null,0); if (conn_ptr) printf ("Connection succeeded \ n"); else printf ("Connection failed \ n"); Mysql_close (conn_ptr); return 0;}
[Email protected]:~$ cd zhuo_mian/[email protected]:~/zhuo_mian$ LSC language connection mysql.c MySQL photoshop[email protected]:~/zhuo_mian$ gcc-i/usr/include/mysql C language Connection mysql.c-l/usr/lib/mysql-l mysqlclient[email Protected]:~/Zhuo _mian$ lsa.out C language Connection mysql.c MySQL photoshop[email protected]:~/zhuo_mian$./a.out connection successful [email protected] : ~/zhuo_mian$
PS:
- My MySQL user's name is "root", the password is "MySQL", there is a database called "Ceshi".
- Since the program contains "mysql.h" files, you need to add something when compiling.