2009-02-12 123 views
0

MCPResult * theResult; MCPConnection * mySQLConnection;使用Cocos2d與我的iPhone應用程序建立遠程數據庫連接

//initialize connection string vars 

NSString *dbURL = @"192.168.0.16"; 

NSString *userName = @""; 

NSString *pass = @""; 
int port = 3306; 

//open connection to database 
mySQLConnection = [[MCPConnection alloc] initToHost: dbURL 
              withLogin:userName 
               password:pass 
              usingPort:port]; 

if ([mySQLConnection isConnected]) 
{ 
    NSLog(@"The connection to database was successfull"); 
} 
else 
{ 
    NSLog(@"The connection to database was failed"); 
} 

//selection to database 

if([mySQLConnection selectDB:@"blackjack_DB"]) 
{ 

    NSLog(@"Database found"); 

} 

else 

{ 

    NSLog(@"Database not found"); 

} 

//selection to Table 

theResult = [mySQLConnection queryString:@"select * from test"];  
//theResult = [mySQLConnection queryString:@"select * from test where id='1'"]; 
//theResult = [mySQLConnection queryString:@"select id from test"]; 

//theResult = [mySQLConnection queryString:@"select name from test where pass='main_pass'"]; 

NSArray *m= [theResult fetchRowAsArray]; 
NSLog(@"%@", m); 
NSLog(@"%@", [m objectAtIndex:2]); 

使用此代碼連接&接收來自remotedatabase信息。 並且還使用一些框架。 AppKit.framework,Cocoa.framework,Carbon.framework,MCPKit_bundled.framework。但是,我沒有將我的應用程序與remort數據庫連接起來。

回答

0

需要更多信息,具體來說,您的iPhone的IP是什麼?我問的原因是因爲除非它的IP地址位於專用C網絡(192.168.x.x)中,否則無法找到數據庫。

我建議將dbUrl設置爲可公開訪問的IP地址。這可能需要一些工作(防火牆,瞭解你的IP地址等),但它應該工作。

此外,打印失敗連接的結果將會很有用(我認爲MCPConnection具有此功能,除了成功/失敗之外)

相關問題