Hi Hitesh,
I both create a database in my Palm app and on the PC. My Quik Budget app
creates default wallets if no database exists. This gives my users a jump
start on figuring out how to use the program. An in-house app I wrote for
work is a reader only so I just create pdb files
from our database and install them.
Here is my C++ class for the PC to create PDB files. It creates them with
the "overwrite if newer" bit set which means that you can install the pdb as
many times as you want and it will delete the older version from the Palm.
Without this bit set, the Palm will keep each copy. However, having this
bit set seems to require the user to reset their device after a hotsync so
if you are only going to install the pdb once, you can set the value to
zero. You'll find the comment near the top of the constructor.
Here is an example of using the class. It also installs the pdb. If you
don't care about that you can specify any directory, forget about including
instappd.h and skip calling Install. This example just installs to the
first user it finds but you could make it more robust by giving the user the
chance to choose the user if there is more than one.
#include <Instappd.h> // From the Conduit Developer's Kit. Only necessary
if you want to auto-install the PDB
...
char PalmName[0x41];
short sSize = sizeof(PalmName);
if (!PltGetUser(0,PalmName, &sSize)) {
cerr << "No Palm users found." << endl;
exit(1);
}
char UserDirectory[0x100];
int len = sizeof(UserDirectory);
PltGetUserDirectory(PalmName,UserDirectory,&len);
PDBWriter myPDB(UserDirectory, thisDBname, thisDBtype, appType,
recordCount);
// Write as much APPINFO as you like...
myPDB << "Hello"; // Automatically appends the zero
myPDB << "World";
myPDB.PutLong(10000000);
for (i=0; i<recordCount;i++) {
myPDB.StartRecord(); // Specify a category as the first param if
you don't want 0, true as second param makes it secret
myPDB << string[i];
myPDB.PutLong(amount[i]);
}
myPDB.Install(); // File will be installed on next hotsync
...
Good luck,
Scott Maxwell
----------------------------------------------------------
Since this list rejects attachments, I will have to put the two files in the
message. Sorry.
PDBWriter.h
-----------------------------------------------------------
// PDBWriter.h: interface for the PDBWriter class.
//
//////////////////////////////////////////////////////////////////////
#if
!defined(AFX_PDBWRITER_H__70A846E0_565D_11D3_B420_0060974103BE__INCLUDED_)
#define AFX_PDBWRITER_H__70A846E0_565D_11D3_B420_0060974103BE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define SECRET 0x10
#define DIRTY 0x40
class PDBWriter
{
public:
// If recCount==0 then it will be calculated automatically, otherwise it
will be verified.
PDBWriter(CString path, CString name, unsigned long dbcode, unsigned long
appcode, int recCount);
virtual ~PDBWriter();
void Close();
void StartRecord(int category=0, bool secret=false);
void ClearTo(int pos);
void Clear(int pos);
void PutZero() { fputc(0,_file); }
void PutByte(unsigned char b) { fputc (b,_file); }
void PutWord(unsigned short w);
void PutLong(unsigned long l);
void PutString(const char *s) { fputs(s,_file); fputc(0,_file); }
void Install(char *username);
PDBWriter& operator<< (char c) { fputc(c,_file); return *this; }
PDBWriter& operator<< (short s) { PutWord(s); return *this; }
PDBWriter& operator<< (long l) { PutLong(l); return *this; }
PDBWriter& operator<< (char *s) { PutString(s); return *this; }
PDBWriter& operator<< (CString &s) { PutString(s); return *this; }
private:
CString _name, _fullpath;
int _recCount;
int _curRecord;
bool _open;
FILE * _file;
};
#endif //
!defined(AFX_PDBWRITER_H__70A846E0_565D_11D3_B420_0060974103BE__INCLUDED_)
PDBWriter.cpp
-----------------------------------------------------------
// PDBWriter.cpp: implementation of the PDBWriter class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PalmMPDExport.h"
#include "PDBWriter.h"
#include <stdio.h>
#include <iostream.h>
#include <time.h>
#include <Instappd.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
PDBWriter::PDBWriter(CString path, CString name, unsigned long dbcode,
unsigned long appcode, int recCount)
: _name(name), _recCount(recCount), _curRecord(-1), _open(true)
{
if (!path.IsEmpty() && path.Right(1) != "\\")
path += "\\";
ASSERT(name.Find('.') == -1);
_fullpath = path+name+_T(".pdb");
_file = fopen(_fullpath,"wb");
ASSERT(_file);
PutString(name);
ClearTo(0x20);
PutWord(0x10); file://Allow newer to install over existing, 0 otherwise
PutWord(0); file://Version
long sec = time(0)+60*60*24*(365*66+(66/4));
PutLong(sec);
PutLong(sec);
ClearTo(0x34);
PutLong(0x50+(recCount<<3));
ClearTo(0x3c);
PutLong(dbcode);
PutLong(appcode);
ClearTo(0x4c);
PutWord(recCount);
ClearTo(0x50+(recCount<<3));
}
PDBWriter::~PDBWriter()
{
Close();
}
void PDBWriter::Close()
{
if (_open) {
ASSERT(_curRecord+1 == _recCount);
fclose(_file);
}
_open = false;
}
void PDBWriter::Install(char *username)
{
Close();
PltRemoveInstallFile(username,_fullpath.GetBuffer(0));
PltInstallFile(username,_fullpath.GetBuffer(0));
}
void PDBWriter::ClearTo(int pos)
{
int p = ftell(_file);
ASSERT(p<=pos);
while (p++<pos)
fputc(0,_file);
}
void PDBWriter::Clear(int pos)
{
while (pos--)
fputc(0,_file);
}
void PDBWriter::StartRecord(int category, bool secret)
{
unsigned short p = ftell(_file);
_curRecord++;
if (!_curRecord) {
if (p == 0x50+(_recCount<<3)) {
fseek(_file,0x34,SEEK_SET);
PutLong(0);
}
}
fseek(_file,0x50+(_curRecord<<3),SEEK_SET);
PutWord(p);
fputc(secret ? category | SECRET : category, _file);
fputc(0,_file);
PutWord(_curRecord);
fseek(_file,p,SEEK_SET);
}
void PDBWriter::PutWord(unsigned short w)
{
fputc(w>>8,_file);
fputc(w&0xff,_file);
}
void PDBWriter::PutLong(unsigned long l)
{
fputc((l>>24)&0xff,_file);
fputc((l>>16)&0xff,_file);
fputc((l>>8)&0xff,_file);
fputc(l&0xff,_file);
}