B4J Library [B4X] [B4XLib] SD_SQL (direct access to MySQL, MariaDB, MS SQL, FireBird)

Star-Dust

Expert
Licensed User
Longtime User
Use SQL commands, in this case INSERT INTO
 

swamisantosh

Member
Licensed User
Longtime User

sorry for HUGE FONT...

Thanks you very much the issue is resolved.

Dim MSSQL As SD_SQL
Private MSLocation As String = "192.168.43.191/global;loginTimeout=30;"
Private MSUsername As String = "sa"
Private MSPassword As String = "solution.123"

Log("---------- NorthWind Database (MSSQL) ----------")
MSSQL.Initialize(Me,"MSSQL","net.sourceforge.jtds.jdbc.Driver", $"jdbc:jtds:sqlserver://${MSLocation}"$, MSUsername, MSPassword)

may it will benefit other.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Log("---------- NorthWind Database (MSSQL) ----------")
MSSQL.Initialize(Me,"MSSQL","net.sourceforge.jtds.jdbc.Driver", $"jdbc:jtds:sqlserver://${MSLocation}"$, MSUsername, MSPassword)
Please use [CODE]code here...[/CODE] tags when posting code.



 

Star-Dust

Expert
Licensed User
Longtime User
Hi, can you also include an example for other backends for example SQLite or it will work with the current implementation?

Thanks
It is exclusive to MS SQL, MySQL
 

asales

Expert
Licensed User
Longtime User
It is exclusive to MS SQL, MySQL
Tested with Firebird database and works fine:
- B4J: jaybird-full-3.0.8.jar
- B4A: Jaybird_2_2_12.aar
B4X:
Dim FBSQL As SD_SQL
Private FBUsername As String = "SYSDBA"
Private FBPassword As String = "masterkey"

FBSQL.Initialize(Me,"FBSQL", "org.firebirdsql.jdbc.FBDriver","jdbc:firebirdsql:127.0.0.1/3050:" & "E:\Projetos\Firebird\B4X.FDB", FBUsername, FBPassword)
 

Star-Dust

Expert
Licensed User
Longtime User
Do my libraries also work for purposes I didn't design them for? Good to know
 

MicroDrie

Well-Known Member
Licensed User
You can try it on two ways. The result of the first routine has count the row in the table and the second routine gives the row count based on a element in the table.

Test for table and table row count:
'    --- Tests whether the given table exists
Public Sub TableExists(SQL As SQL, TableName As String) As Boolean
    Dim count As Int = SQL.ExecQuerySingleResult2("SELECT count(name) FROM sqlite_master WHERE type='table' AND name=? COLLATE NOCASE", Array As String(TableName))
    Return count > 0
End Sub

'    --- Get table row count
Public Sub getTableExists(SQL As SQL, TableName As String) As Int
    Dim Length As Int = SQL As SQL.ExecQuerySingleResult($"SELECT count(name) FROM sqlite_master WHERE type='table' AND name ='${TableName}'"$)
    Log($"${TableName} Length = ${Length}"$)
    Return Length
End Sub
 

Star-Dust

Expert
Licensed User
Longtime User
The ROWCOUNT is missing.
How do I know if the SELECT has not returned anything to me?
There is no RowCount in the Java ResultSet. The only way is to count the lines by spelling them or with a Query that counts the results as @MicroDrie suggested.
 

Star-Dust

Expert
Licensed User
Longtime User
@Star-Dust , this I assume does not use connection pools or does it? Just curious in terms of it handle a large number of concurrent users.
It's just a connector that uses jar drivers.
I believe the pool is about the server
 

amorosik

Expert
Licensed User

For B4J also jaybird-full-4.0.0.java8.jar is ok
 
Hello first of all thank you very much for this contribution I am amazed with the way of connection to the database especially MySQL and Firebird, but how can we use the statements for INSERT, UPDATE, DELETE? I would greatly appreciate a contribution on this.
 

Alex.G

Member
Hi guys,
I'm Alex from Italy.
I'm a Delphi, C++ and Java coder and two months ago I discovered this wonderful community. I am fascinated by the enormous potential of the B4X environment and I thank Erel for what he has done and what he does.
I've no a jRDC server (for now), so I tryed this nice library.

I've a connection problem with SD_SQL.

If I compile in debug mode all runs OK.
If I compile in release mode I receve connection error:

Not connected: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544721. Unable to complete network request to host "192.168.0.99".

Where am I doing wrong?


B4X:
'In Main module:

'FirebirdSQL Connector/J Driver
#AdditionalJar: Jaybird_2_2_12.aar

'In B4XMainPage:

Private Sub B4XPage_Created (Root1 As B4XView)
    Dim FBSQL      As SD_SQL
    Dim FBUsername As String = <my username>
    Dim FBPassword As String = <my password>
    Dim FBDriver   As String = "org.firebirdsql.jdbc.FBDriver"
    Dim FBPath As String = "jdbc:firebirdsql:192.168.0.99/3050:C:\winwork\Cerbero\DB\CER00014.GDB"

    Try
        Main.FBSQL.Initialize(Me,"FBSQL", FBDriver, FBPath, FBUsername, FBPassword)
    Catch
        MsgboxAsync("LastException: "&LastException, "")
    End Try

End Sub

Sub FBSQL_Ready (Success As Boolean)

    If Success Then
        Log("OK")
    Else
        Log("...KO")
    End If
    
End Sub
 

Alex.G

Member
with function ExecNonQuery

B4X:
Dim SQL as String = $"UPDATE <table name> SET <field1> = '${<string1>}', <field2> = '${<string2>}' WHERE <field3> = '${<string3>}'"$

FBSQL.ExecNonQuery(SQL)
 

Star-Dust

Expert
Licensed User
Longtime User
What kind of error do you get? Did you make a log?
 

Alex.G

Member
What kind of error do you get? Did you make a log?
Hi Star-Dust, here the log:

B4X:
Logger connesso a: 57U7N17915028375
--------- beginning of crash
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Not connected: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544721. Unable to complete network request to host "192.168.0.99".
 

Alex.G

Member
correction at may post #36
at row 16 (after Try)

B4X:
FBSQL.Initialize(Me,"FBSQL", FBDriver, FBPath, FBUsername, FBPassword)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…