MS ODBC encoding issue

Regan Heath regan at netmail.co.nz
Thu Jan 17 10:36:16 PST 2013


On Mon, 14 Jan 2013 10:02:04 -0000, Regan Heath <regan at netmail.co.nz>  
wrote:

>> I'm more than happy to upload the database file here,but I can't find  
>> how to.May I have your mail address?Appreciated for all the help!
>
> My email address in the from is valid: regan at netmail dot co dot nz

Ok, solved the issue I think.


1. I can connect to your database file with a connection string like..

   odbc.connect(r"Driver={Microsoft Access Driver (*.mdb,  
*.accdb)};Dbq=<PATHGOESHERE>\db1.mdb;");

Just replace <PATHGOESHERE> with the real path to the file, using single \  
characters for path separators.


2. In fetchAll, I have made the following changes:

     while(true)
     {
        char sz_buf[256];
->     wchar[] pszBuf;
        SQLINTEGER buflen;
        string[] rowData;
->     uint uintVal;

        if(SQLFetch(hStmt)==SQL_NO_DATA)
        {
            break;
        }

        for(int i=1;i<=col;i++)
        {
		
            SQLColAttribute(hStmt, cast(ushort)i, SQL_DESC_NAME,  
sz_buf.ptr, 256, &buf_len, cast(void*)0);
            SQLColAttribute(hStmt, cast(ushort)i, SQL_DESC_TYPE,  
cast(void*)0, 0, cast(short*)0, &colType);
            SQLColAttribute(hStmt, cast(ushort)i, SQL_DESC_LENGTH, null, 0,  
cast(short*)0, &colLen);

->         switch(colType)
->         {
->         case SQL_INTEGER:
->             SQLGetData(hStmt, cast(ushort)i, SQL_C_ULONG, &uintVal,  
uintVal.sizeof, cast(int*)&buflen);
->             rowData ~= to!string(uintVal);
->             break;
->         case SQL_VARCHAR:
->             pszBuf = new wchar[colLen];
->             SQLGetData(hStmt, cast(ushort)i, SQL_C_WCHAR, pszBuf.ptr,  
pszBuf.length, cast(int*)&buflen);
->             pszBuf.length = buflen;
->             rowData ~= toUTF8(pszBuf);
->             break;
->         default:
->             break;
->         }
        }
        v~=rowData;
        row++;
     }

The key here is that when we ask for the VARCHAR data, we ask for it as  
WCHAR (meaning UTF-16 or more likely UCS-2 a subset of UTF-16).  We then  
have to convert it to UTF-8 using std.utf.toUTF8()

Your original code was asking for it as SQL_C_CHAR, and the odbc layer  
knows it cannot represent the Chinese characters in ASCII, so it was  
returning a '?' for each of them.

Now I can see (in Visual-D debugger) the Chinese characters in the  
rowData, but I can't get it to display correctly on my windows console..   
can someone remind me how to do that?

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list