SQLite WHERE syntax error

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

SQLite WHERE syntax error

Post by quailcreek » Fri Apr 17, 2015 1:51 am

Hi All,
I'm having a little trouble with the syntax of defining the tSQLStatement. I'm getting a db error regarding the WHERE part of the statement. What am I doing wrong?

Code: Select all

on mouseUp
   put fld "theID" into tID
   put specialFolderPath("documents") & "/MyTestDB.sqlite" into tDatabaseFile
   put revOpenDatabase("sqlite",tDatabaseFile) into sDatabaseID
   
   if tID is not empty then
      put "SELECT MyInfo(MyName,MyType, WHERE theID = tID)" into tSQLStatement
      revExecuteSQL sDatabaseID,tSQLStatement
   end if
   answer the result
end mouseUp
Tom
MacBook Pro OS Mojave 10.14

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: SQLite WHERE syntax error

Post by Simon » Fri Apr 17, 2015 2:08 am

Hi Tom,
Can you show what is actually in tID?
Quick guess it's probably missing single or double quotes (I forget which ones to use).

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: SQLite WHERE syntax error

Post by quailcreek » Fri Apr 17, 2015 4:16 am

Hi Simon,
tID will be an number. I'm trying to query for the information in a row based upon the unique ID number in the row. I might be using the wrong query structure though.
Tom
MacBook Pro OS Mojave 10.14

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: SQLite WHERE syntax error

Post by Klaus » Fri Apr 17, 2015 12:23 pm

Hi Tom,

you are supplying the STRING tID and not the content of that variable to the SQL string!

Code: Select all

...
if tID is not empty then
     ## put "SELECT MyInfo(MyName,MyType, WHERE theID = tID)" into tSQLStatement
      put "SELECT MyInfo(MyName,MyType, WHERE theID =" & tID & ")" into tSQLStatement
      revExecuteSQL sDatabaseID,tSQLStatement
   end if
...
Best

Klaus

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: SQLite WHERE syntax error

Post by quailcreek » Fri Apr 17, 2015 4:26 pm

Hi Klaus,
Thanks for the reply. I tried ' " & tID & " ' and " & tID & " and ' & tID & '. None of these worked. When I "answer the result" I get (near “WHERE”: syntax error).
Tom
MacBook Pro OS Mojave 10.14

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: SQLite WHERE syntax error

Post by Klaus » Fri Apr 17, 2015 4:33 pm

Hi Tom,

hm, not being an SQL expert, but should that not be something like this:
...
put "SELECT MyInfo,MyName,MyType FROM TABLENAME WHERE theID =" & tID into tSQLStatement
...
?
I guess MyInfo is ALSO a db column you want to retrieve?


Best

Klaus

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: SQLite WHERE syntax error

Post by quailcreek » Fri Apr 17, 2015 4:53 pm

Hi Klaus,
Actually, MyInfo is the name of the table.
Tom
MacBook Pro OS Mojave 10.14

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: SQLite WHERE syntax error

Post by quailcreek » Fri Apr 17, 2015 6:08 pm

Here's what I ended up with:

Code: Select all

 
if tID is not empty then
      
      put "SELECT MyName,MyType FROM MyInfo WHERE TheID = " & tID &" " into tSQLStatement
      put revDataFromQuery(,,sDatabaseID,tSQLStatement) into tList
      
   end if
Thanks everybody.
Tom
MacBook Pro OS Mojave 10.14

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: SQLite WHERE syntax error

Post by Klaus » Fri Apr 17, 2015 6:55 pm

quailcreek wrote:Hi Klaus,
Actually, MyInfo is the name of the table.
Ah, OK, was not sure!
But why the space at the end of your SQL string? 8)
...
put "SELECT MyName,MyType FROM MyInfo WHERE TheID = " & tID &" " into tSQLStatement
...

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: SQLite WHERE syntax error

Post by quailcreek » Fri Apr 17, 2015 7:16 pm

Good catch, Klaus. The space was there just so I could read the code easier. I can see that it really shouldn't be there. Thanks.
Tom
MacBook Pro OS Mojave 10.14

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: SQLite WHERE syntax error

Post by phaworth » Fri Apr 17, 2015 7:36 pm

You can also do this:

put "SELECT MyName,MyType FROM Myinfo WHERE theID =:1" into tSQLStatement
revExecuteSQL sDatabaseID,tSQLStatement,"tid"

Pete

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: SQLite WHERE syntax error

Post by quailcreek » Fri Apr 17, 2015 8:07 pm

Hi Pete,
Does (theID =:1") mean where the query evaluates to true?

Also, how would I get the result of the query? I tried "answer the result" but no-joy.
Tom
MacBook Pro OS Mojave 10.14

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: SQLite WHERE syntax error

Post by phaworth » Fri Apr 17, 2015 8:37 pm

Sorry, took the revExecuteSQL form your earlier post, should be revDataFromQuery

put "SELECT MyName,MyType FROM Myinfo WHERE theID =:1" into tSQLStatement
put revDataFromQuery(,,sDatabaseID,tSQLStatement,"tid") into tData
if tData begins with "revdberr" then
--insert your error handling code here
end if

The ":1" tells SQL to get the value from a variable you supply in the reDataFromQuery call, that's why it has "tid" as a parameter. Since you already have a variable containing the id, that's a convenient way to simplify the SELECT statement.

You can find out more about the ":1" syntax in the dictionary entry for revDataFromQuery.

Pete

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: SQLite WHERE syntax error

Post by quailcreek » Fri Apr 17, 2015 10:28 pm

Thanks, Pete. That make a lot of sense. It is a bit simpler.
Tom
MacBook Pro OS Mojave 10.14

Post Reply

Return to “Databases”