2016-04-11 18 views
0

私はCプログラムをMysqlに接続しようとしています。C Mysql接続ハンドラ

Iは主部(私はmysql.hヘッダが含まれている)

MYSQL  *conn; 
MYSQL_RES *res; 
MYSQL_ROW row; 

int main (int argc, char *argv[]) 
{ 

    conn = my_init(); 
    mysqll_real_connect(conn,"localhost","user","pass","database",0,NULL,0); 

このコードを持っており、私が接続

mysql_query(conn,"show tables"); 
    res = mysql_store_result(conn); 
    while (row = mysql_fetch_row(res)) 
    { 
      fprintf (stdout,"%s\n", row[0]); 
    } 
    fprintf (stdout,"\n%lu rows affected\n", (unsigned long) 
    mysql_num_rows(res)); 

問題であることを呼び出すメイン外部機能を有しますこの方法でコードをコンパイルすると

gcc -D__DEBUG__=0 -Wall test.c -lmysql -o test $(mysql_config --libs --include --cflags) 

私はこのメッセージを受け取ります

test.c:162:14: error: ‘conn’ undeclared (first use in this function) 
test.c:162:14: note: each undeclared identifier is reported only once  
for each function it appears in 
test.c:163:6: error: ‘res’ undeclared (first use in this function) 
test.c:164:10: error: ‘row’ undeclared (first use in this function) 

したがって、1つの接続しか持たずに別の機能に再利用することはできますか?

メイン出口では、mysql_close(conn)を実行します。あなたは、共通のヘッダファイルを作成し、test.cの中で、このファイルを含める必要があり

おかげ

+0

変数はconnのは、RESおよび行が正しく定義されていますか?彼らはグローバルですか? –

答えて

0

:例

#ifndef _MAIN_H 
#define _MAIN_H 1 
#include <mysql/mysql.h> 
extern MYSQL  *conn; 
extern MYSQL_RES *res; 
extern MYSQL_ROW row; 
#endif 
+0

ありがとう –