September 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Adding Records to an Open Database in Windows CE

  • March 29, 2004
  • By Nancy Nicolaisen
  • Send Email »
  • More Articles »

Positioning for Data Access

With the exception of iterative reading through a database opened with the CEDB_AUTOINCREMENT flag, there isn't really a "current record" construct that automatically operates on CE record access. As is the case with most of the rest of life, if you want to be somewhere, you have to exert a bit of effort to get there. Specifically, with respect CE databases, you must seek to a record before you can operate on it using CeSeekDatabase().

//seek to the end & write 
// the database PROPVAL array
CeSeekDatabase(globalHDB, 
               CEDB_SEEK_END,
               1, 
               &dwIndex );

The parameters to CeSeekDatabase(), in the order shown, are the handle to the database; a flag indicating the type of seek to perform; a value whose meaning depends on the seek type (in this case it is the number of records to seek beyond the end of the database); and the address of a variable that holds the index of the new position on a successful return. Seeking is fairly simple if you want to add a record to the beginning or end of the database, and more complex if you want to find a specific record based on the value of a sorted field. For now, we'll let this example stand on its own. We investigate the use of CeSeekDatabase() in greater detail in the next example program, BetterBirthdays.

All that remains is to write the record to the database, clean up after ourselves, and go home.

//set oid parm to 0 to create a new rec
CeWriteRecordProps(globalHDB, 0,
                   NUMBER_BIRTHDAY_PROPS, 
                   pRecord);
if( GetLastError() == ERROR_INVALID_PARAMETER )
     { goto FAIL;}
if( GetLastError() == ERROR_DISK_FULL )
     { goto FAIL;}

First, we write the record, using a call to CeWriteRecordProps(). The parameters are, in the order shown, the handle to the database, the CEOID of the record to write, the number of CEPROPVAL structures in the array we are passing, and a pointer to the block we allocated to hold the array plus additional data storage. Notice that in this case we pass a value of 0 for the record's CEOID. This causes a new record to be created and added to the database. CeWriteRecordProps() returns the CEOID for the new record if the call is successful. To test for success or failure, we call GetLastError(). While you are learning the CE database moves, routinely testing for a status of ERROR_INVALID_PARAMETER is a useful debugging tool, so we show that here.

Finally, we free all the allocations we made to build the record data structures:

//free the cepropval struct array
LocalFree( pRecord );

//free the name buffer
if(lpwszWho)
{
LocalFree( lpwszWho );
}

When the dialog box receives an IDOK or IDCANCEL message, we close the database like this:

if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
    CloseHandle(globalHDB);
    EndDialog(hDlg, LOWORD(wParam));
    return TRUE;
}

Notice that we close the database by using its handle, not its CEOID.

Reviewing the Birthday Reminder Example

At this point, you've seen how to:

  • Create a simple a database using CeCreateDatabase()
  • Open a database without using an active sort order using CeOpenDatabase()
  • Allocate and initialize an array of CEPROPVAL structures to create a record
  • Seek to the end of the database using CeSeekDatabase()
  • Add a record using CeWriteRecordProps()
  • Close a database using CloseHandle()
  • Test for error status using GetLastError()

But wait, there's more...,and we'll see about that in the next example program, BetterBirthdays.

About the Author

Nancy Nicolaisen is a software engineer who has designed and implemented highly modular Windows CE products that include features such as full remote diagnostics, CE-side data compression, dynamically constructed user interface, automatic screen size detection, and entry time data validation.

In addition to writing for Developer.com, she has written several books, including Making Win 32 Applications Mobile.

# # #





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel