News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Question about the API SQLExecute

Started by Rem, June 01, 2025, 11:17:59 AM

Previous topic - Next topic

Rem

Hello Masm team

I'm trying redoing a Tasm ODBC project into Masm x64.

Instate of using Microsoft Database I'm using SQLite3.

I'm able to run successfully a SQL query in "szSQLStatement" using the API "SQLExecDirect"

I'm having an issue when I use the API "SQLExecute"

On the Query Dialog I'm searching by First Name.
When I enter a First Name no information get fetch.

My code in the init Dialog

  szSQLStatement  db  "SELECT * FROM MAIN",0
  szWhereClause   db  " WHERE FIRSTNAME='?'",0
...
  szSearchName db  20  dup(?)
  szSQLQueryBuffer db 256 dup (?)
...
 invoke lstrcpy, addr szSQLQueryBuffer, addr szSQLStatement
 invoke lstrcat, addr szSQLQueryBuffer, addr szWhereClause
...
 invoke SQLBindParameter, hStmt, 1, SQL_PARAM_INPUT, SQL_CHAR, SQL_CHAR, 20, 0, addr szSearchName, 20, addr dqStrLen
 invoke SQLPrepare, hStmt , addr szSQLQueryBuffer, SQL_NTSL
...

My code for the Button control

 invoke GetDlgItemText, hDlg, IDC_EDIT, addr szSearchName, 20
...
 mov dqStrLen, rax
...
 invoke SQLExecute, hStmt
;---------------------------------------

I check each API and all of them return the result "SQL_SUCCESS"

About the API "SQLBindParameter", I try to change SQL_CHAR for SQL_VARCHAR and doesn't work.

One of my test I try is to change: szWhereClause   db  " WHERE FIRSTNAME='Bob'",0
And it work, the name "Bob" show up.

The API "SQLPrepare" should change '?' for 'Bob' in the buffer....

I will need your advise on what I'm doing wrong.

Thank you for your help.




sinsi

I can't build this, can you build a debug version and upload it?
Initial thought is a unicode/ansi conflict, but debugging should show more.

My background is SQL Server.

fearless

what version of sqlite are you using? those api calls look like an older version.

An example using close to the latest version of sqlite for a parameterized query might look like:

.DATA

szTestDB                        DB "test.db",0

SQL_SelectedCarsID_Param        \
                                DB "SELECT Id, Name FROM Cars WHERE Id = ?"
                                DB 0,0

lpszSQLiteColumnText0          DD 0 ; pointer to string received from sqlite3_column_text call
lpszSQLiteColumnText1          DD 0 ; pointer to string received from sqlite3_column_text call

szRowDataBuffer                DB 1024 DUP (0)


.CODE
;------------------------------------------------------------------------------
; SQLite_Parameterized
;
; Using a parameterized query (also called a prepared statement) to retrieve
; a specific row of data (row 3 in this example) from the 'Cars' table in the
; 'test.db' database.

; Parameterized queries use placeholders instead of directly writing the
; values into the statements.

; A question mark (?) is used as a placeholder which is later replaced with an
; actual value.
;
;  "SELECT Id, Name FROM Cars WHERE Id = ?"
;
;------------------------------------------------------------------------------
SQLite_Parameterized PROC
    LOCAL database:sqlite3
    LOCAL result:sqlite3_stmt
    LOCAL err_msg:DWORD
    LOCAL step:DWORD
   
    mov err_msg, 0
   
    Invoke sqlite3_open, Addr szTestDB, Addr database
    .IF eax != SQLITE_OK
        ; Ideally we would output the error message here
        Invoke sqlite3_close, database
        mov eax, FALSE
        ret
    .ENDIF
   
    Invoke sqlite3_prepare_v2, database, Addr SQL_SelectedCarsID_Param, -1, Addr result, 0
    .IF eax != SQLITE_OK
        ; Ideally we would output the error message here
        Invoke sqlite3_close, database
        mov eax, FALSE
        ret
    .ENDIF
   
    Invoke sqlite3_bind_int, result, 1, 3 ; using id of 3 with the first parameterized value (the ? in this case)
   
    Invoke sqlite3_step, result ; execute query
    mov step, eax
    .IF step == SQLITE_ROW
        Invoke sqlite3_column_text, result, 0
        mov lpszSQLiteColumnText0, eax
        Invoke sqlite3_column_text, result, 1
        mov lpszSQLiteColumnText1, eax
       
        Invoke lstrcpy, Addr szRowDataBuffer, lpszSQLiteColumnText0
        Invoke lstrcat, Addr szRowDataBuffer, Addr szSPACE
        Invoke lstrcat, Addr szRowDataBuffer, lpszSQLiteColumnText1
        ; Ideally we would output the result of the query with the text from columns 0 and 1 of the result.
    .ENDIF
   
    Invoke sqlite3_finalize, result
    Invoke sqlite3_close, database
   
    mov eax, TRUE
    ret
SQLite_Parameterized ENDP

zedd

#3
fixed line 510 to
; szWhereClause   db  " WHERE FIRSTNAME='Bob'",0It had a quotation mark (22h) where there should have been a semicolon, assuming it was meant to be commented out.
Now assembles..,

But does not link
Quote: fatal error LNK1104: cannot open file '\masm64\bin64\stubby.exe'

What is 'stubby.exe'? It is not part of the masm64 SDK. WHere is it from??
If it is an external file needed by the program (or needed by the linker), it should be included in the zip file.

I removed reference to 'stubby.exe' from the linker command line and it assembles and links - using ml64 and link.
... and the horse that you rode in on...  :badgrin:  :biggrin:

sinsi

You seem to reuse statement handles but close them seemingly arbitrarily.

I couldn't build the 64-bit code, MASM64 ODBC includes seem to have no "A" functions, only "W".
I now have a working EXE using my 64-bit includes, will update things later.