Home  >  Article  >  Database  >  Ubuntu下的c++连接数据库

Ubuntu下的c++连接数据库

WBOY
WBOYOriginal
2016-06-07 16:53:121160browse

第一步:准备工作gcc,装完之后,还要装一些类库,要不会出很多错误sudo apt-get install g++,glibc,glibc-source,build-essentia

第一步:准备工作
gcc,装完之后,,还要装一些类库,要不会出很多错误

sudo apt-get install g++,glibc,glibc-source,build-essential,libmysql++dev,libmysqlclient15off,libmysqlclient15-dev

上面这些东西,我也不知道哪些真正有用,反正这些我都给装上了

第二步:
示例程序(别人的)

#include
#include
#include
#include

int main(){
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char query[1024];
int t,r;

mysql=mysql_init(NULL);
if (!mysql_real_connect(mysql,"test","root",
"123","test",0,NULL,0))
{
printf( "Error connecting to database: %s\n",mysql_error(mysql));
}
else printf("Connected...\n");

sprintf(query,"select * from first");

t=mysql_query(mysql,query);

if (t)
{
printf("Error making query: %s\n",
mysql_error(mysql));
}
else printf("Query made...\n");
res=mysql_use_result(mysql);
for(r=0;rrow=mysql_fetch_row(res);
if(rowfor(t=0;tprintf("%s",row[t]);
printf("\n");
}
mysql_close(mysql);
return 0;
}


然后gcc test.c -lmysqlclient

另外#include 这里也可能会出现问题,如果需要可以自己修改成绝对路径,再试一下子。

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn