Page 1 of 1

Sqlite Under Linux

Posted: Sat Aug 07, 2004 8:23 pm
by Num3
First of all thank to El Choni for the basic routines to use sqlite dll...

I've ported it to linux and it works just fine!

I've tested it using sqlite version 2.8.xx

You'll need Sqlite linux 'so' and a table to test this code, just place it all in the same dir ;)
You can create one with the sqlite.exe utility
1. sqlite test.db
2. CREATE TABLE bookmarks (id, url, site, category);
3. INSERT INTO bookmarks VALUES ('1', 'http://www.purebasic.com',
'Purebasic', 'basic');

Code: Select all


#SQLITE_OK =0              ; Successful Result
#SQLITE_ERROR =1        ; SQL error Or missing database
#SQLITE_INTERNAL =2   ; An internal logic error in SQLite
#SQLITE_PERM =3         ; Access permission denied
#SQLITE_ABORT =4        ; Callback routine requested An abort
#SQLITE_BUSY =5         ; The database file is locked
#SQLITE_LOCKED =6     ; A table in The database is locked
#SQLITE_NOMEM =7      ; A malloc() failed
#SQLITE_READONLY =8   ; Attempt To write A readonly database
#SQLITE_INTERRUPT =9  ; Operation terminated by SQLite_Interrupt()
#SQLITE_IOERR =10       ; Some kind of disk I/O error occurred
#SQLITE_CORRUPT =11   ; The database disk image is malformed
#SQLITE_NOTFOUND =12 ; (internal Only) table Or record not found
#SQLITE_FULL =13          ; Insertion failed because database is full
#SQLITE_CANTOPEN =14 ; Unable To open The database file
#SQLITE_PROTOCOL =15 ; database lock protocol error
#SQLITE_EMPTY =16       ; (internal Only) database table is empty
#SQLITE_SCHEMA =17    ; The database schema changed
#SQLITE_TOOBIG =18      ; Too much Data For one Row of A table
#SQLITE_CONSTRAINT =19 ; abort due To contraint violation
#SQLITE_MISMATCH =20    ; Data type mismatch
#SQLITE_MISUSE =21        ; Library used incorrectly
#SQLITE_NOLFS =22         ; Uses OS features not supported on host
#SQLITE_AUTH =23        ; Authorization denied
#SQLITE_ROW =100        ; sqlite_step() has another Row ready
#SQLITE_DONE =101       ; sqlite_step() has finished executing

Procedure.s Get_Path()
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux  
 
  Protected *Environ.LONG
  
  !extrn _environ
  !MOV eax, [_environ]
  !MOV [esp+4], eax
  
  Variable$ = "_=" ; This is the system variable that gives the path of the running app
  
  
  While *Environ\l <> 0
    If CompareMemoryString(@Variable$, *Environ\l, 0, Len(Variable$)) = 0
      ProcedureReturn PeekS(*Environ\l + Len(Variable$))
    EndIf
    *Environ + 4
  Wend
  ;end;***
  
  ProcedureReturn ""
  CompilerEndIf
  
EndProcedure

Procedure.l SQLite_Get_Table(DB_Handle.l, SQLString.s,  * Rows,  * Cols)
  Shared Result.l
  Shared LResultsPtr.l
  Shared LRows.l
  Shared LCols.l
  
  Result  = CallFunction(0, "sqlite_get_table", DB_Handle, SQLString, @LResultsPtr, @LRows, @LCols, 0)
  
  If Result  = #SQLITE_OK
    ; return number of rows/columns
    PokeL(*Rows,  LRows)
    PokeL(*Cols,  LCols)
    
    ; redimension results array (clears data)
    Dim DBData.s(LRows, LCols - 1)
    ; copy data into array
    Address.l  = LResultsPtr
    AddrInc.l  = LCols  * 4
    For Row.l  = 0 To LRows
      For Col.l  = 0 To LCols - 1
        DBData(Row,  Col) = PeekS(PeekL(Address  + Col  * 4))
      Next
      Address  + AddrInc
    Next
    ; free table memory
    CallFunction(0,  "sqlite_free_table", LResultsPtr)
  EndIf
  
  ProcedureReturn Result
EndProcedure

path$=""

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    ;We need to remove the application name out of the path...
    path$=Get_Path()
    w=Len(path$)
    While k=0
      a+1
      k=FindString(path$,"/",w-a)
    Wend
    path$=Mid(path$,1,k)

CompilerEndIf

errormsg.s=""
If OpenLibrary(0,path$+"sqlite.so")
  
  db=CallFunction(0,"sqlite_open",path$+"test.db",0,errormsg)
  
  If db
    
    SQL.s  = "SELECT id, url, site, category FROM bookmarks WHERE category LIKE 'delp%'"
    Result  = SQLite_Get_Table(db, SQL, @Rows, @Cols)
    
    If Result  = #SQLITE_OK
      ; get the results
      ; display number of rows/columns
      a$=""
      a$+ "Rows = "  + Str(Rows)+Chr(10)
      a$+ "Columns = "  + Str(Cols)+Chr(10)+Chr(10)
      
      ; display column headers
      For Col.l  = 0 To Cols  - 1
        a$+ DBData(0, Col)+Chr(10)
      Next
      ; display returned rows
      For Row.l  = 1 To Rows
        a$+ "--------------" +Chr(10)
        For Col  = 0 To Cols  - 1
          a$+ DBData(Row, Col)+Chr(10)
        Next
      Next
      
      CallFunction(0,"sqlite_close",db)
      MessageRequester("Result",a$)
      a$=""
      
    Else
      MessageRequester("Error","Unable to make query"+Chr(10)+errormsg)
    EndIf
    
    
  Else
    MessageRequester("Error","Unable to open database"+Chr(10)+errormsg)
  EndIf
  
  
Else
  MessageRequester("Error","Unable to open library")
EndIf


Posted: Sun Aug 08, 2004 6:10 am
by sec
Segmentation fault cause by Get_Path()
although i have sqlite3_ :wink: