dbTalk Databases Forums  

UPDATE query not happy with date fields

comp.databases.ms-access comp.databases.ms-access


Discuss UPDATE query not happy with date fields in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Riley DeWiley
 
Posts: n/a

Default UPDATE query not happy with date fields - 08-19-2005 , 05:41 PM






I have an UPDATE query that is always setting 0 records. When I cut and
paste the SQL into Access and use it, it fails in the same way unless I
coerce the date fields to be '=now()', in which case it works. I never get
errors, and if I don't try to update the date fields, I always update the
expected number of rows (1). When I do date conversion using the same
conversion function, then call CRowset::Add() to add a record to the same
table, it works as expected.

I am using OLEDB/C++ against Jet 4.0.

Here are some code fragments for reading enjoyment.

Any insights appreciated.

RDeW

---------------------------------
//accessor
class CFileUpdateByNameAccessor

{

public:

void ClearRecord()

{

memset(&this->m_DriveName, 0, offsetof(CFileUpdateByNameAccessor,
m_TextType) + sizeof(m_TextType));

}

wchar_t m_DriveName[DRIVEBUFSIZE];

wchar_t m_DirName[DIRECTORYBUFSIZE];

wchar_t m_FileName[FILEBUFSIZE];

LONG m_Checksum;

LONG m_SizeLo32;

DATE m_CreationDateTime;

DATE m_LastAccessDateTime;

DATE m_ModificationDateTime;

DATE m_SSLastUpdate;

LONG m_TextType;



BEGIN_PARAM_MAP(CFileUpdateByNameAccessor)

COLUMN_ENTRY_TYPE(1, DBTYPE_DATE, m_CreationDateTime)

COLUMN_ENTRY_TYPE(2, DBTYPE_DATE, m_ModificationDateTime)

COLUMN_ENTRY_TYPE(3, DBTYPE_DATE, m_LastAccessDateTime)

COLUMN_ENTRY_TYPE(4, DBTYPE_DATE, m_SSLastUpdate)

COLUMN_ENTRY(5, m_Checksum)

COLUMN_ENTRY(6, m_SizeLo32)

COLUMN_ENTRY(7, m_TextType)

COLUMN_ENTRY(8, m_DriveName)

COLUMN_ENTRY(9, m_DirName)

COLUMN_ENTRY(10, m_FileName)


END_PARAM_MAP()

DEFINE_COMMAND_EX(CFileUpdateByNameAccessor, L"\

UPDATE Files \

SET \

CreationDateTime = (?), \

ModificationDateTime = (?), \

LastAccessDateTime = (?), \

SSLastUpdate = (?), \

Checksum = (?), \

SizeLo32 = (?), \

TextType = (?) \

WHERE \

Files.FileID in \

(SELECT Files.FileID from files, directories, drives \

where drives.name = (?) and \

directories.name = (?) and \

files.name = (?) and \

drives.driveid = directories.driveid and \

files.directoryid = directories.directoryid)")

};



///date conversion function, uses IDataConvert, returns S_OK and
reasonable-looking values

bool CMyDB::FileTimeToDBDate(const FILETIME *pFT, DATE *pD)

{

unsigned long nBytesConverted = 0;

DBSTATUS dbStatus = {0,};

assert(pFT);

assert(pD);

assert(_Module.pCvt);

/*

HRESULT DataConvert (

DBTYPE wSrcType,

DBTYPE wDstType,

ULONG cbSrcLength,

ULONG * pcbDstLength,

void * pSrc,

void * pDst,

ULONG cbDstMaxLength,

DBSTATUS dbsSrcStatus,

DBSTATUS * pdbsStatus,

BYTE bPrecision,

BYTE bScale,

DBDATACONVERT dwFlags);

*/

HRESULT hr =

_Module.pCvt->DataConvert(

DBTYPE_FILETIME, //file time goes in

DBTYPE_DATE, //date comes out

sizeof(*pFT), //size of input

&nBytesConverted, //how many bytes did we get?

(void *)pFT, //data in

(void *)pD, //data out

sizeof(*pD), //size of data out

DBSTATUS_S_OK, //magic per Status page in OLEDB docs

&dbStatus, //address to store status out

0, //don't care about precision ...

0, //...or about scale

DBDATACONVERT_DEFAULT //default conversion

);

if(FAILED(hr))

{

LogDBError(__WFILE__, __LINE__, hr);

return false;

}

assert(nBytesConverted);

assert(SUCCEEDED(dbStatus));

return true;

}



//------------------------UPDATE code, szFN has a "nice" file name here ...

wcsncpy(fun.m_DriveName, szFN, (pDir - szFN));

wcsncpy(fun.m_DirName, pDir, (pFile - pDir));

wcsncpy(fun.m_FileName, pFile, wcslen(pFile));

//here we get the file dates and times as FILETIME structs


if(!GetFileAttributesExW(szFN, GetFileExInfoStandard, &gfi))

return E_INVALIDARG;


FILETIME ft = {0,};

SYSTEMTIME st = {0,};

GetSystemTime(&st);

SystemTimeToFileTime(&st, &ft);

//convert the FILETIMEs to db DATE per fxn above

FileTimeToDBDate(&ft, &fun.m_SSLastUpdate);

FileTimeToDBDate(&gfi.ftCreationTime, &fun.m_CreationDateTime);

FileTimeToDBDate(&gfi.ftLastAccessTime, &fun.m_LastAccessDateTime);

FileTimeToDBDate(&gfi.ftLastWriteTime, &fun.m_ModificationDateTime);



fun.m_Checksum = CalcChecksum(szFN);

fun.m_SizeLo32 = gfi.nFileSizeLow;


hr = fun.Open();


//...error trap omitted, always get S_OK, but nothing updated, stepping into
code shows 0 records updated.

fun.Close();



Reply With Quote
  #2  
Old   
Riley DeWiley
 
Posts: n/a

Default Re: UPDATE query not happy with date fields - 08-19-2005 , 09:49 PM






Sorry, that is CRowset::Insert(), not Add()

RDeW

"Riley DeWiley" <riley.dewiley (AT) gmail (DOT) com> wrote

Quote:
I have an UPDATE query that is always setting 0 records. When I cut and
paste the SQL into Access and use it, it fails in the same way unless I
coerce the date fields to be '=now()', in which case it works. I never get
errors, and if I don't try to update the date fields, I always update the
expected number of rows (1). When I do date conversion using the same
conversion function, then call CRowset::Add() to add a record to the same
table, it works as expected.

I am using OLEDB/C++ against Jet 4.0.

Here are some code fragments for reading enjoyment.

Any insights appreciated.

RDeW

---------------------------------
//accessor
class CFileUpdateByNameAccessor

{

public:

void ClearRecord()

{

memset(&this->m_DriveName, 0, offsetof(CFileUpdateByNameAccessor,
m_TextType) + sizeof(m_TextType));

}

wchar_t m_DriveName[DRIVEBUFSIZE];

wchar_t m_DirName[DIRECTORYBUFSIZE];

wchar_t m_FileName[FILEBUFSIZE];

LONG m_Checksum;

LONG m_SizeLo32;

DATE m_CreationDateTime;

DATE m_LastAccessDateTime;

DATE m_ModificationDateTime;

DATE m_SSLastUpdate;

LONG m_TextType;



BEGIN_PARAM_MAP(CFileUpdateByNameAccessor)

COLUMN_ENTRY_TYPE(1, DBTYPE_DATE, m_CreationDateTime)

COLUMN_ENTRY_TYPE(2, DBTYPE_DATE, m_ModificationDateTime)

COLUMN_ENTRY_TYPE(3, DBTYPE_DATE, m_LastAccessDateTime)

COLUMN_ENTRY_TYPE(4, DBTYPE_DATE, m_SSLastUpdate)

COLUMN_ENTRY(5, m_Checksum)

COLUMN_ENTRY(6, m_SizeLo32)

COLUMN_ENTRY(7, m_TextType)

COLUMN_ENTRY(8, m_DriveName)

COLUMN_ENTRY(9, m_DirName)

COLUMN_ENTRY(10, m_FileName)


END_PARAM_MAP()

DEFINE_COMMAND_EX(CFileUpdateByNameAccessor, L"\

UPDATE Files \

SET \

CreationDateTime = (?), \

ModificationDateTime = (?), \

LastAccessDateTime = (?), \

SSLastUpdate = (?), \

Checksum = (?), \

SizeLo32 = (?), \

TextType = (?) \

WHERE \

Files.FileID in \

(SELECT Files.FileID from files, directories, drives \

where drives.name = (?) and \

directories.name = (?) and \

files.name = (?) and \

drives.driveid = directories.driveid and \

files.directoryid = directories.directoryid)")

};



///date conversion function, uses IDataConvert, returns S_OK and
reasonable-looking values

bool CMyDB::FileTimeToDBDate(const FILETIME *pFT, DATE *pD)

{

unsigned long nBytesConverted = 0;

DBSTATUS dbStatus = {0,};

assert(pFT);

assert(pD);

assert(_Module.pCvt);

/*

HRESULT DataConvert (

DBTYPE wSrcType,

DBTYPE wDstType,

ULONG cbSrcLength,

ULONG * pcbDstLength,

void * pSrc,

void * pDst,

ULONG cbDstMaxLength,

DBSTATUS dbsSrcStatus,

DBSTATUS * pdbsStatus,

BYTE bPrecision,

BYTE bScale,

DBDATACONVERT dwFlags);

*/

HRESULT hr =

_Module.pCvt->DataConvert(

DBTYPE_FILETIME, //file time goes in

DBTYPE_DATE, //date comes out

sizeof(*pFT), //size of input

&nBytesConverted, //how many bytes did we get?

(void *)pFT, //data in

(void *)pD, //data out

sizeof(*pD), //size of data out

DBSTATUS_S_OK, //magic per Status page in OLEDB docs

&dbStatus, //address to store status out

0, //don't care about precision ...

0, //...or about scale

DBDATACONVERT_DEFAULT //default conversion

);

if(FAILED(hr))

{

LogDBError(__WFILE__, __LINE__, hr);

return false;

}

assert(nBytesConverted);

assert(SUCCEEDED(dbStatus));

return true;

}



//------------------------UPDATE code, szFN has a "nice" file name here
...

wcsncpy(fun.m_DriveName, szFN, (pDir - szFN));

wcsncpy(fun.m_DirName, pDir, (pFile - pDir));

wcsncpy(fun.m_FileName, pFile, wcslen(pFile));

//here we get the file dates and times as FILETIME structs


if(!GetFileAttributesExW(szFN, GetFileExInfoStandard, &gfi))

return E_INVALIDARG;


FILETIME ft = {0,};

SYSTEMTIME st = {0,};

GetSystemTime(&st);

SystemTimeToFileTime(&st, &ft);

//convert the FILETIMEs to db DATE per fxn above

FileTimeToDBDate(&ft, &fun.m_SSLastUpdate);

FileTimeToDBDate(&gfi.ftCreationTime, &fun.m_CreationDateTime);

FileTimeToDBDate(&gfi.ftLastAccessTime, &fun.m_LastAccessDateTime);

FileTimeToDBDate(&gfi.ftLastWriteTime, &fun.m_ModificationDateTime);



fun.m_Checksum = CalcChecksum(szFN);

fun.m_SizeLo32 = gfi.nFileSizeLow;


hr = fun.Open();


//...error trap omitted, always get S_OK, but nothing updated, stepping
into code shows 0 records updated.

fun.Close();





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.