The C API in Practice
What you have seen so far will take you a long
way. If you want to understand what is going on in more detail,
understand proper error handling, or work with binary data, however,
we need to drop down a level and look at the API in a practical
example. To these ends, we will look to an API that provides callers
with access to delayed stock quotes stored in a MySQL database. The
application leverages a single MySQL table, Stock
,
with the following schema:
CREATE TABLE Stock ( symbol CHAR(5) NOT NULL PRIMARY KEY, openPrice REAL NOT NULL, currPrice REAL NOT NULL, high52 REAL NOT NULL, low52 REAL NOT NULL );
Our library needs two basic functions:
-
void assign_stock(Stock *stock);
Assigns the values in the
Stock
structure to the database-
Stock *get_stock(char *symbol);
Retrieves the
Stock
structure for the specified stock
These functions naturally depend on a struct that mirrors the database schema for the stock quotes.
Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.