///////////////////////////////////
/* insert.c */
#include
#include
#include "/usr/local/include/mysql/mysql.h"
/*注意哦,上面必须是mysql.h的绝对地址,一般在mysql下的include目录下,仔细看看你的在哪里?*/
int main(int argc, char *argv[])
{
MYSQL my_connection;
int res;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "localhost", "root", "","test",0,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success\n");
//这是插入语句
res = mysql_query(&my_connection, "insert into student values('123456','elephant','man')");
//这是更新语句
//res = mysql_query(&my_connection, "select * from student");
if (!res)
{
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
/*里头的函数返回受表中影响的行数*/
}
else
{
//分别打印出错误代码及详细信息
fprintf(stderr, "Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
mysql_close(&my_connection);
}
else
{
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
//gcc -o insert insert.c -L /usr/local/lib/mysql/*.a -lz
注意,redh9中MySQL 没有mysql.h
所以要加:mysql-3[1].23.58.tar.gz 包
#./insert
......
`t conncet to local MySQL server through socket '/tmp/mysql.sock'
...
这个问题主要提示是,不能通过'/tmp/mysql.sock'连到服务器,而php标准配置正是用过'/tmp/mysql.sock',但是一些mysql安装方法将mysql.sock放在/var/lib/mysql.sock或者其他的什么地方,你可以通过修改/etc/my.cnf文件来修正它,打开文件,可以看到如下的东东:
[mysqld]
socket=/var/lib/mysql.sock
改一下就好了,但也会引起其他的问题,如mysql程序连不上了,再加一点:
[mysql]
socket=/tmp/mysql.sock
或者用这样的方法:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
最后再次执行就OK了....
阅读(1677) | 评论(0) | 转发(0) |