Seite 3 von 3

Re: FindString

Verfasst: 12.07.2018 11:48
von #NULL
Velindos hat geschrieben:Hallo,
Funzt nicht!
Hallo,
Reicht nicht!

SQL-Fehler? Keine Ergebnisse? oder zuviele Ergebnisse?

Re: FindString

Verfasst: 13.07.2018 12:35
von Velindos
Hallo,
Reicht nicht!
natürlich nicht!

Also es werden keine Ergebnisse angezeigt, sprich es kommt nichts zurück!

Anderes muss erst testen!

Gruss ... Velindos!

Re: FindString

Verfasst: 14.07.2018 08:50
von #NULL
Ich habe im Post oben im SQL string REGEX statt REGEXP geschrieben, was falsch ist. Ich bekomme es aber trotzdem nicht zum Laufen. Habe das hier versucht: https://stackoverflow.com/a/8338515
aber regexp bleibt undefiniert.

Code: Alles auswählen

UseSQLiteDatabase()

Procedure CheckDatabaseUpdate(Database, Query$)
  Result = DatabaseUpdate(Database, Query$)
  If Result = 0
    Debug DatabaseError()
  EndIf
  
  ProcedureReturn Result
EndProcedure

DatabaseFile$ = GetTemporaryDirectory()+"Database.sqlite"

If CreateFile(0, DatabaseFile$)
  CloseFile(0)
  
  If OpenDatabase(0, DatabaseFile$, "", "")
    
    ;If DatabaseQuery(0, "SELECT load_extension('/usr/lib/sqlite3/pcre.so'); ")
    ;Else
    ;  Debug DatabaseError()
    ;EndIf
    
    CheckDatabaseUpdate(0, "CREATE TABLE testtable (id int, txt text, vc varchar)")
    CheckDatabaseUpdate(0, "INSERT INTO testtable (id, txt, vc) VALUES ('1', 'abc 11 de', 'QRS 11 TU')")
    CheckDatabaseUpdate(0, "INSERT INTO testtable (id, txt, vc) VALUES ('1', 'abc 22 de', 'QRS 22 TU')")
    CheckDatabaseUpdate(0, "INSERT INTO testtable (id, txt, vc) VALUES ('1', 'abc 33 de', 'QRS 33 TU')")
    
    q.s = ""
    q + "select * from testtable where "
    ;q + " txt like '%22%' "
    q + " txt REGEXP '.*' "
    If DatabaseQuery(0, q)
      
      cols = DatabaseColumns(0)
      
      s.s = ""
      For i=0 To cols-1
        s + " | " + LSet(DatabaseColumnName(0, i), 12, " ")
      Next
      Debug s
      
      While NextDatabaseRow(0)
        s.s = ""
        For i=0 To cols-1
          s + " | " + LSet(GetDatabaseString(0, i), 12 ," ")
        Next
        Debug s
      Wend
      
      FinishDatabaseQuery(0)
    Else
      Debug DatabaseError()
    EndIf
    
    CloseDatabase(0)
  Else
    Debug "Can't open database !"
  EndIf
Else
  Debug "Can't create the database file !"
EndIf

Ansonsten einfach select * und mit der letzten Funktion von NicTheQuick auf PB Ebene nachfiltern (Kannst natürlich auch noch mit LIKE im SQL auf reines Vorkommen vorfiltern):

Code: Alles auswählen

UseSQLiteDatabase()

Procedure CheckDatabaseUpdate(Database, Query$)
  Result = DatabaseUpdate(Database, Query$)
  If Result = 0
    Debug DatabaseError()
  EndIf
  
  ProcedureReturn Result
EndProcedure

Procedure FindStringEx(String.s, StringToFind.s, StartPosition.i = 1, Mode.i = #PB_String_CaseSensitive)
  Protected flag.i = 0, pos.i
  If Mode <> #PB_String_CaseSensitive
    flag = #PB_RegularExpression_NoCase
  EndIf
  Protected StringToFindEscaped.s
  Protected *c.Character = @StringToFind
  While *c\c
    Select *c\c
      Case '\', '^', '$', '{', '}', '[', ']', '(', ')', '.', '*', '+', '?', '|', '<', '>', '-', '&'
        StringToFindEscaped + "\" + Chr(*c\c)
      Default
        StringToFindEscaped + Chr(*c\c)
    EndSelect
    *c + SizeOf(Character)
  Wend
  Protected re.i = CreateRegularExpression(#PB_Any, "\b" + StringToFindEscaped + "\b", #PB_RegularExpression_MultiLine | #PB_RegularExpression_AnyNewLine | flag)
  
  If ExamineRegularExpression(re, String)
    While NextRegularExpressionMatch(re)
      pos = RegularExpressionMatchPosition(re)
      If pos >= StartPosition
        Break
      EndIf
      pos = 0
    Wend
  EndIf
  
  FreeRegularExpression(re)
  
  ProcedureReturn pos
EndProcedure

DatabaseFile$ = GetTemporaryDirectory()+"Database.sqlite"

If CreateFile(0, DatabaseFile$)
  CloseFile(0)
  
  If OpenDatabase(0, DatabaseFile$, "", "")
    
    CheckDatabaseUpdate(0, "CREATE TABLE testtable (id int, txt text)")
    CheckDatabaseUpdate(0, "INSERT INTO testtable (id, txt) VALUES ('1', 'ein K11 ist')")
    CheckDatabaseUpdate(0, "INSERT INTO testtable (id, txt) VALUES ('1', 'keinK11 ist')")
    CheckDatabaseUpdate(0, "INSERT INTO testtable (id, txt) VALUES ('1', 'keinK111 ist')")
    
    q.s =  "select * from testtable "
    If DatabaseQuery(0, q)
      
      cols = DatabaseColumns(0)
      
      s.s = ""
      For i=0 To cols-1
        s + " | " + LSet(DatabaseColumnName(0, i), 12, " ")
      Next
      Debug s
      
      While NextDatabaseRow(0)
        colMatch = #False
        
        s.s = ""
        For i=0 To cols-1
          If FindStringEx(GetDatabaseString(0, i), "K11")
            colMatch = #True
          EndIf
          s + " | " + LSet(GetDatabaseString(0, i), 12 ," ")
        Next
        
        If colMatch
          Debug s
        EndIf
      Wend
      
      FinishDatabaseQuery(0)
    Else
      Debug DatabaseError()
    EndIf
    
    CloseDatabase(0)
  Else
    Debug "Can't open database !"
  EndIf
Else
  Debug "Can't create the database file !"
EndIf

Re: FindString

Verfasst: 14.07.2018 10:51
von mk-soft
Kleiner Tipp mit SQLite...

Man kann auch eine Memory-Datenbank erstellen

Code: Alles auswählen

DatabaseFile$ = ":memory:"
Somit braucht man für solche Sachen keine Datei erstellen.