SQLite File open. read . write . deleat

Post

Posted
Rating:
#1 (In Topic #857)
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
I posted earlier about SQL file handling, after some research and trial and error I have got my program working.
So I thought I would share my results for anyone else that is where I was a month ago (searching here for answers).
Not all of us here are EXPERTS (me included ) .. so this may be of help to some other New-be here   :)
most of my information came from this site : SQLite Tutorial

So here is a module that will handle a SQLite file
If anyone has suggestions or problems or bugs let me know.
If you think this should be in the "Farm" please put it there.


Code (gambas)

  1. ' ' Gambas module file
  2.    '' Requires    gb.db.sqlite3
  3.    '' Module file to handle SQLite file
  4.    '' SQLite  file handeling subroutines
  5.    '' writen by  GrayGhost4  Marvin Clavey  June 2022
  6.    
  7.   Public sadata As String[]
  8.  
  9.  
  10. Public Sub GetFileData(sFilename As String, scolumnName As String) As String[]
  11. Dim test As Variant[]
  12.      Try $con.Close()             ' Close the connection. The try allows it to fail without error
  13.      $con.Type = "sqlite3"        ' Defines the type of connection
  14.      $con.Host = Application.path ' Host will be the path where the sqlite db file is
  15.      $con.Name = "data.SQL"  '  database name is the name of the database file"
  16.      Try $con.Open()                  ' We activate and open the connection, the try is to allow an error
  17.      If Error Then Print "Cannot Open Database. Error = "; Error.Text
  18.      '' Retreve all the data from sFilename that is in scolumnName
  19.      RS = $con.Exec("Select * From " & Quote(sFilename))
  20.      Return RS.All(scolumnName)   '' return all the data from one column
  21.  End
  22.        
  23. Public Sub GetRecordData(sFileName As String, sFileColumn As String, sFileIndex As String) As String[]
  24.     Dim saInfo As New String[]    ' must have "NEW" or you will get a null error with .add
  25.     Dim obj As ResultField
  26.     RS = $con.Exec("Select * From" & Quote(sFileName) & "where" & Quote(sFileColumn) & "like" & Quote(sFileIndex))
  27.      For Each obj In RS.Fields
  28.      sainfo.Add(RS[obj.Name])
  29.      Next
  30.      Return saInfo
  31.       End
  32.  
  33. Public Sub UpdateFileData(sFileName As String, sFileIndex As String, sainfo As String[])
  34.     Dim saCommand As String  
  35.     Dim obj As ResultField
  36.     Dim x As Integer = 0
  37.     For Each obj In RS.Fields                ''assemble the update command "column" , "data"
  38.        saCommand &= Quote(obj.Name) & " = "
  39.        saCommand &= Quote(sainfo[x]) & ","
  40.        Inc x
  41.     Next
  42.     saCommand = Left(saCommand, -1)        '' remove the last "'"  from saCommand
  43.        $con.Exec("update" & Quote(sFileName) & "set" & saCommand & "where accname like" & Quote(sFileIndex))
  44.  
  45. Public Sub InsertNewRecord(sFileName As String)
  46.   Dim sNewRecord As String
  47.   For Each obj As Object In RS.Fields    ' This assenbles the File column names it to A string
  48.     sNewRecord &= Quote(obj.Name) & "," ' to insert into a blank record to be edited later
  49.   Next
  50.    sNewRecord = Left(sNewRecord, -1)         ' remove the last "'"  from command
  51.    sNewRecord = Right(sNewRecord, -(InStr(sNewRecord, ",") - 1))       ' Remove the first reccord Field (which is the index to the file)
  52.    sNewRecord = "(" & Quote("newwAcc-" & Str(Rand(20, 100))) & sNewRecord & ")"  ' Add a new first field (random) for index
  53.     $con.Exec("INSERT INTO " & Quote(sFileName) & " VALUES " & sNewRecord)
  54.  
  55. Public Sub deleatOneRecord(sFileName As String, sFileField As String, sFileIndex As String)
  56.     '' Deleat one reccord from the table with an indexed coluem
  57.         $con.Exec("delete from" & Quote(sFileName) & "where" & Quote(sFileField) & "like " & Quote(sFileIndex))
  58.  
  59.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.