gb.db2 Order by

Post

Posted
Rating:
#1 (In Topic #1437)
Regular
Andreas_K is in the usergroup ‘Regular’
 Hello, since i updatet from gb.db to db.db2 the order by in the sql statement dont work anymore, what i have to change?
I have also the problem, when i have a combobox and i open the popup (in the combobox), select a entry and then i enter in the field a text, the keypress event is not working, until i put the focus to another control and then i go back. The same problem i have in the IDE, from time to time, i need to select again the module or class, otherwise the keyboard is not working. I have this issue on Ubuntu 24.04 and on Ubuntu 24.10 (x86_64 and arm64). Anyone a idea what is the problem?
Thanks
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
I'll leave the database problem to those with better knowledge on that subject.

Regarding your "Click" problem, try  Public Sub ComboBox1_Activate() not Public Sub ComboBox1_Click(). I think that will help you.
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
Andreas_K is in the usergroup ‘Regular’
Thanks, sorry i wrote wrong, key_release is not working, key_press works.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 give us an example of how you are doing the order by query.
I have had no problems at all with db2,
b

Online now: No Back to the top

Post

Posted
Rating:
#5
Banned
 I do not get any key_release issues with combobox

But I often find i have typed something in a class/module but it did not appear because the editor was not focused and i have to click on the document to use the keyboard.

This happens sometimes after hitting the "run" button to test a program. (and possibly other IDE forms opening then closing)
It's simply (annoyingly) after a test run the editor does not regain focus and needs to be clicked.

It maybe a toolkit issue , try the IDE (and your program) with QT and see if it still happens.
env GB_GUI=gb.gui.qt gambas3
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
Andreas_K is in the usergroup ‘Regular’

Code (gambas)

  1. Public Function ImportDB(sTable As String, sField1 As String, sField2 As String, Optional sWhere As String) As Boolean
  2.  
  3.    Dim Res As Result
  4.    Dim sSql As String = DB.Subst("SELECT * FROM &1 ORDER BY '&2' ", sTable, sField1)
  5.  
  6.    If sWhere <> "" Then
  7.       ssql = db.Subst("SELECT * FROM &1 " & sWhere & " ORDER BY '&2' ", sTable, sField1)
  8.    Endif
  9.  

With gb.db works, but with gb.db2 not.
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
Andreas_K is in the usergroup ‘Regular’
Today Key_Press don't work with Key.UP and Key.DOWN, i need to put the code  in Key_Release to work? after Key_Release is worked out the Click Event is fired??.
I have a combobox with there i search in a gridlist the data, with up and down i scroll the gridlist.

Code (gambas)

  1. Public Sub cbosearch_KeyRelease()
  2.    ' Message(Key.Code)
  3.  
  4.    If mGridlist.aList.Count = 0 Then Return
  5.  
  6.    Dim iIndex As Integer
  7.  
  8.    For i As Integer = 0 To GridList.Rows.Count - 1
  9.       If GridList.Rows[i].Selected Then
  10.          iindex = i
  11.          i = GridList.Rows.Count - 1
  12.       Endif
  13.    Next
  14.  
  15.    Select Case Key.Code
  16.       Case Key.Enter, Key.Return 'Enter
  17.          iIndex = mGridlist.FindIndexofText(cboSearch.Text)
  18.  
  19.          If iindex = -1 Then Return
  20.          GridList.Scroll(0, GridList.Current.Y)
  21.          Dim Result As String = GridList.Current.Text
  22.          cbosearch.Text = Result
  23.          'Show Record
  24.          If GridList.Row = -1 Then Return
  25.          GridSelected(GridList[GridList.Row, 0].Text)
  26.          Return
  27.       Case Key.Up
  28.          If iindex > 1 Then
  29.             iindex = iindex - 1
  30.             GridList.Row = iindex
  31.             GridList.Scroll(0, GridList.Current.Y)
  32.             Result = GridList.Current.Text
  33.             cbosearch.Text = Result
  34.             Return
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
Poly is in the usergroup ‘Guru’
Poly is in the usergroup ‘GambOS Contributor’

Andreas_K said

Code (gambas)

  1. Public Function ImportDB(sTable As String, sField1 As String, sField2 As String, Optional sWhere As String) As Boolean
  2.  
  3.       ssql = db.Subst("SELECT * FROM &1 " & sWhere & " ORDER BY '&2' ", sTable, sField1)
  4.    Endif
  5.  

With gb.db works, but with gb.db2 not.

Hello, I am not an expert, but I am currently experimenting with an SQL database.
So far I have no problems with gb.db2
But I immediately notice something in your code.
You have not quoted correctly in db.Subst.
If you want to substitute a table name, you have to put it in square brackets.

See /doc/db-quoting - Gambas Documentation

"If a substitution pattern in enclosed with square brackets, then the argument is supposed to be a table name, and the Connection.Quote method is used to correctly quote the table name into the result string. "

so you have to write

Code

 ssql = db.Subst("SELECT * FROM [&1]"  & sWhere &  "ORDER BY '&2' ", sTable, sField1)

Try it out to see if this fixes the error.

Best regards
Poly
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Quincunxian is in the usergroup ‘Blogger’
Hi Poly,
Just a best practice thing - I'd recommend NOT using the name 'sWHERE' as a variable as it's a SQL key word and it makes it somewhat confusing when you look at the statement.

Can you give me an example of what you would pass in sWhere please, as I can't quite make sense of your SQL statement.

I'm not an expert but I do write a lot of MySql / SQLite based applications in Gambas.

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
This is a perfect example of what happens when the original post contains two questions!

Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
Poly is in the usergroup ‘Guru’
Poly is in the usergroup ‘GambOS Contributor’
Hi Quincunxian

Quincunxian said


Can you give me an example of what you would pass in sWhere please, as I can't quite make sense of your SQL statement.


I have simply taken Andreas' example and tried to quote correctly.
The sWhere doesn't make sense to me in this context either, but he wrote that this example worked for gb.db.
That's why I assumed that it could just be a problem with the quoting. As explained in my link to Wiki.

But it is not.

The following works for me with gb.db and gb.db.form but not with gb.db2 and gb.db2.form

Code (gambas)

  1.   rs = DB.Subst("SELECT * From [&1] WHERE '&2' = &3 Order by '&4' DESC", "Namen", "Vorname", "Risa", "Nachname")

So I can confirm the problem.
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
why bother with .subst ?
I use sqlite not sql … is there a diffenance ?….   Try this :

Code (gambas)

  1. rs = DB("SELECT * From" & Quote(Namen) & "WHERE" & Quote(Vorname) & " = " & Quote(Risa) " Order by " & Quote(Nachmame) & " DESC")  

Or maybe all the variables need to be in brackets >   [&3]
Online now: No Back to the top

Post

Posted
Rating:
#13
Guru
Poly is in the usergroup ‘Guru’
Poly is in the usergroup ‘GambOS Contributor’

grayghost4 said


Try this :

Code (gambas)

  1. rs = DB("SELECT * From" & Quote(Namen) & "WHERE" & Quote(Vorname) & " = " & Quote(Risa) " Order by " & Quote(Nachmame) & " DESC")  

Many thanks,

I I had to adapt your code a bit, but it doesn't work.

Code (gambas)

  1. rs = DB("SELECT * From" & Quote("Namen") & "WHERE" & Quote("Vorname") & " = " & Quote("Risa") & "Order by" & Quote("Nachname") & " DESC")

with gb.db2 I get: Typ missmatsch wanted DB, got String instead  
with gd.db I get: not a function

I think there is a problem with gd.db2 or it works differently.
I get even with the simplest form that works normally under gd.db, with gd.db2:

rs = DB.Subst("SELECT * FROM [&1]", "Namen")

the following error.

No such table: 'Name'

But I have to check this again in a programme without IDE. Because I use a data browser to display it.
That's why the code looks exactly like this for me:

Code

DataSource1.Table = DB.Subst("SELECT * FROM [&1]", "Namen")

Works perfectly with gd.db and gb.db.form but not with gb.db2 and gb.db2.form

Thank you very much for your attention.

Best regards
Poly
Online now: No Back to the top

Post

Posted
Rating:
#14
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
Or maybe all the variables need to be in brackets > [&3]   if that puts them in " " quotes.

edit … I see that does not work  ;)
Online now: No Back to the top

Post

Posted
Rating:
#15
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
This is my code which work with both versions

Code (gambas)

  1. Public Sub GetRecordData(sFileName As String, sFileColumn As String, sFileIndex As String) As String[]
  2.    
  3.    Dim saInfo As New String[]    ' must have "NEW" or you will get a null error with .add
  4.    Dim obj As ResultField
  5.    
  6.    RS = $con.Exec("Select * From" & Quote(sFileName) & "where" & Quote(sFileColumn) & "like" & Quote(sFileIndex))
  7.    
  8.    For Each obj In RS.Fields
  9.       sainfo.Add(RS[obj.Name])
  10.    Next
  11.    Return saInfo
  12.    
Online now: No Back to the top

Post

Posted
Rating:
#16
Guru
Poly is in the usergroup ‘Guru’
Poly is in the usergroup ‘GambOS Contributor’
In my case it recognises it as a table, but says it doesn't know this table.

So it is definitely a different behaviour due to the new component.
It may be that my code is somehow badly built, I will test this again in a very simple database without an IDE.
So far I can't say that this is a bug or error.

I will test your code immediately :)

Thanks Poly
Online now: No Back to the top

Post

Posted
Rating:
#17
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
This is the complete module "  

Code (gambas)

  1. ' Gambas module file
  2.  
  3. '' Module file to handle SQLite file
  4. '' SQLite  file handeling subroutines
  5. '' writen by  GrayGhost4  Marvin Clavey  June 2022
  6.  
  7. Private sadata As String[]
  8.  
  9. Public Sub GetFileData(sfilename As String, scolumnName As String) As String[]
  10.    
  11.    With $con
  12.       Try .Close()             ' Close the connection. The try allows it to fail without error
  13.       .Type = "sqlite3"        ' Defines the type of connection
  14.       .Host = Application.path ' Host will be the path where the sqlite db file is
  15.       .Name = "data.SQL"  '  database name is the name of the database file"
  16.       Try .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.       RS = .Exec("Select * From " & sfilename)
  19.    End With
  20.    Return RS.All(scolumnName)   ' return all the data from one column
  21.    
  22.  
  23. Public Sub GetRecordData(sFileName As String, sFileColumn As String, sFileIndex As String) As String[]
  24.    
  25.    Dim saInfo As New String[]    ' must have "NEW" or you will get a null error with .add
  26.    Dim obj As ResultField
  27.    
  28.    RS = $con.Exec("Select * From" & Quote(sFileName) & "where" & Quote(sFileColumn) & "like" & Quote(sFileIndex))
  29.    
  30.    For Each obj In RS.Fields
  31.       sainfo.Add(RS[obj.Name])
  32.    Next
  33.    Return saInfo
  34.    
  35.  
  36. Public Sub UpdateFileData(sFileName As String, sFileIndex As String, saInfo As String[])
  37.    
  38.    Dim saCommand As String  
  39.    Dim obj As ResultField
  40.    
  41.    saInfo = saInfo.Reverse()
  42.    For Each obj In RS.Fields                'assemble the update command "column" = "data"
  43.       saCommand &= Quote(obj.Name) & " = " & Quote(saInfo.Pop()) & ","
  44.    Next
  45.    saCommand = Left(saCommand, -1)        ' remove the last ","  from saCommand string
  46.    $con.Exec("update" & Quote(sFileName) & "set" & saCommand & "where accname = " & Quote(sFileIndex))
  47.    
  48.  
  49. Public Sub InsertNewRecord(sFileName As String)
  50.    
  51.    Dim sNewRecord As String
  52.    
  53.    For Each obj As Object In RS.Fields    ' This assenbles the File column names it to A string
  54.       sNewRecord &= Quote(obj.Name) & "," ' to insert into a blank record to be edited later
  55.    Next
  56.    sNewRecord = Left(sNewRecord, -1)         'remove the last ","  from the string
  57.    sNewRecord = Replace(sNewRecord, "accname", "newAcc-" & Str(Rand(20, 100)))  ' replace first field (random) for index
  58.    $con.Exec("INSERT INTO " & Quote(sFileName) & " VALUES (" & sNewRecord & ")")
  59.    
  60.  
  61. Public Sub deleatOneRecord(sFileName As String, sFileField As String, sFileIndex As String)
  62.    '  Deleat one reccord from the table with an indexed coluem
  63.    
  64.    $con.Exec("delete from" & Quote(sFileName) & "where" & Quote(sFileField) & " = " & Quote(sFileIndex))
  65.    
  66.  
Online now: No Back to the top

Post

Posted
Rating:
#18
Guru
Poly is in the usergroup ‘Guru’
Poly is in the usergroup ‘GambOS Contributor’
Hi Graygost

It works as you said, but then of course the DB for the string has to be removed.
This is also how it works for me with gb.db2

Code (gambas)

  1. DataSource1.Table = "SELECT * From" & Quote("Namen") & "WHERE" & Quote("Vorname") & " = " & Quote("Risa") & "Order by" & Quote("Nachname") & " DESC"


Thank you very much.

But then it is a bug or not.
So the substitution with Db.Subst does not work.
That shouldn't be the case, should it?
Online now: No Back to the top

Post

Posted
Rating:
#19
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
 how about trying to Quote the subitutions

Quote(&3) ….

also I dont think you need the " " around the varable name… that is what Quote() does
Online now: No Back to the top

Post

Posted
Rating:
#20
Guru
Poly is in the usergroup ‘Guru’
Poly is in the usergroup ‘GambOS Contributor’
Your code works perfectly and the code I gave works with gd.db and is also described in the wiki exactly the same way.

/doc/db-quoting - Gambas Documentation

Of course it is nonsense that there are two asterisks ** , of course there is only one, which stands for all columns.

Therefore, I would now clearly say that it is a bug in the DB.Stubst function.
But it would be great if someone else could confirm this.


And I have to set the inverted commas in the quote functions, otherwise I get an error.

unknown identifier

Your code is brilliant. Thank you very much.
Online now: No Back to the top

Post

Posted
Rating:
#21
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
I got some of the substitutions working

Code (gambas)

  1.  
  2.    $con.Exec(Subst("delete From  [&1] where  [&2] = " & Quote(sFileIndex), sFileName, sFileField))
  3.  
  4.    ' the above line working for the lower line ... but can't get the third sub to work  :(
  5.  
  6.    ' $con.Exec("delete from" & Quote(sFileName) & "where" & Quote(sFileField) & " = " & Quote(sFileIndex))
Online now: No Back to the top

Post

Posted
Rating:
#22
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
also found you don't need the [ ]

Code (gambas)

  1.    $con.Exec(Subst("delete From  &1 where  &2 = " & Quote(sFileIndex), sFileName, sFileField))

but I can't get the third Subst to work
Online now: No Back to the top

Post

Posted
Rating:
#23
Regular
Andreas_K is in the usergroup ‘Regular’
The only way that is working on my machine is:

Code (gambas)

  1. Public Function ImportDB(sTable As String, sField1 As String, sField2 As String, Optional sFilter As String) As Boolean
  2.  
  3.    Dim Res As Result
  4.    Dim sSql As String = DB.Subst("SELECT * FROM &1 ORDER BY " & sField1, sTable)
  5.  
  6.    If sFilter <> "" Then
  7.       ssql = db.Subst("SELECT * FROM &1 " & sFilter & " ORDER BY " & sField1, sTable)
  8.    Endif
  9.  
  10.    res = db.Exec(sSql)

Order by works only without " ' " (Apostrophe) and db.subst put this evertime, so its not working.

https://gambaswiki.org/wiki/doc/db-quoting with this i get only error, table not found….

Sorry for the two questions in one post.
Online now: No Back to the top

Post

Posted
Rating:
#24
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Ah, the fundamental misunderstand I believe that you may have is that DB.Subst is like Subst. It's not, nor was it ever intended to be.
It is for quoting values in SQL clauses in a way that Gambas can submit properly quoted sql.
If you want to insert bare strings into your sql then you must do it in 2 (or more)  steps. Using Subst for the bare strings and using DB.Subst for actual values.

hth
b

Online now: No Back to the top

Post

Posted
Rating:
#25
Guru
Poly is in the usergroup ‘Guru’
Poly is in the usergroup ‘GambOS Contributor’
Hi thatbruce

thatbruce said

Ah, the fundamental misunderstand I believe that you may have is that DB.Subst is like Subst. It's not, nor was it ever intended to be.
It is for quoting values in SQL clauses in a way that Gambas can submit properly quoted sql.

However, this does not explain why it is shown differently in the wiki and why it runs in gb.db but not with gb.db2

It may be that some people here are only interested in a certain code to make something work. Not for me. I thought we should all be interested in finding and reporting errors or misunderstandings in the wiki or bugs in the individual components.

So if

Code (gambas)

  1. [DataSource1.Table = DB.Subst("SELECT * From [&1] WHERE '&2' = &3 Order by '&4' DESC", "Namen", "Vorname", "Risa", "Nachname")
works for me fine with gb.db and gb.db.form, but not with gb.db2 and db.gb2.form, then something must have changed in the Db.Subst function in the two components.

But if this works for you, then it may also be due to my system.

I am using Devuan , x11 and Gambas 3.20.2

Best regards Poly
Online now: No Back to the top
1 guest and 0 members have just viewed this.