|
- #include <stdlib.h>
- #include <stdio.h>
- #include "mysql.h"
- MYSQL my_connection;
- MYSQL_RES *res_ptr;
- MYSQL_ROW sqlrow;
- int main(int argc, char *argv[]) {
- int res;
- mysql_init(&my_connection);
- if (mysql_real_connect(&my_connection,"localhost","rick","bar", "rick", 0, NULL, 0)) {
- printf("Connection success\n");
- res = mysql_query(&my_connection, "SELECT childno, fname,age FROM children WHERE age > 5");
- if (res) {
- printf("SELECT error: %s\n", mysql_error(&my_connection));
- } else {
- res_ptr = mysql_store_result(&my_connection);
- if (res_ptr) {
- printf("Retrieved %luows\n",(unsigned long)mysql_num_rows(res_ptr));
- while ((sqlrow = mysql_fetch_row(res_ptr))) {
- printf("Fetched data...\n");
- }
- if (mysql_errno(&my_connection)) {
- fprintf(stderr, "Retrive error: s\n",mysql_error(&my_connection));
- }
- }
- mysql_free_result(res_ptr);
- }
- 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行 #include "mysql.h"
编译时提示不存在
然后改为 #include "mysql/mysql.h"
编译时提示如下:
- /tmp/cccsfBE0.o: In function `main':
- /tmp/cccsfBE0.o(.text+0xf): undefined reference to `mysql_init'
- /tmp/cccsfBE0.o(.text+0x36): undefined reference to `mysql_real_connect'
- /tmp/cccsfBE0.o(.text+0x65): undefined reference to `mysql_query'
- /tmp/cccsfBE0.o(.text+0x83): undefined reference to `mysql_error'
- /tmp/cccsfBE0.o(.text+0xa9): undefined reference to `mysql_store_result'
- /tmp/cccsfBE0.o(.text+0xd1): undefined reference to `mysql_num_rows'
- /tmp/cccsfBE0.o(.text+0xf6): undefined reference to `mysql_fetch_row'
- /tmp/cccsfBE0.o(.text+0x12d): undefined reference to `mysql_errno'
- /tmp/cccsfBE0.o(.text+0x146): undefined reference to `mysql_error'
- /tmp/cccsfBE0.o(.text+0x16d): undefined reference to `mysql_free_result'
- /tmp/cccsfBE0.o(.text+0x17d): undefined reference to `mysql_close'
- /tmp/cccsfBE0.o(.text+0x1a7): undefined reference to `mysql_errno'
- /tmp/cccsfBE0.o(.text+0x1bd): undefined reference to `mysql_error'
- /tmp/cccsfBE0.o(.text+0x1d0): undefined reference to `mysql_errno'
- collect2: ld returned 1 exit status
复制代码
请问如何连接?
mysql_real_connect(&my_connection,"localhost","rick","bar", "rick", 0, NULL, 0)中各个参数是什么意思啊? |
|