// TestApp.cpp // Test application for learning SQLite // Created 1/14/08 by Kyle Oliver // Last revised 2/18/08 #include #include #include #include "sqlite3.h" // (Your linker also needs to be able to access the SQLite library.) using namespace std; /** * A callback function used for querying the database. This function is provided * in the SQLite documentation. * * @param NotUsed arbitrary pointer that gets passed from sqlite3_exec() * @param argc * @param argv * @param azColName */ static int callback(void *NotUsed, int argc, char **argv, char **azColName) { int i; for(i=0; i pairs; pairs[closedClub] = ccNum; pairs[newerClub] = ncNum; pairs[badClub] = bcNum; pairs[farClub] = fcNum; // Because you can't just put a blob into a text statement to pass into the // database, you need to prepare a compiled INSERT statement. Let's do this // for each item in the map. const char* currClub; int* currNum; sqlite3_stmt* compdStmt; int rc; while (!pairs.empty()) { currClub = (*pairs.begin()).first; currNum = (*pairs.begin()).second; // The first step is to write an SQL statement with wildcards. const char* stmtTxt = "INSERT INTO ClubNums(bus, num) VALUES(?,?)"; // Then you compile the SQL command into an sqlite3 statment. rc = sqlite3_prepare(testDB, stmtTxt, -1, &compdStmt, 0); if(rc != SQLITE_OK) { cout << sqlite3_errmsg(testDB) << endl; exit(1); } // Now bind text and a blob, repectively, to the wildcards in the compiled // statement. sqlite3_bind_text(compdStmt, 1, currClub, -1, SQLITE_STATIC); sqlite3_bind_blob(compdStmt, 2, currNum, // Fixed bug caught by Rob Kennedy (currNum[0] + 1) * sizeof(int), SQLITE_STATIC); // Now run the virtual machine on this statement by calling sqlite3_step(). sqlite3_step(compdStmt); // Remove the pair and reset the virtual machine. pairs.erase(pairs.begin()); sqlite3_reset(compdStmt); } // Finalize the virtual machine to release its memory. sqlite3_finalize(compdStmt); /////////////////////////// Query the table with blobs /////////////////////// // Create an SQL statment for a query on the database. const char* searchArg = "SELECT num FROM ClubNums WHERE bus = ?"; // Compile the SQL command into an sqlite3 statment. rc = sqlite3_prepare(testDB, searchArg, -1, &compdStmt, 0); if (rc != SQLITE_OK) { cout << sqlite3_errmsg(testDB) << endl; exit(1); } // Decide which number we want to search for and bind it to the SQL variable. const char* getThisNum = "Shank Hall"; sqlite3_bind_text(compdStmt, 1, getThisNum, -1, SQLITE_STATIC); // Execute the query. int* numArr; rc = sqlite3_step(compdStmt); if (SQLITE_ROW == rc) { // Get the size of the blob. int bytes = sqlite3_column_bytes(compdStmt, 0); // Allocate some memory and copy the blob. numArr = (int*) malloc(bytes); memcpy(numArr, sqlite3_column_blob(compdStmt, 0), bytes); // Release the resources from the compiled statement. sqlite3_finalize(compdStmt); } // Get the number of digits in the number. int digs = numArr[0]; // Print 'em. cout << "Phone number for " << getThisNum << ": "; for (int i = 1; i <= digs; i++) { cout << numArr[i]; } cout << endl; //////////////////////////// Close the database ///////////////////////////// // Close the database. sqlite3_close(testDB); }