mysql安装连接测试c_Ubuntu 12.04下安装mysql并用C程序测试连接
1.先安裝mysql 代碼:sudo apt-get install mysql-server mysql-client 2.再裝開發包 代碼:sudo apt-get install libmysqlclient-dev
啟動mysql服務:
1.net start mysql 這是在DOS環境下采用的命令,在linux中沒有這個命令.
2.?/etc/init.d/mysql restart 這是linux下的服務啟動命令,我的機子上已經啟動本服務,運行命令的結果顯示如下
上面顯示已經開啟服務,還給出了相應進程號.
3.測試連接,編輯conn_mysql.c文件:
代碼:
#include
#include
#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", "xtaywgqt","c_test",0,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success\n");
res = mysql_query(&my_connection, "insert into user_info values(3,'Li','f')");
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;
}
4.編譯方法:
gcc $(mysql_config --cflags) conn_mysql.c -o conn_mysql $(mysql_config --libs)
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的mysql安装连接测试c_Ubuntu 12.04下安装mysql并用C程序测试连接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这样设置就可远程维护路由器了-电脑如何远
- 下一篇: iosetup mysql_InnoDB