ADORecordset & ADOConnection Library for MySQL

avacondios

Active Member
Licensed User
Longtime User
Dear all,

Attached, you will find an external library for direct access to Mysql. Copy the files on the external library directory and also download and save on the same directory, the "mysql-connector-java-5.1.22-bin.jar" (from Oracle).

This external library implements the AdoConnection & AdoRecordset class from the old good days of visual basic 6. Change your android targetSdkVersion to 8, because of known bag.

[Class AdoRecordset]

[Properties]
1.Connection - Gets or sets the AdoConnection associated with the AdoRecordset
2.SqlStatement - Gets or sets the SQL Statement
3.isClosed - Retrieves whether this Recordset object has been closed. A Recordset is closed if the method close has been called on it, or if it is automatically closed
4.MaxRecords - Sets or returns the maximum number of records to return to a AdoRecordset object from a query

[Methods]
1.BOF - Returns true if the current record position is before the first record, otherwise false
2.EOF - Returns true if the current record position is after the last record, otherwise false
3.FieldSet - Sets value to active row's field
4.Field -Returns the Value of field
5.InitializeEvent - Activate the events trigger in case of error
6.Open - Open the recordset
7.Close - Closes the recordset
9.MovePrevious - Moves the record pointer to the previous record
10.MoveNext - Moves the record pointer to the next record
11.MoveFirst - Moves the record pointer to the first record
12.MoveLast - Moves the record pointer to the last record
13.MoveTo - Moves the record pointer to specific record
14.AddNew - Add an empty row
15.Update - Update the recordset (New record or update of existing record)
15.Delete - Delete the active record
16.RecordCount - Return the Recordset rows' number
17.RowNumber - Retrieves the current row number

[Events]
1.Sub AdoRecordsetError (Message as String, State as String, ErrorCode as int)


[Class AdoConnection]

[Properties]
1.UserName - Gets the Username
2.Password - Gets the Password
3.HostIP - Gets the HostIP
4.HostPortNumber - Gets the HostPortNumber
5.CharacterEncoding - Gets the CharacterEncoding
6.SocketTimeout - Gets or set a timeout (in seconds) on network socket operations (0, the default means no timeout), before throwing an error
7.LoginTimeout - Gets or set a the maximum time in seconds that a driver will wait while attempting to connect to a database
8.ConnectionTimeout - Gets or set a timeout (in seconds) for socket connect (0, the default means no timeout). before throwing an error
9.Database - Gets the name of the current database or the database to be used after a connection is opened
10.State - Indicates the state of the AdoConnection when the most recent network operation was performed on the connection

[Methods]
1.Open - Opens a database connection with the property settings specified by the ConnectionString
2.Close - Closes the connection to the database
3.BeginTrans - Starts a database transaction (Warning : It works only on MySQL INNODB tables)
4.CommitTrans - Saves any changes and ends the current transaction. (Warning : It works only on MySQL INNODB tables)
5.RollbackTrans - Cancels any changes made during the current transaction and ends the transaction. (Warning : It works only on MySQL INNODB tables)
6.Execute - Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement
7.ExecuteInsertCommand - Executes the given Insert SQL statement
8.ExecuteQuerySingleResult - Executes the given SQL statement, which returns a single ResultSet object

[Events]
Sub AdoConnectionError (Message as String, State as String, ErrorCode as int)



Following a simple code for access of data as the old time :

Dim AdoConn As AdoConnection
Dim AdoRec As AdoRecordset
Dim Result As Int
Dim ResultS As String

'Replace the values, with your Mysql Server properties
AdoConn.ConnectionString("192.168.43.111","3306","UTF-","demo","demo","demo")
AdoConn.ConnectionTimeout=1
AdoConn.LoginTimeout=1
AdoConn.SocketTimeout=1

'if the mysql driver return error, the subs AdoConnectionError will activate
AdoConn.InitializeEvent
AdoConn.Open

AdoRec.InitializeEvent
AdoRec.Connection=AdoConn
AdoRec.SqlStatement="select * from demo"
AdoRec.Open

If AdoRec.isClosed Then
' Do something
Else
' Do something
End If

If AdoRec.RecordCount>0 Then

Do While Not(AdoRec.EOF)
Log(AdoRec.Field("DemoField"))
AdoRec.MoveNext
Loop
End If

AdoRec.Close


--------------------------------------

I am waiting your feedback
 

Attachments

  • AdoDB.zip
    10.2 KB · Views: 814

moster67

Expert
Licensed User
Longtime User
Nice - good old days but surely still useful. In this moment I don't need to use a database with my apps but I will keep it in mind if I need it later on.

Many thanks for your addition to the growing resources of B4A.
 

Jaames

Active Member
Licensed User
Longtime User
It is nice lib, access mySql database without php scripts would be very useful for many B4A users i'm pretty sure.

For me it does not working

- target SDK is 8
- I downloaded mysql-connector-java-5.1.22-bin.jar and copied it to the additional lib folder
- Added reference to your "AdoDB" lib

And if I use WiFi connection , when i compile and run app, it loops in this line:
B4X:
AdoConn.Open
and eventually crashes the app without any error message

If I use data mobile internet it gives me this error :

B4X:
AdoConnectionError : 


Error message : Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
State : 08S01
Error code : 0
AdoRecordsetError : 


Error message : AdoConnection has closed or it did not opened
State : Open
Error code : 9999
AdoRecordsetError : 
Error message : Recordset has closed or it did not opened
State : isClosed
Error code : 9999
Connected
AdoRecordsetError : 
Error message : Recordset has closed or it did not opened
State : RecordCount
Error code : 9999
** Activity (main) Pause, UserClosed = false **

I added subs as you mentioned :

B4X:
Sub AdoConnectionError (Message As String, State As String, ErrorCode As Int)
Log("AdoConnectionError : " & CRLF & "Error message : " & Message & CRLF & "State : " & State & CRLF & "Error code : " & ErrorCode)
End Sub

Sub AdoRecordsetError (Message As String, State As String, ErrorCode As Int)
Log("AdoRecordsetError : " & CRLF & "Error message : " & Message & CRLF & "State : " & State & CRLF & "Error code : " & ErrorCode)
End Sub

I tried with 2 different MySQL providers and 2 different databases..

Can you give me a hint what could be wrong ?

Thanks and :sign0098:
 
Last edited:

avacondios

Active Member
Licensed User
Longtime User
Hi,

The errors that you got, it refers that the library cannot connect with your database server. The library has error handling, so it will not create exceptions.

Please check, on the Mysql Server properties, that you have provided the rights, external clients to communicate with the Database on the port 3306. Also, check with telnet command, that you have access on the Mysql server, on the port 3306 (firewall issues).

Also, can you provide the connectionstring ?

Antonis Vakondios
 

Jaames

Active Member
Licensed User
Longtime User
I guess it is my mySQL provider , does not allow me to connect remotely...

there is my connectionstring:

AdoConn.ConnectionString("195.222.33.174","3306"," UTF-8","wpBaza","user","pw")

Username and password are not real in the example above.

thanks
 
Last edited:

qsrtech

Active Member
Licensed User
Longtime User
Perhaps a shot in the dark, but will this work with SQL Server?
 

asmag

Member
Licensed User
Longtime User
Hi!

Where is mysql-connector-java-5.1.22-bin.jar ? The post is not visible for download.

Regards

asmag
 

avacondios

Active Member
Licensed User
Longtime User
Hi asmag,

you can download direct from "http://dev.mysql.com/downloads/connector/j/" and copy/paste the jar file on your external lib directory.

Antonis
 

laguilar

Member
Licensed User
Longtime User
Field Names?

Could a List object property be added to the recordset object to allow getting the field names within the result set?
 

micp

Member
Licensed User
Longtime User
Creating database in MySQL server.

How do i create a MySQL database and import it to my server?

Note: I am a real beginner.
 

djpero

Member
Licensed User
Longtime User
It is nice lib, access mySql database without php scripts would be very useful for many B4A users i'm pretty sure.

For me it does not working

- target SDK is 8
- I downloaded mysql-connector-java-5.1.22-bin.jar and copied it to the additional lib folder
- Added reference to your "AdoDB" lib

And if I use WiFi connection , when i compile and run app, it loops in this line:
B4X:
AdoConn.Open
and eventually crashes the app without any error message

If I use data mobile internet it gives me this error :

B4X:
AdoConnectionError : 


Error message : Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
State : 08S01
Error code : 0
AdoRecordsetError : 


Error message : AdoConnection has closed or it did not opened
State : Open
Error code : 9999
AdoRecordsetError : 
Error message : Recordset has closed or it did not opened
State : isClosed
Error code : 9999
Connected
AdoRecordsetError : 
Error message : Recordset has closed or it did not opened
State : RecordCount
Error code : 9999
** Activity (main) Pause, UserClosed = false **

I added subs as you mentioned :

B4X:
Sub AdoConnectionError (Message As String, State As String, ErrorCode As Int)
Log("AdoConnectionError : " & CRLF & "Error message : " & Message & CRLF & "State : " & State & CRLF & "Error code : " & ErrorCode)
End Sub

Sub AdoRecordsetError (Message As String, State As String, ErrorCode As Int)
Log("AdoRecordsetError : " & CRLF & "Error message : " & Message & CRLF & "State : " & State & CRLF & "Error code : " & ErrorCode)
End Sub

I tried with 2 different MySQL providers and 2 different databases..

Can you give me a hint what could be wrong ?

Thanks and :sign0098:

Same error in my case. Any suggestion?
 

rbghongade

Active Member
Licensed User
Longtime User
great work!

This is indeed evolution of B4A coming closer to VB. Such things simplify the learning curve a great deal. Keep up the good work.
 

ivanomonti

Expert
Licensed User
Longtime User
Hi, error 999

my project test

mysql 5.x.x ... I connect to my database either directly or with web service

B4X:
Sub Globals
   Dim AdoConn As AdoConnection
   Dim AdoRec As AdoRecordset
   Dim Result As Int
   Dim ResultS As String

   Dim Button1 As Button
   Dim EditText1 As EditText ' [COLOR="Red"]query "select * from table_000_user"[/COLOR]
End Sub


Code into button

B4X:
Sub Button1_Click

   AdoConn.ConnectionString("IP","3306"," UTF-8","schema","user","password")
   AdoConn.ConnectionTimeout=1
   AdoConn.LoginTimeout=1
   AdoConn.SocketTimeout=1
   
   AdoConn.InitializeEvent
   AdoConn.Open

   AdoRec.InitializeEvent
   AdoRec.Connection=AdoConn
        
   AdoRec.SqlStatement=EditText1.Text
   AdoRec.Open

   If AdoRec.isClosed Then
      Msgbox("isClose","")
   Else
      Msgbox("mmmmm","")
   End If

   If AdoRec.RecordCount>0 Then
   Do While Not(AdoRec.EOF)
   Log(AdoRec.Field("a01"))
   AdoRec.MoveNext
   Loop
   End If

   Msgbox("Record " & AdoRec.MaxRecords,"")

   AdoRec.Close
End Sub


Control error

B4X:
Sub AdoConnectionError (Message As String, State As String, ErrorCode As Int)
   Log("AdoConnectionError : " & CRLF & "Error message : " & Message & CRLF & "State : " & State & CRLF & "Error code : " & ErrorCode)
End Sub

Sub AdoRecordsetError (Message As String, State As String, ErrorCode As Int)
   Log("AdoRecordsetError : " & CRLF & "Error message : " & Message & CRLF & "State : " & State & CRLF & "Error code : " & ErrorCode)
End Sub

ERROR

Installing file.
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
AdoConnectionError :
Error message : Could not create connection to database server.
State : 08001
Error code : 0
AdoRecordsetError :
Error message : AdoConnection has closed or it did not opened
State : Open
Error code : 9999
AdoRecordsetError :
Error message : Recordset has closed or it did not opened
State : isClosed
Error code : 9999
AdoRecordsetError :
Error message : Recordset has closed or it did not opened


State : RecordCount
Error code : 9999
 

micp

Member
Licensed User
Longtime User
Ado connection and recordset.

Hi, first of all, thank,s a lot for sharing. i just install the librairy as you explained. i have an error message when opening ba4 saying ; "Missing jar fil for librairy ; c:\android\build.xml i saw some path's in that file that probably can't be found in my jdk as i am using a newer version. There is probably some other files in that kit that will look for others un reachable path. Do you suggest that i should go back with jdk 1.6?
 

avacondios

Active Member
Licensed User
Longtime User
Hi ivanomonti,

I will run the same code on my testbed and I will come back.

For micp's question, you need to have the mysql-connector-java-5.1.22-bin.jar, under on the same external lib directory that you have stored the rest of external libraries.
 

HCAZ

Member
Licensed User
Longtime User
Example

Can anyone post a working example to download please, I cant seem to gt this working, and it doesn't give any errors, so an example would be great. :sign0163:
 

Sytek

Active Member
Licensed User
Longtime User
It throws me this error:
main_button1_click (java line: 278)
java.lang.NoSuchMethodError: java.sql.ResultSet.isClosed
at telesoft.b4a.mysql.AdoRecordset.isClosed(AdoRecordset.java:76)
at b4a.example.main._button1_click(main.java:278)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:153)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:149)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:55)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
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:4635)
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:916)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)

Code in Button:
B4X:
Sub Button1_Click
   Dim AdoConn As AdoConnection
Dim AdoRec As AdoRecordset
Dim Result As Int
Dim ResultS As String
'Replace the values, with your Mysql Server properties
AdoConn.ConnectionString("myip","3306","UTF-8","mydb","myuser","mypass")
AdoConn.ConnectionTimeout=1
AdoConn.LoginTimeout=1
AdoConn.SocketTimeout=1
'if the mysql driver return error, the subs AdoConnectionError will activate
AdoConn.InitializeEvent
AdoConn.Open
AdoRec.InitializeEvent
AdoRec.Connection=AdoConn
AdoRec.SqlStatement="select * from mytable"
AdoRec.Open
If AdoRec.isClosed Then
' Do something
Else
' Do something
End If
If AdoRec.RecordCount>0 Then
Do While Not(AdoRec.EOF)
Log(AdoRec.Field("fieldname"))
AdoRec.MoveNext
Loop
End If
AdoRec.Close
End Sub
Thank's :sign0098:

Updated by Me!

This one was solved in http://www.b4x.com/forum/176578-post40.html
 
Last edited:

BetoRey

New Member
Licensed User
Longtime User
Android version...

Hi,

Great job. Thanks indeed.

Do you have an idea on how to make it work with latter versions of the Android SDK?.

I need my project to work on 14, and would be glad to help you out in case that is needed.

Thanks in advance!

BetoRey
 
Last edited:
Top