关于mysql_real_connect(mysql,"192.168.1.68"
这是我的代码:
- #include <mysql.h>;
- #include <stdio.h>;
- int main(){
- MYSQL mysql; // need a instance to init
- MYSQL_RES *res;
- MYSQL_ROW row;
- char *query;
- int t,r;
- // connect the database
- mysql_init(&mysql);
- if (!mysql_real_connect(&mysql,"192.168.1.68", "root", "mm128133", "chen",0,NULL,0))
- {
- printf( "Error connecting to database: %s\n",mysql_error(&mysql));
- }
- else printf("Connected...\n");
- // get the result from the executing select query
- query = "select * from chen_table";
- t = mysql_real_query(&mysql,query,(unsigned int) strlen(query));
- if (t)
- {
- printf("Error making query: %s\n",
- mysql_error(&mysql));
- }
- else printf("[%s] made...\n", query);
- res = mysql_store_result(&mysql);
- while(row = mysql_fetch_row(res))
- {
- for(t=0;t<mysql_num_fields(res);t++)
- {
- printf("%s ",row[t]);
- }
- printf("\n");
- }
- printf("mysql_free_result...\n");
- mysql_free_result(res); //free result after you get the result
- sleep(1);
- // execute the insert query
- query = "insert into chen_table(id, name) values(3, 'kunp')";
- t = mysql_real_query(&mysql,query,(unsigned int) strlen(query));
- if (t)
- {
- printf("Error making query: %s\n",
- mysql_error(&mysql));
- }
- else printf("[%s] made...\n", query);
- mysql_close(&mysql);
- return 0;
- }
- 用gcc编译也成功了,
- gcc -o text mysqltest.c -I/usr/include/mysql -L/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz
- mysql_real_connect(&mysql,"192.168.1.68", "root", "mm128133", "chen",0,NULL,0))
- 其中192.168.1.68是我的内网的IP地址,如果改成localhost,就可以连到数据库中,但是用这个IP的话就会出现这样的问题:
- Error connecting to database: Access denied for user: 'root@vicp.net' (Using password: YES)
- 不知道哪位大侠能帮帮小弟阿,好急阿。
复制代码
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论