/* * A simple example client, using the embedded MySQL server library */ #include #include #include #include #include MYSQL *db_connect(const char *dbname); void db_disconnect(MYSQL *db); const char *server_groups[] = { "test_libmysqld_SERVER", "embedded", "server", NULL }; static void test_prepared_statement(MYSQL *pMysql) { MYSQL_STMT *pStmt = mysql_stmt_init(pMysql); if (pStmt) { const char* pCmd = "SET SESSION character_set_client = utf8"; int err = mysql_stmt_prepare(pStmt, pCmd, strlen(pCmd)); if (!err) err = mysql_stmt_execute(pStmt); if (err) fprintf(stderr, "%d %s\n",err,mysql_stmt_error(pStmt)); } } int main(int argc, char **argv) { MYSQL *mysql; mysql_server_init(argc, argv, (char **)server_groups); mysql = db_connect(NULL); test_prepared_statement(mysql); test_prepared_statement(mysql); test_prepared_statement(mysql); test_prepared_statement(mysql); test_prepared_statement(mysql); mysql_close(mysql); mysql_server_end(); exit(EXIT_SUCCESS); } static void die(MYSQL *db, char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); (void)putc('\n', stderr); if (db) db_disconnect(db); exit(EXIT_FAILURE); } MYSQL * db_connect(const char *dbname) { MYSQL *db = mysql_init(NULL); if (!db) die(db, "mysql_init failed: no memory"); /* * Notice that the client and server use separate group names. * This is critical, because the server will not accept the * client's options, and vice versa. */ mysql_options(db, MYSQL_READ_DEFAULT_GROUP, "test_libmysqld_CLIENT"); if (!mysql_real_connect(db, NULL, NULL, NULL, dbname, 0, NULL, 0)) die(db, "mysql_real_connect failed: %s", mysql_error(db)); return db; } void db_disconnect(MYSQL *db) { mysql_close(db); }