Hi,
I have been trying to store a character string into the database w/ no
avail. It seems like such an important thing for any palm application: to
store a char* into the database and be able to retrieve it. I have search
countless references on the web and havent found something that can help me.
I have been storing a char* string from user input, and writing it
sucessfully into a database. When i read from the database i can retrieve
the char* i saved. But the problem arises when i exit the program and start
the program again and try to access the same char* in the database. The
char* isnt there anymore and is replaced w/ weird random characters or parts
of character strings from else where in my program.
Why is the database not storing my char* when i exit and restart my
application? Is it because I store the char* information in a MemHandle and
the palm os automatically free's all MemHandle when the application exits?
Here are some code that simulates what i am doing...
typedef struct{
char* str;
}report;
//Global variable r
report newReport;
typdef report * ReportDBptr;
void initreport(){
newReport.str = MemHandleLock(MemHandleNew(20));
StrCopy(newReport.str, "no data yet");
}
//assume that one of my functions saves user input data into the char* by
way of StrCopy.
//ie (StrCopy(newReport.str, newstr))
//function that saves my global variable into a database.
Err savedata(){
DmOpenRef gDB;
LocalID dbID;
Err err = 0;
ReportDBptr reportdbptr;
MemHandle recordH;
UInt16 index;
UInt32 offset;
//a function i wrote to create and retrive the correct LocalID for my
database.
dbID = GetCreateDBID(ReportDBName, ReportDBType);
gDB = DmOpenDatabase(0, dbID, dmModeReadWrite);
if (!gDB) return DmGetLastErr();
recordH = DmNewHandle(gDB, sizeof(newReport));
reportdbptr = MemHandleLock(recordH);
DmWrite(reportdbptr, 0, &newReport, sizeof(newReport));
index = 0;
MemHandleUnlock(recordH);
err = DmAttachRecord(gDB, &index, recordH, NULL);
if (err) MemHandleFree(recordH);
err = DmCloseDatabase(gDB);
return err;
}
void retrievedata(){
ControlPtr pushbutton;
DmOpenRef gDB;
LocalID dbID;
ReportDBptr reportdbptr;
MemHandle memH;
char* label = "";
int i;
dbID = GetCreateDBID(ReportDBName, ReportDBType);
gDB = DmOpenDatabase(0, dbID, dmModeReadWrite);
//get a controlptr to the pushbutton
pushbutton = FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, pushbuttonid));
//Retrieve a ptr to the data i saved in the database
memH = DmQueryRecord(gDB, i);
reportdbptr = MemHandleLock(memH);
label = (char*)CtlGetLabel(pushbutton);
//copy the char* in the database to the pushbutton's label.
StrCopy(label, (const char*)reportdbptr->Title);
CtlSetLabel(pushbutton, label);
MemHandleUnlock(memH);
DmCloseDatabase(gDB);
}
My retrievedata function works fine and my label shows the correct char* i
saved in the database.
When i exit my program and re-enter, I can no longer retrieve that same
data. (any UInt16 variables that i saved is still retrievable however)
Why is this?
I hope someone can shed light to this problem that has been buggin me.
Thank you.
Ray
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/