MySQL in Cocoa (Obj-C)

martinibook

Aktives Mitglied
Thread Starter
Dabei seit
20.08.2005
Beiträge
8.730
Reaktionspunkte
350
Hallo,
Ich möchte gerne mit meinen Cocoa Programmen in Obj-C auf meine Datenbank zugreifen. Wie mache ich das, was brauche ich dazu?

Martin
 
Okay, ich habe jetzt ein neues Projekt mit dem Framework drin erstellt.
Wie kann ich denn jetzt MySQL abfragen stellen?

Martin
 
schon mal eine framework API doku gesehen?
 
Nein, bin ganz neu bei XCode und Cocoa

Martin
 
Ich hoffe, ich sehe das nicht falsch, aber auf der Seite geht es um MySQL allgemein oder auch in C++?

In PHP geht das ja so:
PHP:
<?PHP
// Login
$server = '127.0.0.1';
$user = 'user';
$passwd = '';
$db = 'datenbank';

$dbh = mysql_connect($server, $user, $passwd);

mysql_select_db($db, $dbh);

// Abfrage
$sql = 'SELECT * FROM table ORDER BY id';
$erg = mysql_query($sql, $dbh);
while ($l = mysql_fetch_assoc($erg))
	{

	}
?>
Dazu suche ich das entsprechende Stück in Cocoa/C++.

http://dev.mysql.com/doc/refman/5.0/en/c-api-prepared-statements.html
Ist das was passendes?

Entschuldigung, wenn ich was unorganisiert klinge, aber ich habe nahezu keine Ahnung von C++ und C++ & MySQL, aber ich habe das schon in PHP gemacht.

Martin
 
ja, das sind die funktionen, aber wie du siehst gibt es kein mysql_fetch_assoc() in der C API...
aber wenn man in die php mysql doku guckt:
mysql_fetch_assoc() entspricht in der Funktionsweise exakt dem Aufruf von mysql_fetch_array() mit Angabe von MYSQL_ASSOC als optionalen zweiten Parameter. Diese Funktion liefert Ihnen nur ein assoziatives Array. Das war das ursprüngliche Verhalten von mysql_fetch_array(). Falls Sie sowohl numerische, als auch assoziative Indizes benötigen, verwenden Sie mysql_fetch_array().

ansonsten ist das programmieren ja gleich bis auf die sprachunterschiede...
 
Also einfach Variablennamen ohne $, dein Zitat und dann läuft das?
 
Habe jetzt folgendes Programm und folgende ausgabe:
Code:
#import <Cocoa/Cocoa.h>

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

NSString *server = @"127.0.0.1";
NSString *user = @"dbuser";
NSString *passwd = @" ";

if (mysql_real_connect(*server, *user, *passwd))
	NSLog(@"Okay");




    return NSApplicationMain(argc,  (const char **) argv);
}
Code:
Vokabeltrainer has exited due to signal 11 (SIGSEGV).
[Session started at 2006-03-23 20:12:49 +0100.]
Vokabeltrainer(678) malloc: ***  Deallocation of a pointer not malloced: 0xa28e6de4; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
Vokabeltrainer(678) malloc: ***  Deallocation of a pointer not malloced: 0x4; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
Vokabeltrainer(678) malloc: ***  Deallocation of a pointer not malloced: 0xa28e6ed4; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
Vokabeltrainer(678) malloc: ***  Deallocation of a pointer not malloced: 0x3028d1; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
Vokabeltrainer(678) malloc: ***  Deallocation of a pointer not malloced: 0xa28fb858; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug

Vokabeltrainer has exited due to signal 11 (SIGSEGV).
 
es könnte helfen mal die API doku zu lesen, was steht denn da weiter unten?

Application programs should use this general outline for interacting with MySQL:

Initialize the MySQL library by calling mysql_library_init(). The library can be either the mysqlclient C client library or the mysqld embedded server library, depending on whether the application was linked with the -libmysqlclient or -libmysqld flag.

Initialize a connection handler by calling mysql_init() and connect to the server by calling mysql_real_connect().

Issue SQL statements and process their results. (The following discussion provides more information about how to do this.)

Close the connection to the MySQL server by calling mysql_close().

End use of the MySQL library by calling mysql_library_end().
 
oneOeight schrieb:
...
ansonsten ist das programmieren ja gleich bis auf die sprachunterschiede...
Ein Bonmont erster Güte, wenn ich das mal so sagen darf :D.
 
Zurück
Oben Unten