安装mysql
sudo apt-get install mysql-server mysql-client
再装开发包
代码:
sudo apt-get install libmysqlclient15-dev
可以用以下代码测试一下
运行环境 gcc (Ubuntu 4.4.1-4ubuntu8) 4.4.1
代码:/*connect.c*/
/* Simple C program that connects to MySQL Database server*/
#include
#include
#include
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "123456"; /* 此处改成你的密码 */
char *database = "mysql";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
caojiangfeng@caojiangfeng-laptop:$gcc connect.c -o connect -lmysqlclient
caojiangfeng@caojiangfeng-laptop:$./connect
MySQL Tables in mysql database:
columns_priv
db
event
func
general_log
help_category
help_keyword
help_relation
help_topic
host
ndb_binlog_index
plugin
proc
procs_priv
servers
slow_log
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user
阅读(1140) | 评论(0) | 转发(0) |