Save in ASCII

Just starting out? Need help? Post your questions and find answers here.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

I did an initial test of this with the code below in unicode mode compile with unicode source. When I wrote the file I tried with pb_ascii and the default (which is utf8 in unicode mode)

The output was UTF8 text in default and CP932 in pb_ascii (which is what my system is set to use in the regional settings when not using unicode)

If you need to force a specific code page different from the user's default then you will need to use WideCharToMultiByte

Does this help at all or do you need to force a code page non standard to the system running the app?

Code: Select all

UseSQLiteDatabase()

  Filename$ = "c:\utf.db"

  If OpenFile(0, Filename$)
    Debug "Database file created"
    CloseFile(0)
  EndIf
  
  If OpenDatabase(0, Filename$, "", "")
    Debug "Connected to PureBasic.sqlite"
    If DatabaseUpdate(0, "CREATE TABLE info (test VARCHAR(255));")
      Debug "Table created"
    EndIf
    
    DatabaseUpdate(0, "insert into info (test) values('日本語')") ;
    
    DatabaseQuery(0, "SELECT * FROM info")
    
    OpenFile(0,"c:\UTFOut2.txt")
        While NextDatabaseRow(0)
            WriteString(0, GetDatabaseString(0, 0));#PB_Ascii       
        Wend
    CloseFile(0)
        
  EndIf

Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
silvercover
User
User
Posts: 86
Joined: Sat Aug 04, 2007 6:57 pm

Post by silvercover »

I did an initial test of this with the code below in unicode mode compile with unicode source. When I wrote the file I tried with pb_ascii and the default (which is utf8 in unicode mode)

The output was UTF8 text in default and CP932 in pb_ascii (which is what my system is set to use in the regional settings when not using unicode)

If you need to force a specific code page different from the user's default then you will need to use WideCharToMultiByte

Does this help at all or do you need to force a code page non standard to the system running the app?
I did the same last time but it didn't help. I think I need that WideCharToMultiByte .

I also have no problem receiving values from that app.

Thank you.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

can you post some text of the type we are talking about?
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Okay, I think this should solve it

The code page here is set to 932 so you need to change that to whatever.

Notes
1. I'm using writedata not writestring this time so as to be sure PB conversions are not effecting this
2. This must use unicode mode compile as otherwise the input to the WideCharToMultiByte_ function will not be in the right format.
3. I've just set an arbitrary buffer of len x 3 because japanese can take up 3 bytes per char in utf8, if you call WideCharToMultiByte_ with param 6 set to 0 then the function won't convert but rather return the size for you only so you can alloc the memory then call again with that memory size after allocation. Details are here http://msdn.microsoft.com/en-us/library/ms776420.aspx

Code: Select all

UseSQLiteDatabase()

  Filename$ = "c:\utf.db"

  If OpenFile(0, Filename$)
    Debug "Database file created"
    CloseFile(0)
  EndIf
  
  If OpenDatabase(0, Filename$, "", "")
    Debug "Connected to PureBasic.sqlite"
    If DatabaseUpdate(0, "CREATE TABLE info (test VARCHAR(255));")
      Debug "Table created"
    EndIf
    
    DatabaseUpdate(0, "insert into info (test) values('日本語')") ;
    
    DatabaseQuery(0, "SELECT * FROM info")
    
    OpenFile(0,"c:\UTFOut2.txt")
        While NextDatabaseRow(0)
            UTFChar.s = GetDatabaseString(0, 0)
            *OutBuff = AllocateMemory(Len(UTFChar) * 3)
            ActualSize = WideCharToMultiByte_(932, 0, @UTFChar, Len(UTFChar), *OutBuff, Len(UTFChar) * 3, 0, 0) 
            Debug ActualSize
            WriteData(0,*OutBuff,ActualSize)
            
            ;WriteString(0, GetDatabaseString(0, 0));#PB_Ascii       
        Wend
    CloseFile(0)
        
  EndIf


Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
silvercover
User
User
Posts: 86
Joined: Sat Aug 04, 2007 6:57 pm

Post by silvercover »

THANK YOU very much pdwyer.
:D

Now it's working and i can easily put my code page and get the right result.

You rock man! :)

Thank you all guys.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

:mrgreen:
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
silvercover
User
User
Posts: 86
Joined: Sat Aug 04, 2007 6:57 pm

Post by silvercover »

Hi again,

I have another problem on this issue. when I use pdwyer's code in executable application (Compiled with utf8) it works perfect, but when I put the code in a procedure to make my DLL(Compiled with utf8) file it does not work as expected!

I use ProcedureCDLL to make my procedures.

What's wrong?:?:

Thanks in advance.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

was the app calling the dll unicode compiled?
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
silvercover
User
User
Posts: 86
Joined: Sat Aug 04, 2007 6:57 pm

Post by silvercover »

pdwyer wrote:was the app calling the dll unicode compiled?
Yes it does.
DaylightDreamer
User
User
Posts: 52
Joined: Wed Feb 04, 2009 8:11 am
Location: Armenia

Post by DaylightDreamer »

Is there is a way to put data in SQLite database in ANSI already ? :?
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Not sure what you mean
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Post Reply