c语言操作mysql

Ubuntu自带mysql 所以不用安装。

登陆命令:

mysql -uroot –p

然后根据提示输入密码

 

FAQ:

1.      ERROR 1045 (28000): Accessdenied for user 'nsfocus'@'localhost'

解决办法:

# sudo /etc/init.d/mysqlstop
# sudo mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';   
mysql> FLUSH PRIVILEGES;
mysql> quit

#/etc/init.d/mysqld restart
# mysql -uroot -p
Enter password:
输入上面更改的新密码  如上是newpassword

mysql>

搞定!

 

Mysql命令大全

http://see.xidian.edu.cn/cpp/u/mysql_ml/

http://www.php100.com/html/webkaifa/database/Mysql/2009/0910/3288.html

 

MySQLC语言API接口

http://see.xidian.edu.cn/cpp/html/775.html

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>  

//定义数据库操作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "select * from result"
#define INSERT_QUERY "insert into result values(null, \"%s\", \"%s\", %d)"

int main(int argc, char **argv) //char **argv 相当于 char *argv[]
{
	MYSQL mysql,*sock;    //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
	MYSQL_RES *res;       //查询结果集,结构类型
	MYSQL_FIELD *fd ;     //包含字段信息的结构
	MYSQL_ROW row ;       //存放一行查询结果的字符串数组
	char  qbuf[160];      //存放查询sql语句字符串
	int num_row = 0;
	int num_col = 0;
	int i = 0;

	//初始化
	mysql_init(&mysql);

	// 连接数据库
	if (!(sock = mysql_real_connect(&mysql,"localhost","root","root","sping",0,NULL,0))) 
	{
		fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
		perror("");
		exit(1);
	}
/*
//插入
	sprintf(qbuf,INSERT_QUERY, "123,234,23,12", "www.163.com", 0);
	printf("%s\n", "insert into result values(null, \"123.123.23.23\", \"www.16.com\", 0)");
	if(mysql_query(sock, qbuf))
	{
		fprintf(stderr,"Query failed (%s)\n",mysql_error(sock));
		exit(1);
	}
*/

	memset(qbuf, 0, sizeof(qbuf));
	sprintf(qbuf,SELECT_QUERY);
	//查询
	if(mysql_query(sock,qbuf)) 
	{
		fprintf(stderr,"Query failed (%s)\n",mysql_error(sock));
		exit(1);
	}



	if (!(res=mysql_store_result(sock))) 
	{
		fprintf(stderr,"Couldn't get result from %s\n", mysql_error(sock));
		exit(1);
	}

	num_row = mysql_num_rows(res); /* Get the no. of row */
	num_col = mysql_num_fields(res); /* Get the no. of column */

	//得到结果长度
	printf("number of fields returned: %d\n", num_row);
	printf("num_col: %d\n", num_col);

	//显示结果
	while(row=mysql_fetch_row(res))//获取具体的数据
	{
		for(i=0;i<num_col;i++)
		{
			printf("%s\t",row[i]);
		}
		printf("\n");
	}


	mysql_free_result(res);
	mysql_close(sock);
	exit(0);

	return 0;  

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值