Example #1
0
void *Mysql_create(char *host, char *user, char *pass, char *db) {
	MYSQL *con = mysql_init(NULL);

	if (con == NULL) {
		fprintf(stderr, "%s\n", mysql_error(con));
		exit(1);
	}

	Mysql proto = { .host = host, .user = user, .pass = pass, .db = db, .con =
			con, .init = Mysql_init, .query = Mysql_query, .printResults =
			Mysql_results, .close = Mysql_close };

	Mysql *m = calloc(1, sizeof(Mysql));
	*m = proto;

	if (mysql_real_connect(m->con, host, user, pass, db, 0, NULL, 0) == NULL) {
		finish_with_error(m);
	}

	if (!m->init(m)) {
		// looks like it didn't initialize properly
		m->close(m);
		return NULL;
	} else {
		// all done, we made an object of any type
		return m;
	}

}