Android Question crud with Microsoft SQL Server????

npsonic

Active Member
Licensed User
You can find more info from tutorial Directly connect to remote databases.

This is just a small mod from that example and I have not tested this example, but you get the idea. You need to also download jtds and jdbcSQL libs.

Add these to your Starter service
B4X:
Sub Process_Globals
    Public mssql As JdbcSQL
    Private driver As String = "net.sourceforge.jtds.jdbc.Driver"
    Private jdbcUrl As String = "jdbc:jtds:sqlserver://192.168.0.6/test"
    Private Username As String = "username"
    Private Password As String = "password"
End Sub

Sub Service_Create
    DisableStrictMode
End Sub

Sub DisableStrictMode
    Dim jo As JavaObject
    jo.InitializeStatic("android.os.Build.VERSION")
    If jo.GetField("SDK_INT") > 9 Then
        Dim policy As JavaObject
        policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
        policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
        Dim sm As JavaObject
        sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
    End If
End Sub

Sub Connect As ResumableSub
    mssql.InitializeAsync("mssql", driver, jdbcUrl, Username, Password)
    Wait For MSSQL_Ready (Success As Boolean)
    If Success = False Then
        Log("Check unfiltered logs for errors.")
    End If
    Return Success
End Sub

Sub CloseConnection
     mssql.Close
End Sub

Sub NonQueryAsync (NonQuery As String) As ResumableSub
    Wait For (Connect) Complete (Success As Boolean)
    If Success Then
        Try
            mssql.ExecNonQuery(NonQuery)
        Catch
            Success = False
            Log(LastException)
        End Try
        CloseConnection
    End If
    Return Success
End Sub

Sub QueryAsync (Query As String) As ResumableSub
    Wait For (Connect) Complete (Success As Boolean)
    Dim res As Object
    If Success Then
        Try
            Dim eqa As Object = mssql.ExecQueryAsync("mysql", Query, Null)
            Wait For (eqa) mysql_QueryComplete (Success As Boolean, Crsr As JdbcResultSet)
            If Success Then
                res = Crsr
            End If
        Catch
            Log(LastException)
        End Try
    End If
    Return res
End Sub

and here's some examples. Use these in your Main code module
B4X:
Sub Example1
    Wait For (CallSub2(Starter,"NonQueryAsync","CREATE TABLE testing (val1 nvarchar(5) NULL, val2 smallint NOT NULL)")) Complete (Success As Boolean)
    Log("example1 success=" & Success)
End Sub
Sub Example2
    Wait For (CallSub2(Starter,"NonQueryAsync","INSERT INTO testing val1,val2 VALUES ('A',1),('B',2),('C',3)")) Complete (Success As Boolean)
    Log("example2 success=" & Success)
End Sub
Sub Example3
    Wait For (CallSub2(Starter,"QueryAsync","SELECT * FROM testing")) Complete (Result As JdbcResultSet)
    Dim row As String
    Do While Result.NextRow
        row = ""
        For i = 0 To Result.ColumnCount - 1
            row = Result.GetString2(i) & "  "
        Next
        Log(row)
    Loop
    Result.Close
    CallSub(Starter,"CloseConnection")
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…