Sqlite using LIKE % with SetDatabaseString

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fig
Enthusiast
Enthusiast
Posts: 352
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Sqlite using LIKE % with SetDatabaseString

Post by Fig »

Hi,

I would like to use the LIKE %? with SetDatabaseString()
Is it possible ?

Code: Select all

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$, "", "")
   
      CheckDatabaseUpdate(0, "CREATE TABLE food (name Text)")

      CheckDatabaseUpdate(0, "INSERT INTO food (name) VALUES ("+"'test'"+")")
      CheckDatabaseUpdate(0, "INSERT INTO food (name) VALUES ("+"'test test lol'"+")")
      CheckDatabaseUpdate(0, "INSERT INTO food (name) VALUES ("+"'amha test test lol'"+")")
      text.s="test"
      SetDatabaseString(0,0,text)
      
      If DatabaseQuery(0, "SELECT * FROM food WHERE name LIKE ? ;")
         While NextDatabaseRow(0)
            Debug "First "+GetDatabaseString(0, 0)
         Wend
         FinishDatabaseQuery(0)
      EndIf
      SetDatabaseString(0,0,text)
      If DatabaseQuery(0, "SELECT * FROM food WHERE name LIKE ? % ;")
      	While NextDatabaseRow(0)
      		Debug "Second "+GetDatabaseString(0, 0)
      	Wend
      	FinishDatabaseQuery(0)
      EndIf
      CloseDatabase(0)
   Else
      Debug "Can't open database !"
   EndIf
Else
   Debug "Can't create the database file !"
EndIf
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
Starwolf20
User
User
Posts: 25
Joined: Fri Sep 04, 2009 7:08 pm
Location: Corsica

Re: Sqlite using LIKE % with SetDatabaseString

Post by Starwolf20 »

Sql right syntax = SELECT * FROM food WHERE name LIKE '%' ;
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: Sqlite using LIKE % with SetDatabaseString

Post by kpeters58 »

This can easily be done - have a look below.

Code: Select all

EnableExplicit

UseSQLiteDatabase()


Procedure.s QuotedStr(Str.s)
  ProcedureReturn "'" + ReplaceString(Str, "'", "''") + "'"
EndProcedure  

Procedure CheckDatabaseUpdate(Database, Query.s)
  Protected result = DatabaseUpdate(Database, Query.s)
  
   If result = 0
      Debug DatabaseError()
   EndIf
   
   ProcedureReturn result
EndProcedure

Define DatabaseFile.s = GetTemporaryDirectory() + "Database.sqlite"

If CreateFile(0, DatabaseFile)
   CloseFile(0)
   
   If OpenDatabase(0, DatabaseFile, "", "")
   
      CheckDatabaseUpdate(0, "CREATE TABLE food (name Text)")
      ;
      CheckDatabaseUpdate(0, "INSERT INTO food (name) VALUES (" + QuotedStr("red plum")    + ")")
      CheckDatabaseUpdate(0, "INSERT INTO food (name) VALUES (" + QuotedStr("purple plum") + ")")
      CheckDatabaseUpdate(0, "INSERT INTO food (name) VALUES (" + QuotedStr("yellow plum") + ")")
      CheckDatabaseUpdate(0, "INSERT INTO food (name) VALUES (" + QuotedStr("yellow apple") + ")")
      ;
;     Define text.s = "%rpl%"   ; = instring search
      Define text.s = "yellow%" ; = begins with search
;     Define text.s = "%le"     ; = ends in search
      SetDatabaseString(0, 0, text)
      If DatabaseQuery(0, "SELECT * FROM food WHERE name LIKE ?;")
         While NextDatabaseRow(0)
            Debug "Found: " + GetDatabaseString(0, 0)
         Wend
         FinishDatabaseQuery(0)
       EndIf
      CloseDatabase(0)
   Else
      Debug "Can't open database!"
   EndIf
Else
   Debug "Can't create the database file!"
EndIf
PB 5.73 on Windows 10 & OS X High Sierra
User avatar
mk-soft
Always Here
Always Here
Posts: 6205
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Sqlite using LIKE % with SetDatabaseString

Post by mk-soft »

Small tip for create test sqlite database :wink:

Code: Select all

Define DatabaseFile.s = ":memory:"

If #True
   If OpenDatabase(0, DatabaseFile, "", "")
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Fig
Enthusiast
Enthusiast
Posts: 352
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Sqlite using LIKE % with SetDatabaseString

Post by Fig »

Nice tips !

Thank you all, it's clearer for me now ! :D
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
Post Reply