# Help with SQL syntax on SQLite 

**URL:** <https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357>\
**Category:** Databases\
**Created:** [September 23, 2019, 9:26pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357 "2019-09-23T21:26:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [September 23, 2019, 9:26pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/1 "2019-09-23T21:26:07Z")

</div>

Hi there!

I would appreciate a little help with an SQL Select I´m attempting to run against an SQLITE DB. Any ideas why this Select won´t run on?

`SELECT B.ID AS Bale_ID, P.Code AS P_Code, 
B.Permanent_Bale_ID, b.Bale_Storage_Location, 
S.Contract_Number, S.Contract_Date AS S_Contract_Date, S.Date_Ship 
FROM BALES AS B INNER JOIN SHIPMENTS AS S 
ON S.ID = B.Shipment_ID 
INNER JOIN Partners AS P 
ON P.ID = S.Partner_ID 
ORDER BY S.Date_Ship `

The Select runs fine on “DB Browser for SQLite” but returns a DB error when try the statement using a DB.SQLSelect(SQL).

Any help is much appreciated!

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [September 23, 2019, 9:28pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/2 "2019-09-23T21:28:19Z")

</div>

remove the “AS” keywords

```auto
FROM BALES B INNER JOIN SHIPMENTS S 
```

---

<div class="post-metadata">

**Author:** ![Norman\_Palardy](https://forum.xojo.com/user_avatar/forum.xojo.com/norman_palardy/32/169_2.png) [@Norman\_Palardy](https://forum.xojo.com/u/Norman_Palardy)\
**Post date:** [September 23, 2019, 9:33pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/3 "2019-09-23T21:33:11Z")

</div>

AS should be syntactically fine in both the selected column list an table aliases and the join clauses  
[https://sqlite.org/lang\_select.html](https://sqlite.org/lang_select.html)

---

<div class="post-metadata">

**Author:** ![Norman\_Palardy](https://forum.xojo.com/user_avatar/forum.xojo.com/norman_palardy/32/169_2.png) [@Norman\_Palardy](https://forum.xojo.com/u/Norman_Palardy)\
**Post date:** [September 23, 2019, 9:33pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/4 "2019-09-23T21:33:39Z")

</div>

> [@455122:@Hector Marroquin](#):
>
> The Select runs fine on “DB Browser for SQLite” but returns a DB error when try the statement using a DB.SQLSelect(SQL).

what error are you getting in Xojo ?

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [September 23, 2019, 10:19pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/5 "2019-09-23T22:19:56Z")

</div>

> [@455127:@Norman Palardy](#):
>
> what error are you getting in Xojo ?

I´m getting a  
1 - near “?SELECT”: syntax error

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [September 23, 2019, 10:24pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/6 "2019-09-23T22:24:24Z")

</div>

[quote=455124:@Dave S]remove the “AS” keywords

`
FROM BALES B INNER JOIN SHIPMENTS S 
`[/quote]

Thanks for your reply, this looks like the cause of my problem. I will read the link that Norman posted.

I tried this but it didn´t work either

`ELECT B.ID AS Bale_ID, P.Code AS P_Code, 
B.Permanent_Bale_ID, b.Bale_Storage_Location, 
S.Contract_Number, S.Contract_Date AS S_Contract_Date, 
S.Date_Ship 
FROM BALES B INNER JOIN SHIPMENTS S 
ON S.ID = B.Shipment_ID INNER JOIN Partners P 
ON P.ID = S.Partner_ID ORDER BY S.Date_Ship `

Got some reading to do!

Thanks Dave and Norman

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [September 23, 2019, 10:40pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/7 "2019-09-23T22:40:05Z")

</div>

Still can´t get this to work. I removed all the Aliases from the columns and tables list, here is the Xojo code I´m attempting to run. I know… I know… this is just a test, I will code it better as soon as I get the SQL syntax to work. This too works on a

Here is my xojo code:

[code]SQL = "?SELECT B.ID, P.Code, B.Permanent\_Bale\_ID, "  
SQL = SQL + "B.Bale\_Storage\_Location, "  
SQL = SQL + "S.Contract\_Number, S.Contract\_Date, "  
SQL = SQL + "S.Date\_Ship "  
SQL = SQL + "FROM BALES B "  
SQL = SQL + "INNER JOIN SHIPMENTS S "  
SQL = SQL + "ON S.ID = B.Shipment\_ID "  
SQL = SQL + "INNER JOIN Partners P "  
SQL = SQL + "ON P.ID = S.Partner\_ID "  
SQL = SQL + "ORDER BY S.Date\_Ship DESC "

// I know I should not use this for debbuging 🙂  
MsgBox(“Load Bales sql” + EndOfLine + SQL)

Dim RS As RecordSet  
RS = App.DB.SQLSelect(SQL)

If App.DB.Error Then  
MsgBox(“Error reading the Bales list” + EndOfLine + \_  
App.DB.ErrorCode.ToText + " - " + App.DB.ErrorMessage + EndOfLine + \_  
“Bales\_Container - Load\_Bales”)  
Exit  
Else[/code]

Help? 😉

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [September 23, 2019, 10:52pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/8 "2019-09-23T22:52:59Z")

</div>

It might help to get an SQLite Database manager to test your query  
Personally I suggest [Tadpole](http://www.rdS.com/tadpole) but full disclosure… I wrote it 🙂

One of my current apps has a query that is well over 100 lines long (lots of UNION)… and Tadpole saved me hours as I could test sections to find the error…

Once each section worked… I put it into Xojo like you did above…

Suggestion… you might find this format easier to read

```auto
SQL = _
"?SELECT B.ID, P.Code, B.Permanent_Bale_ID, " +_
" B.Bale_Storage_Location, " +_
 " S.Contract_Number, S.Contract_Date, " +_
              "S.Date_Ship " +_
 "FROM BALES B " +_
 "INNER JOIN SHIPMENTS S " +_
 " ON S.ID = B.Shipment_ID " +_
 "INNER JOIN Partners P " +_
 " ON P.ID = S.Partner_ID " +_
 "ORDER BY S.Date_Ship DESC "
```

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [September 23, 2019, 11:16pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/9 "2019-09-23T23:16:53Z")

</div>

[quote=455141:@Dave S]It might help to get an SQLite Database manager to test your query  
Personally I suggest [Tadpole](http://www.rdS.com/tadpole) but full disclosure… I wrote it 🙂

One of my current apps has a query that is well over 100 lines long (lots of UNION)… and Tadpole saved me hours as I could test sections to find the error…

Once each section worked… I put it into Xojo like you did above…

Suggestion… you might find this format easier to read

`
SQL = _
"?SELECT B.ID, P.Code, B.Permanent_Bale_ID, " +_
" B.Bale_Storage_Location, " +_
 " S.Contract_Number, S.Contract_Date, " +_
              "S.Date_Ship " +_
 "FROM BALES B " +_
 "INNER JOIN SHIPMENTS S " +_
 " ON S.ID = B.Shipment_ID " +_
 "INNER JOIN Partners P " +_
 " ON P.ID = S.Partner_ID " +_
 "ORDER BY S.Date_Ship DESC "
`[/quote]

I´ll get Tadpole! I have tested the SQL code using DB Browser for SQLite Database manager and it runs OK in it. I´ll disect the SQL syntax bit by bit. Your formatting suggestion looks good, will try!

---

<div class="post-metadata">

**Author:** ![Jay\_Madren](https://forum.xojo.com/user_avatar/forum.xojo.com/jay_madren/32/6425_2.png) [@Jay\_Madren](https://forum.xojo.com/u/Jay_Madren)\
**Post date:** [September 23, 2019, 11:19pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/10 "2019-09-23T23:19:02Z")

</div>

[quote=455137:@Hector Marroquin]I´m getting a  
1 - near “?SELECT”: syntax error[/quote]  
Error Code 1 is a “SQL logic error or missing database”. Since the SQL looks fine, you should make sure you’re connecting to the correct db file.

---

<div class="post-metadata">

**Author:** ![Jay\_Madren](https://forum.xojo.com/user_avatar/forum.xojo.com/jay_madren/32/6425_2.png) [@Jay\_Madren](https://forum.xojo.com/u/Jay_Madren)\
**Post date:** [September 23, 2019, 11:22pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/11 "2019-09-23T23:22:07Z")

</div>

Also try a simple “Select \* from BALES” to see if you get an error, and each of the other tables.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [September 23, 2019, 11:27pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/12 "2019-09-23T23:27:10Z")

</div>

Hector, you have “invisible” characters on your code. I guess you copied the SELECT statement from some app and pasted directly to Xojo.

I think if you use the option to “clean invisible ascii characters”, then your code will work.

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [September 23, 2019, 11:27pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/13 "2019-09-23T23:27:23Z")

</div>

> [@455145:@Jay Madren](#):
>
> Also try a simple “Select \* from BALES” to see if you get an error, and each of the other tables.

Thanks for your reply. The SELECT \* FROM Bales was the first thing I tested and it works fine, right now I´m rebuilding the SQL statement bit by bit, so far this simple join is working:

```auto
SELECT B.ID AS BALE_ID, B.Permanent_Bale_ID FROM Bales AS B INNER JOIN Shipments AS S ON B.SHIPMENT_ID = S.ID 
```

I´ll test a multi table join next.

Thanks again for your response

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [September 23, 2019, 11:29pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/14 "2019-09-23T23:29:03Z")

</div>

[quote=455147:@Alberto DePoo]Hector, you have “invisible” characters on your code. I guess you copied the SELECT statement from some app and pasted directly to Xojo.

I think if you use the option to “clean invisible ascii characters”, then your code will work.[/quote]

Ohhhhh this never crossed my mind! Will check right now!

---

<div class="post-metadata">

**Author:** ![Hector\_Marroquin](https://forum.xojo.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Hector\_Marroquin](https://forum.xojo.com/u/Hector_Marroquin)\
**Post date:** [September 23, 2019, 11:36pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/15 "2019-09-23T23:36:35Z")

</div>

[quote=455147:@Alberto DePoo]Hector, you have “invisible” characters on your code. I guess you copied the SELECT statement from some app and pasted directly to Xojo.

I think if you use the option to “clean invisible ascii characters”, then your code will work.[/quote]

Thanks Alberto! You saved me a night of no sleep. I indeed copied the code from a SQLite manager where I wrote it, never thought I was getting invisible characters.

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [September 23, 2019, 11:43pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/16 "2019-09-23T23:43:29Z")

</div>

wow… no SQLite Managment program should even allow such a thing to occur ☹

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [September 23, 2019, 11:47pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/17 "2019-09-23T23:47:58Z")

</div>

> [@455158:@Dave S](#):
>
> wow… no SQLite Managment program should even allow such a thing to occur ☹

From my tests, several do that, don’t know why.

Glad my time wasted some time ago could help Hector today 😉

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [September 25, 2019, 10:26am UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/18 "2019-09-25T10:26:02Z")

</div>

Usually, Xojo remove gremlin characters

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [September 25, 2019, 12:24pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/19 "2019-09-25T12:24:12Z")

</div>

> [@455298:@Emile Schwarz](#):
>
> Usually, Xojo remove gremlin characters

I think you need to manually do it, I don’t know if there is an automatic setting:  
[![](https://i.ibb.co/98Qh4Xr/image.png)](https://imgbb.com/)

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [September 25, 2019, 4:13pm UTC](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357/20 "2019-09-25T16:13:13Z")

</div>

Nice !

[Next page](https://forum.xojo.com/t/help-with-sql-syntax-on-sqlite/50357.md?page=2)
