Mssql

Hans Scholten

Member
Licensed User
Longtime User
I do not know if it is possible but I want to connect to a remote mssql express server and show the fields in a grid (or html table)

I hope someone can help me with a sample

Regards,
Hans
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is a tutorial which demonstrates how you can use a simple PHP script together with the Http library to connect to a MySQL server. Connecting to a different server should be very similar: http://www.b4x.com/forum/basic4andr...-connect-android-mysql-database-tutorial.html

DBUtils generates Html tables from a local SQLite database. You can copy the code that builds the table and change it to work with other data source. http://www.b4x.com/forum/basic4andr...475-dbutils-android-databases-now-simple.html
 
Upvote 0

Hans Scholten

Member
Licensed User
Longtime User
I can manage my things in vbnet but this is complete new to me ;)

I have installed php on my homeserver, made exact the same table in mssql (the countries table)

id (varchar(2))
name (varchar(50))
population (smalint)

Then made the countries.php but I think that is not correct, put it on my server, if I run the file then I get

Error occured
An error has occured in sub: java.lang.Exception:Sub hc_responseerror signature does not match expected signature. Continue? if I press yes then nothing happens, so I think the connection is good username and password also or am I wrong.

What do I need to change in the countries.php
<?
$databasename = "wonen";

// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = 'number to server';

// Connect to MSSQL
$link = mssql_connect($server, 'usrnm', 'psswrd');

if (!$link) {
die('Something went wrong while connecting to MSSQL');
}



mysql_select_db($databasename) or die(mysql_error());
$query = file_get_contents("php://input");
$sth = mysql_query($query);

if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
 
Upvote 0

Hans Scholten

Member
Licensed User
Longtime User
Erel I updated the Library (two files) now I get An error in sub: main_hc_responseerror (B4Aline:47) log (Response Getstring ("UTF8")) java.lang.NullpointerException

I think I need to change the php file but do not know what I need to change I want to get the data from a mssql server so I think the select is not good in the php file

BTW is there a way to set breakpoints or I can step to the code so I can watch where it goes wrong

Regards,
Hans
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can comment out these lines to prevent this error:
If Response <> Null Then
Log(Response.GetString("UTF8"))
Response.Release
End If

Can you post the stack trace that appears in the logs when this error happens?
The logs should also display a line with the status code which is important.

Breakpoints will be added in the next version.
 
Upvote 0

Hans Scholten

Member
Licensed User
Longtime User
Erel found it (sorry I look over it)

request time failed: java.net.SocketException: Address family not supported by protocol





>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<


CheckJNI is ON


--- registering native functions ---


GC_EXPLICIT freed 436 objects / 25560 bytes in 185ms


Scanning package: /data/app/vmdl44929.tmp


Removing non-system package:anywheresoftware.b4a.samples.mysql


Force stopping package anywheresoftware.b4a.samples.mysql uid=10033


Sending signal. PID: 287 SIG: 9


Unexpected resume of com.android.launcher while already resumed in anywheresoftware.b4a.samples.mysql


WIN DEATH: Window{43fab840 anywheresoftware.b4a.samples.mysql/anywheresoftware.b4a.samples.mysql.main paused=false}


WIN DEATH: Window{43fe0760 anywheresoftware.b4a.samples.mysql/anywheresoftware.b4a.samples.mysql.main paused=false}


WIN DEATH: Window{43fe8058 anywheresoftware.b4a.samples.mysql/anywheresoftware.b4a.samples.mysql.main paused=false}


Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43f5e090


Scanning package anywheresoftware.b4a.samples.mysql


Package anywheresoftware.b4a.samples.mysql codePath changed from /data/app/anywheresoftware.b4a.samples.mysql-1.apk to /data/app/anywheresoftware.b4a.samples.mysql-2.apk; Retaining data and using new


/data/app/anywheresoftware.b4a.samples.mysql-2.apk changed; unpacking


DexInv: --- BEGIN '/data/app/anywheresoftware.b4a.samples.mysql-2.apk' ---


DexOpt: load 96ms, verify 296ms, opt 9ms


DexInv: --- END '/data/app/anywheresoftware.b4a.samples.mysql-2.apk' (success) ---


Code path for pkg : anywheresoftware.b4a.samples.mysql changing from /data/app/anywheresoftware.b4a.samples.mysql-1.apk to /data/app/anywheresoftware.b4a.samples.mysql-2.apk


Resource path for pkg : anywheresoftware.b4a.samples.mysql changing from /data/app/anywheresoftware.b4a.samples.mysql-1.apk to /data/app/anywheresoftware.b4a.samples.mysql-2.apk


Activities: anywheresoftware.b4a.samples.mysql.main


Force stopping package anywheresoftware.b4a.samples.mysql uid=10033


move /data/dalvik-cache/data@[email protected]@classes.dex -> /data/dalvik-cache/data@[email protected]@classes.dex


New package installed in /data/app/anywheresoftware.b4a.samples.mysql-2.apk


GC_FOR_MALLOC freed 9376 objects / 565568 bytes in 92ms


Force stopping package anywheresoftware.b4a.samples.mysql uid=10033


GC_EXPLICIT freed 741 objects / 42240 bytes in 67ms


no available voice recognition services found


GC_EXPLICIT freed 4293 objects / 267832 bytes in 98ms


unlink /data/dalvik-cache/data@[email protected]@classes.dex


Shutting down VM


adbd disconnected


NOTE: attach of thread 'Binder Thread #3' failed





>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<


CheckJNI is ON


--- registering native functions ---


Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=anywheresoftware.b4a.samples.mysql/.main }


Start proc anywheresoftware.b4a.samples.mysql for activity anywheresoftware.b4a.samples.mysql/.main: pid=318 uid=10033 gids={3003}


Shutting down VM


adbd disconnected


NOTE: attach of thread 'Binder Thread #3' failed


** Activity (main) Create, isFirst = true **


GC_FOR_MALLOC freed 7526 objects / 369512 bytes in 76ms


** Activity (main) Resume **


Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@43e8dcb8 (uid=10033 pid=318)


Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43f29970


invoke: expected 4 args, received 3 args





java.lang.Exception: Sub hc_responseerror signature does not match expected signature.


at anywheresoftware.b4a.BA.raiseEvent2(BA.java:101)


at anywheresoftware.b4a.BA$1.run(BA.java:202)


at android.os.Handler.handleCallback(Handler.java:587)


at android.os.Handler.dispatchMessage(Handler.java:92)


at android.os.Looper.loop(Looper.java:123)


at android.app.ActivityThread.main(ActivityThread.java:4627)


at java.lang.reflect.Method.invokeNative(Native Method)


at java.lang.reflect.Method.invoke(Method.java:521)


at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)


at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)


at dalvik.system.NativeStart.main(Native Method)


java.lang.Exception: Sub hc_responseerror signature does not match expected signature.


Displayed activity anywheresoftware.b4a.samples.mysql/.main: 2223 ms (total 2223 ms)


GC_EXPLICIT freed 690 objects / 39416 bytes in 113ms


GC_EXPLICIT freed 214 objects / 14616 bytes in 208ms


GC_EXPLICIT freed 727 objects / 53624 bytes in 100ms
 
Upvote 0

Hans Scholten

Member
Licensed User
Longtime User
Erel you are right, at home I update the files today I installed it at computer at work. Now I do not get an error but also no data while there is data in the table

GC_EXPLICIT freed 2601 objects / 110616 bytes in 80ms





>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<


CheckJNI is ON


--- registering native functions ---


GC_EXPLICIT freed 107 objects / 5536 bytes in 140ms


Scanning package: /data/app/vmdl22559.tmp


Removing non-system package:anywheresoftware.b4a.samples.mysql


Force stopping package anywheresoftware.b4a.samples.mysql uid=10033


Sending signal. PID: 346 SIG: 9


Unexpected resume of anywheresoftware.b4a.designer while already resumed in anywheresoftware.b4a.samples.mysql


WIN DEATH: Window{43fd42d8 anywheresoftware.b4a.samples.mysql/anywheresoftware.b4a.samples.mysql.main paused=false}


Displayed activity anywheresoftware.b4a.samples.mysql/.main: 28007 ms (total 28007 ms)


Scanning package anywheresoftware.b4a.samples.mysql


Package anywheresoftware.b4a.samples.mysql codePath changed from /data/app/anywheresoftware.b4a.samples.mysql-2.apk to /data/app/anywheresoftware.b4a.samples.mysql-1.apk; Retaining data and using new


/data/app/anywheresoftware.b4a.samples.mysql-1.apk changed; unpacking


DexInv: --- BEGIN '/data/app/anywheresoftware.b4a.samples.mysql-1.apk' ---


DexOpt: load 136ms, verify 283ms, opt 9ms


DexInv: --- END '/data/app/anywheresoftware.b4a.samples.mysql-1.apk' (success) ---


Code path for pkg : anywheresoftware.b4a.samples.mysql changing from /data/app/anywheresoftware.b4a.samples.mysql-2.apk to /data/app/anywheresoftware.b4a.samples.mysql-1.apk


Resource path for pkg : anywheresoftware.b4a.samples.mysql changing from /data/app/anywheresoftware.b4a.samples.mysql-2.apk to /data/app/anywheresoftware.b4a.samples.mysql-1.apk


Activities: anywheresoftware.b4a.samples.mysql.main


Force stopping package anywheresoftware.b4a.samples.mysql uid=10033


move /data/dalvik-cache/data@[email protected]@classes.dex -> /data/dalvik-cache/data@[email protected]@classes.dex


New package installed in /data/app/anywheresoftware.b4a.samples.mysql-1.apk


Force stopping package anywheresoftware.b4a.samples.mysql uid=10033


GC_EXPLICIT freed 5907 objects / 415672 bytes in 304ms


GC_EXPLICIT freed 608 objects / 34536 bytes in 91ms


GC_EXPLICIT freed 2381 objects / 118896 bytes in 91ms


no available voice recognition services found


unlink /data/dalvik-cache/data@[email protected]@classes.dex


Shutting down VM


adbd disconnected


JNI: AttachCurrentThread (from ???.???)


NOTE: attach of thread 'Binder Thread #3' failed





>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<


CheckJNI is ON


--- registering native functions ---


Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=anywheresoftware.b4a.samples.mysql/.main }


Start proc anywheresoftware.b4a.samples.mysql for activity anywheresoftware.b4a.samples.mysql/.main: pid=376 uid=10033 gids={3003}


Shutting down VM


adbd disconnected


NOTE: attach of thread 'Binder Thread #3' failed


** Activity (main) Create, isFirst = true **


GC_FOR_MALLOC freed 7576 objects / 371400 bytes in 69ms


** Activity (main) Resume **


Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@43f6a710 (uid=10033 pid=376)


Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43ef91f0


java.lang.IllegalStateException: Target host must not be null, or set in parameters.


at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:561)


at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)


at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)


at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)


at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)


at anywheresoftware.b4a.http.HttpClientWrapper$3.run(HttpClientWrapper.java:179)


at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)


at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)


at java.util.concurrent.FutureTask.run(FutureTask.java:137)


at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)


at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)


at java.lang.Thread.run(Thread.java:1096)


Error: java.lang.IllegalStateException: Target host must not be null, or set in parameters., StatusCode: -1
 
Upvote 0

Hans Scholten

Member
Licensed User
Longtime User
Erel I have been busy all day to get a good connection to my server made some changes, as far as I can see it must work, I now test it and get this error




>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<


CheckJNI is ON


--- registering native functions ---


GC_EXPLICIT freed 282 objects / 18208 bytes in 126ms


Scanning package: /data/app/vmdl32207.tmp


Removing non-system package:anywheresoftware.b4a.samples.mysql


Force stopping package anywheresoftware.b4a.samples.mysql uid=10035


Scanning package anywheresoftware.b4a.samples.mysql


Package anywheresoftware.b4a.samples.mysql codePath changed from /data/app/anywheresoftware.b4a.samples.mysql-2.apk to /data/app/anywheresoftware.b4a.samples.mysql-1.apk; Retaining data and using new


/data/app/anywheresoftware.b4a.samples.mysql-1.apk changed; unpacking


DexInv: --- BEGIN '/data/app/anywheresoftware.b4a.samples.mysql-1.apk' ---


DexOpt: load 99ms, verify 391ms, opt 11ms


DexInv: --- END '/data/app/anywheresoftware.b4a.samples.mysql-1.apk' (success) ---


Code path for pkg : anywheresoftware.b4a.samples.mysql changing from /data/app/anywheresoftware.b4a.samples.mysql-2.apk to /data/app/anywheresoftware.b4a.samples.mysql-1.apk


Resource path for pkg : anywheresoftware.b4a.samples.mysql changing from /data/app/anywheresoftware.b4a.samples.mysql-2.apk to /data/app/anywheresoftware.b4a.samples.mysql-1.apk


Activities: anywheresoftware.b4a.samples.mysql.main


Force stopping package anywheresoftware.b4a.samples.mysql uid=10035


move /data/dalvik-cache/data@[email protected]@classes.dex -> /data/dalvik-cache/data@[email protected]@classes.dex


New package installed in /data/app/anywheresoftware.b4a.samples.mysql-1.apk


Force stopping package anywheresoftware.b4a.samples.mysql uid=10035


GC_EXPLICIT freed 7500 objects / 519184 bytes in 167ms


no available voice recognition services found


GC_EXPLICIT freed 596 objects / 34112 bytes in 284ms


GC_EXPLICIT freed 3248 objects / 185576 bytes in 99ms


unlink /data/dalvik-cache/data@[email protected]@classes.dex


Shutting down VM


adbd disconnected


NOTE: attach of thread 'Binder Thread #3' failed





>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<


CheckJNI is ON


--- registering native functions ---


Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=anywheresoftware.b4a.samples.mysql/.main }


Start proc anywheresoftware.b4a.samples.mysql for activity anywheresoftware.b4a.samples.mysql/.main: pid=346 uid=10035 gids={3003}


Shutting down VM


adbd disconnected


NOTE: attach of thread 'Binder Thread #3' failed


** Activity (main) Create, isFirst = true **


GC_FOR_MALLOC freed 7551 objects / 370456 bytes in 80ms


** Activity (main) Resume **


Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@43fa6a08 (uid=10035 pid=346)


Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43fb83e0


Displayed activity anywheresoftware.b4a.samples.mysql/.main: 1942 ms (total 1942 ms)


GC_FOR_MALLOC freed 5570 objects / 307112 bytes in 233ms


Response from server: <?



$databasename = "wonen";







// Server in the this format: <computer>\<instance name> or



// <server>,<port> when using a non default port number



$server = 'iptoserver';







// Connect to MSSQL



$link = mssql_connect($server, 'name', 'psswrd');







if (!$link) {



die('Something went wrong while connecting to MSSQL');



}



mssql_select_db($databasename) or die(mysql_error());



$query = file_get_contents("php://input");



$sth = mysql_query($query);







if (mysql_errno()) {



header("HTTP/1.1 500 Internal Server Error");



echo $query.'\n';



echo mysql_error();



}



else



{



$rows = array();



while($r = mysql_fetch_assoc($sth)) {



$rows[] = $r;



}



print json_encode($rows);



}



?>


main_hc_responsesuccess (B4A line: 63)


countries = parser.NextArray 'returns a list with maps





java.lang.RuntimeException: JSON Array expected.


at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:59)


at anywheresoftware.b4a.samples.mysql.main._hc_responsesuccess(main.java:306)


at java.lang.reflect.Method.invokeNative(Native Method)


at java.lang.reflect.Method.invoke(Method.java:521)


at anywheresoftware.b4a.BA.raiseEvent2(BA.java:99)


at anywheresoftware.b4a.BA$1.run(BA.java:202)


at android.os.Handler.handleCallback(Handler.java:587)


at android.os.Handler.dispatchMessage(Handler.java:92)


at android.os.Looper.loop(Looper.java:123)


at android.app.ActivityThread.main(ActivityThread.java:4627)


at java.lang.reflect.Method.invokeNative(Native Method)


at java.lang.reflect.Method.invoke(Method.java:521)


at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)


at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)


at dalvik.system.NativeStart.main(Native Method)


java.lang.RuntimeException: JSON Array expected.
 
Upvote 0

Hans Scholten

Member
Licensed User
Longtime User
Sorry connectioncode

Sub ExecuteRemoteQuery(Query As String, TaskId As Int)
Dim req As HttpRequest
req.InitializePost2("https://hansscholten.homeserver.com/agenda/countries.php", query.GetBytes("UTF8"))
hc.Execute(req, TaskId)
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("Error: " & Reason & ", StatusCode: " & StatusCode)
If Response <> Null Then
Log(Response.GetString("UTF8"))
Response.Release
End If
ProgressDialogHide
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim res As String
res = Response.GetString("UTF8")
res = res.SubString(1) 'removes first character 'This didn't fix the error!!!
Log("Response from server: " & res)
Dim parser As JSONParser
parser.Initialize(res)
Select TaskId
Case COUNTRIES_LIST
'add the countries to the ListView
Dim countries As List
countries = parser.NextArray 'returns a list with maps
For i = 0 To countries.Size - 1
Dim m As Map
m = countries.Get(i)
'We are using a custom type named TwoLines (declared in Sub Globals).
'It allows us to later get the two values when the user presses on an item.
Dim tl As TwoLines
tl.First = m.Get("id")
tl.Second = m.Get("name")
ListView1.AddTwoLines2(tl.First, tl.Second, tl)
Next
ProgressDialogHide
Case COUNTRY_POPULATION
Dim l As List
l = parser.NextArray
If l.Size = 0 Then
lblPopulation.Text = "N/A"
Else
Dim m As Map
m = l.Get(0)
lblPopulation.Text = NumberFormat2(m.Get("population"),0, 0, 0, True) & " (K)"
End If
End Select
response.Release
End Sub
 
Upvote 0
Top