SQLite3 BaseFunction-Include for Win + Lin + Unicode

Share your advanced PureBasic knowledge/code with the community.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Thanks Thorsten1867

I have updated the source, but only the second version:
http://www.purebasic.fr/english/viewtop ... 667#194667
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Post by Thorsten1867 »

Convert old ANSI database to new unicode database:

Code: Select all

; Convert SQLite-Database (ANSI -> Unicode)
; by Thorsten Hoeppner

XIncludeFile "SQLite3_Include.pbi"

If Not SQLiteInit()
  MessageRequester("Error", "Couldn't init SQLite3 lib") : End
EndIf

Structure SQLiteMasterStructure
  Name.s
  sql.s
EndStructure

Procedure.b ConvertDB(dbfile.s) ; Konvertiere von ANSI nach Unicode
  If Not FileExist(dbfile) : ProcedureReturn #False : EndIf
  Define.SQ3_TABLEMAP TableMap
  NewList Table.SQLiteMasterStructure()
  
  CopyFile(dbfile, StringField(dbfile,1,".")+".asc")
  
  ; ===== Load as ANSI =====
  hDB1.l = SQLiteOpen(dbfile, #True)
  If hDB1
    ;{ --- Get table names ---
    If SQLiteGetTable(hDB1, "SELECT name, sql  FROM sqlite_master WHERE type='table';", TableMap)
      While SQLiteNextRow(TableMap)
        AddElement(Table())
        Table()\Name = SQLiteColValue(TableMap,1)
        Table()\sql = SQLiteColValue(TableMap,2)
      Wend 
    EndIf ;}
    ;{ --- Read table content ---
    If CreateFile(0, "SQLDump.sql")
      ForEach Table()
        If SQLiteGetTable(hDB1, "SELECT * FROM "+Table()\Name+";", TableMap)
          WriteStringN(0, Table()\sql)
          While SQLiteNextRow(TableMap)
            SQLiteSelectCol(TableMap, 1)
            sql$ = "INSERT INTO "+Table()\Name+" VALUES ('"+SQLiteValue(TableMap)+"'"
            While SQLiteNextCol(TableMap)
              sql$ + ", '"+SQLiteValue(TableMap)+"'"
            Wend
            sql$ + ");"
            sql$ = ReplaceString(sql$, Chr(10), " ")
            sql$ = RemoveString(sql$, Chr(13))
            WriteStringN(0, sql$, #PB_UTF8)
          Wend
          SQliteFreeTable(TableMap)
        EndIf
      Next
      CloseFile(0)
    EndIf ;}
    SQLiteClose(hDB1)
  EndIf
  
  SQLiteEnd()
  If Not SQLiteInit()
    MessageRequester("Error", "Couldn't init SQLite3 lib")
  EndIf
  
  ;{ ===== Save as unicode =====
  If ReadFile(1, "SQLDump.sql")
    DeleteFile(dbfile)
    hDB.l = SQLiteOpen(dbfile)
    If hDB
      SQLiteExecute(hDB, "BEGIN TRANSACTION;")
      While Eof(1) = 0 
        SQLiteExecute(hDB, ReadString(1))
      Wend
      If SQLiteExecute(hDB, "CREATE TABLE DBInfo (id INTEGER PRIMARY KEY, format TEXT);")
        SQLiteExecute(hDB, "INSERT INTO DBInfo (format) VALUES('unicode');")
      EndIf
      SQLiteExecute(hDB, "COMMIT;")
      SQLiteClose(hDB)
    Else
      ProcedureReturn #False
    EndIf
    CloseFile(1)
  Else
    ProcedureReturn #False
  EndIf ;}
  
  DeleteFile("SQLDump.sql")
  
  ProcedureReturn #True
EndProcedure

Procedure.b CheckDBCodePage(dbfile.s) ; Überprüfe CodePage
  If Not FileExist(dbfile) : ProcedureReturn #False : EndIf
  Define.SQ3_TABLEMAP TableDB
  hDB = SQLiteOpen(dbfile)
  If hDB
    If SQLiteGetTable(hDB, "Select format From DBInfo;", TableDB) 
      SQliteFreeTable(TableDB)
    ElseIf SQLiteErrorMsg(hDB) = "no such table: DBInfo"
      SQLiteClose(hDB)
      ProcedureReturn #True
    EndIf 
  EndIf
  SQLiteClose(hDB)
  ProcedureReturn #False
EndProcedure

If CheckDBCodePage("test.db") ; Überprüfe Codepage (Datenbank-Klasse)
  ConvertDB("test.db")
EndIf

SQLiteEnd()
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

Thanks so much for these updated functions. Much more simple than the mess I was dealing with before. And it seems faster than before...? Although that may be a newer dll.

So, my question - what'd be the easiest/fastest (possible?) way to include the sqlite dll in the app to use with your latest include?

Thanks!
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Post by Thorsten1867 »

I think I found a bug:

Code: Select all

XIncludeFile("SQLite3_Include.pbi")

SQLiteInit()

Procedure.s CheckRSK(hDB.l) 
  Define.SQ3_TABLEMAP TableDB 
  found.b = #False : test$ = "Test"
  SQLiteGetTable(hDB, "SELECT opt FROM worte WHERE wortstamm = '"+test$+"' ;", TableDB)
  While SQLiteNextRow(TableDB) 
    Select SQLiteColValue(TableDB, 1)
      Case "1"
        ; --- found ---
        Break 
    EndSelect
  Wend
  SQliteFreeTable(TableDB)
  ProcedureReturn "Test"
EndProcedure

hDB = SQLiteOpen("TestDB.db")
SQLiteExecute(hDB, "CREATE TABLE worte (id INTEGER PRIMARY KEY, wortstamm TEXT, endung TEXT, opt TEXT);")
SQLiteExecute(hDB, "INSERT INTO worte (wortstamm, endung, opt) VALUES('Test', 'e', '1');")
; --------------------------------------------------
Result$ = CheckRSK(hDB) : Debug "Result: "+Result$
; --------------------------------------------------
SQLiteClose(hDB)

SQLiteEnd() ; Close SQLite
The result of the procedure is "1" and not "Test"?????
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

Thorsten1867 wrote:The result of the procedure is "1" and not "Test"?????
please check the last line of your CheckRSK()-Proc. ;-)

Greetings ... Kiffi
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Post by Thorsten1867 »

I return "Test", but the result is "1". :shock:
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

Thorsten1867 wrote:I return "Test", but the result is "1". :shock:
<SorryForGerman>
Wo ist denn jetzt Dein Problem? Wieso gibst Du immer 'Test' zurück (unabhängig vom Ergebnis)?
</SorryForGerman>

Greetings ... Kiffi
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Post by Thorsten1867 »

[german]
Ich habe den Orginalcode auf ein Minimum reduziert und ich gebe Test zurück, damit ich sicher bin, das der Fehler nicht an einer Variablen liegen kann. Egal was ich zurückgebe ich erhalte immer "1", was eigentlich völlig unmöglich sein sollte. Scheinbar liegt das Problem an dem "Select" in Verbindung mit "SQLiteColValue()".
[/german]
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

ah, now i see. This is really very strange... :shock:

Greetings ... Kiffi

// Edit:

Here ist a little workaround for this strange bug. By avoiding the
'break'-Statement, your Procedure returns the correct value. But
nevertheless i don't know, why this happens...

Code: Select all

Procedure.s CheckRSK(hDB.l)
  Define.SQ3_TABLEMAP TableDB
  Define.l RowCounter
  found.b = #False : test$ = "Test"
  SQLiteGetTable(hDB, "SELECT opt FROM worte WHERE wortstamm = '"+test$+"' ;", TableDB)
  
  For RowCounter = 1 To SQLiteRows(TableDB)
  
    SQLiteSelectRow(TableDB, RowCounter)
    SQLiteSelectCol(TableDB, 1)
    
    Select SQLiteValue(TableDB)
      Case "1"
        ; --- found ---
        RowCounter = SQLiteRows(TableDB)
    EndSelect
    
  Next
    
  SQliteFreeTable(TableDB)
  
  ProcedureReturn "Test"
  
EndProcedure
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Post by Thorsten1867 »

I don't know why, but this seems to work, too:

Code: Select all

Procedure.s CheckRSK(hDB.l)
  Define.SQ3_TABLEMAP TableDB
  found.b = #False : test$ = "Test"
  SQLiteGetTable(hDB, "SELECT opt FROM worte WHERE wortstamm = '"+test$+"' ;", TableDB)
  While SQLiteNextRow(TableDB)
    opt$ = SQLiteColValue(TableDB, 1)
    Select opt$
      Case "1"
        ; --- found ---
        Break
    EndSelect
  Wend
  SQliteFreeTable(TableDB)
  ProcedureReturn "Test"
EndProcedure 
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

Thanks for this man, it will make my life alot easier!
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

Well, merged it into my engine and it's running great! Very nice work man.
User avatar
michel51
Enthusiast
Enthusiast
Posts: 290
Joined: Mon Nov 21, 2005 10:21 pm
Location: Germany

Post by michel51 »

ts-soft wrote:For advanced using, i have add bindings-functions, all functions with unicode-support!
Last update: 07.08.2007
I've dowloaded this last version of "SQLite3_Include.pbi" and tried to run with the first (and other) example codes.(see the first posts)
But I get only this error message (dont' know, how to post an image :roll: )

Code: Select all

PureBasic - Error

purebasic.asm: 1263:Invalid mnemonic 'SUB'
purebasic.asm: 1264:Invalid mnemonic 'CALL'
purebasic.asm: 1265:Invalid mnemonic 'ADD'
purebasic.asm: 1585:Invalid mnemonic 'SUB'
purebasic.asm: 1586:Invalid mnemonic 'CALL'
purebasic.asm: 1587:Invalid mnemonic 'ADD'
purebasic.asm: 3120:Invalid mnemonic 'SUB'
purebasic.asm: 3121:Invalid mnemonic 'CALL'
purebasic.asm: 3122:Invalid mnemonic 'ADD'
purebasic.asm: 3939:Parameter syntax error (parameter 1)
Error: Assambler
This message occures also if I try only the include

Code: Select all

XIncludeFile "SQLite3_Include.pbi"
EnableExplicit
It seems, that is a error within the include file - but I cannot think it me.
But it should run with MacOsX
Only the FIRST version was running. That's posted by me in May '07

What is wrong?
michel51

Mac OS X Snow Leopard (10.6.8 ) Intel
PureBasic V 5.21(x64), V 5.22beta
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

@michel51
Sorry, i have no "Apfelchen" Macintosh, so i can't help.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
CSAUER
Enthusiast
Enthusiast
Posts: 188
Joined: Mon Oct 18, 2004 7:23 am
Location: Germany

Post by CSAUER »

You are right. The actual Beta of PB for Mac is not working with the latest code. It should not be a problem of the SQLite-include, it should be PB for Mac in general. I cannot find out where it comes from, but I passed Fred a few example codes to go deep into details.
I don't know about the actual status. Hopefully he will get PB for Mac fully compatible, so that every standard PB4 code works fine.

Another question from my side:
Does anybody have an idea, how to explore table names?
I know that there is a ".tables" command, but it does not work with SQLiteGetTable and SQLiteExecute does not bring a result.
If you key into SQLite.exe it works fine.
Post Reply