|

楼主 |
发表于 2004-9-29 20:18:48
|
显示全部楼层
多谢兄弟指教,回复晚了,多多见谅。
最初由 alen 发表
代码有多处错误,建议查看mysql的C API:
1.mysql_init (mysql)应写为
mysql=mysql_init(NULL);
//变量命名有些奇怪,不如声明为MYSQL *conn;
在mysql的手册中,mysql_init函数的用法如下:
MYSQL *mysql_init(MYSQL *mysql)
使用MYSQL类型的指针初始化应该没有问题,我另外也编写了终端的程序使用mysql_init (mysql)来初始化连接,之前定义 MYSQL *mysql,是可以查询到数据库的,在mysql的手册中有如下说明:
Allocates or initializes a MYSQL object suitable for mysql_real_connect(). If mysql is a NULL pointer, the function allocates, initializes, and returns a new object. Otherwise, the object is initialized and the address of the object is returned. If mysql_init() allocates a new object, it will be freed when mysql_close() is called to close the connection.
我使用NULL来初始化连接,也是可以查询到数据库的,如上说明,若参数为NULL,则分配内存及初始化,若参数不是NULL,即是已经分配了MYSQL类型的变量作为参数,则初始化该变量。
2.mysql = mysql_real_connect (mysql, hostname, username, password, database, 0, 0, 0);
参数类型应该有不匹配,之所以没有报错,是因为你将整数0赋给了一个指针!!
MYSQL *
mysql_real_connect(MYSQL *conn,char *host_name,char *user_name,
char *user_name,char *password,char *db_name,
unsigned int port_num,char *socket_name,
unsigned int flags);
(3.22以前参数列表中没有char *db_name,实际应用中需要判断版本)
MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)
# If port is not 0, the value will be used as the port number for the TCP/IP connection. Note that the host parameter determines the type of the connection.
# If unix_socket is not NULL, the string specifies the socket or named pipe that should be used. Note that the host parameter determines the type of the connection.
# The value of client_flag is usually 0
以上是mysql手册的内容,仔细看来,我使用的参数并没有错,但可能是我经验不足,还就兄弟们解惑。另外,我写的另外的只在终端查询的程序,使用mysql = mysql_real_connect (mysql, hostname, username, password, database, 0, 0, 0);语句并没有出错,可以查询数据库。
还有3.22之前的版本没有char *db_name参数,这点多谢兄弟提醒,我刚使用Mysql,没有这个版本之前的使用经验,多谢!若是3.22版本之前,那应该使用什么函数连接指定的数据库呢?是不是使用下面这个函数:
int mysql_select_db(MYSQL *mysql, const char *db)
3.row = mysql_fetch_row (query_result);//没有进行异常处理!!!
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
label = gtk_label_new (row[0]);
button = gtk_button_new_with_label ("退出");
box = gtk_vbox_new (TRUE, 5);
以为是一个测试的练习程序,也出现许多错误,真是惭愧!
4.“我是直接取查询到的MYSQL_ROW类型变量row作为label = gtk_label_new (row[0]);传递给GTK函数的,这样是不是不行?"
label = gtk_label_new (row[0]);你觉得会有问题吗?!!!类型是相同的!!!
其实这些问题,你自己是完全可以找到并排除的!!!代码很简单但感觉比较乱,让人没有兴趣读下去,多看些资料,会好!!!
我一直以为是在这个语句上出的问题,那是因为用GDB单步运行到这句时程序就接收到SIGSEGV信号退出了,兄弟的话让我排除了一个错误的想法。 |
|