C语言与数据库连接

MYSQL*stumysql;
MYSQL_RES*stures;
MYSQL_ROW row;
连接数据库:
stumysql=mysql_init(NULL)
mysql_real_connect(stumysql,"localhost","root","123456","student",0,NULL,0)
读出数据库数据:
mysql_query(stumysql,"select * from stu_info order by num")
stures=mysql_store_result(stumysql);
i=mysql_num_rows(stures);   数据库的行数
row=mysql_fetch_row(stures);
p1->num=atoi(row[0]);
strncpy(p1->name,row[1],20);
p1->age=atoi(row[2]);
strncpy(p1->sex,row[3],10);
strncpy(p1->jiguan,row[4],30)
mysql_close(stumysql);
存入数据库:
mysql_query(stumysql,"TRUNCATE TABLE stu_info");
for(j=0;j<n;j++)
{
 sprintf(query,"insert into stu_info(num,name,age,sex,jiguan)values('%d','%s','%d','%s','%s')",p->num,p->name,p->age,p->sex,p->jiguan);
 mysql_query(stumysql,query);
 p=p->next;
}

你可能感兴趣的:(c,数据库,mysql,query,insert,语言)