Access Databases From MoSync

22 posts / 0 new
Last post
Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:
Access Databases From MoSync

Hey everyone, One topic which keeps coming up is how to access your database, particular Sql Server. I've been answering these questions and pushing people in the right direction, but I've now done a blog post with some server code (ASP.NET) and a MoSync project. The MoSync project gives you a new set of query classes: SQLQuery, NonQuery and SProcQuery which will execute your SQL against the ASP.NET code and a new set of data classes: DataSet, DataTable, DataRow which work just a very little bit like ADO. I've done all of the patching together and creating datasets from the XML returned by the website, so now you can all do:

class MyQuery : public QueryListener
{
  private:
   SQLQuery* q;
  public:
   MyQuery()
   {
     q = new SQLQuery("http://www.mywebserver.com/MoSyncData/", this);
     q->execute("Select * from items where itemId = 4");
   }
   void queryReady(int resultCode, DataSet& results)
   {
     if(resultCode == 200)
     { 
       //Read item name from table 0, row 0
       String itemName = results[0][0]["ItemName"];
     }
     else if(resultCode == 500)
     {
        //Server error, probably bad SQL
     }
     else
     {
        //Network or IIS error
     }
   }
};

Hopefully this is simple enough that everyone can just see how to use it from this code.

Detailed instructions can be found on the blog http://www.mosync.com/blog/2011/02/database-connections-mosync, but ask support question in the forum as I'm not alerted to comments on the blog.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hello Sam!

I want to read data from the MS Project Server database.
Do I need web services on my server to use your code? Or can i access my SQL Database also directly?

Regards,
Joe Kingman

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

This code uses web services only. To get native access you'll need to write a custom driver in C++ and expose the database over the internet, which will give you a massive security headache. These web services are probably the fastest way to get completely flexible access to a database, but I still normally create webservices to perform the database tasks I want to achieve.

JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hi Sam!
Nice Work Sam with the SQL Server Connection!
I got a question: I have to authenticate on the Sharepoint Server. So i need to send an HTTP-Authentications Header? Where exactly do i need to place the Authentication in the Code (which class)?
When i try to execute the code i get the error message:

C:\MoSync\Projekt\MoSyncData\main.cpp: In member function `void TestQuery::runQuery()':
C:\MoSync\Projekt\MoSyncData\main.cpp:38: error: cannot allocate an object of type `SQLQuery'
C:\MoSync\Projekt\MoSyncData\main.cpp:38: error: because the following virtual functions are abstract:
--> GCC PIPIL Compiler v2:11:50:55:Aug 26 2009 (O2)
c:/MoSync/include/MTXml/MTXml.h:331: error: void Mtx::XmlListenerT::mtxParseError(int) [with Tchar = char]
C:\MoSync\Projekt\MoSyncData\main.cpp:41: error: cannot allocate an object of type `SProcQuery'
C:\MoSync\Projekt\MoSyncData\main.cpp:41: error: because the following virtual functions are abstract:
c:/MoSync/include/MTXml/MTXml.h:331: error: void Mtx::XmlListenerT::mtxParseError(int) [with Tchar = char]

How i can fix this?
Hope you can help me! I'm new to that kind of programming.

Regards,
Joe Kingman

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Hi Joe,

MTXml has been updated since I wrote that code to include a new function. The short way to fix it is to add a method to SQLQuery.h. In the public section of SQLQuery add the line

void mtxParseError(int error) {}

Which will allow compilation but ignore parsing errors.

Authentication headers need to be added to the request in XmlConnection.  You need to add a request header.  I did this in another class.

//LOG("Sending authentication");
int bufferSize = strlen(mCurrentRequest->getUserName())
		+ strlen(mCurrentRequest->getPassword()) + 2;
char buffer[bufferSize];
snprintf(buffer, bufferSize, "%s:%s",
		mCurrentRequest->getUserName(),
		mCurrentRequest->getPassword());
String auth = Base64::encode(buffer, strlen(buffer));
//LOG("Unenc: %s", buffer);
char enc[auth.length() + 7];
snprintf(enc, auth.length() + 7, "Basic %s", auth.c_str());
//LOG("Sending authentication");
//LOG("%s", enc);
mConn.setRequestHeader("Authorization", enc);

JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hi Sam,
thanks for the fast respond, i have put the mtxParseError method into the public section of the SQLQuery class in the Query.h file and fixed that error but i got another error message now:

Pipe tool failed. (See console for more information).
Command line: c:\MoSync\bin\pipe-tool.exe

Console output:
Runtime Warning: data section is too small for the stack, heap & BSS
In File '': Error: Data section is too small!,

Is it caused by a out of memory error?
If it is so, how i can change the memory settings of the MoRe emulator?

Regards,
Joe

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Have a look here - http://www.mosync.com/content/error-malloc-failed

JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hi Sam,
Thank you for your help!

But i have still some questions. I want to install the web services on my Sharepoint server. For that purpose i copied the .aspx, .aspx.cs and the web.config files in the directory ProgramFiles\Common Files\Microsoft Shared\web server extensions\14\ISAPI\SQLConnection\.
When i try to access the services through my Browsers it says that i have some duplicate entries in my web.config file. I think that error appears because i have a parent web.config and a child web.config. I don't know which entries i can delete in the config file.
Can you also explain what the connectionString has to contain. And was my procedure to install the services, correctly?

Kind Regards,
Joe

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

I definitely wouldn't install them there. The normal procedure would be go through the IIS manager and create a new web application. This can be any folder, but if you've not got a web root folder then I would create one. These services don't have to be directly related to sharepoint at all, and in fact I would advise you to keep them separate. If you want to be able to access them from the phone network, then they are going to have to be on a webserver that has a public interface (i.e. you can see it on a web browser from anywhere) but also has a private connection to the database.

Have a look here for example Sharepoint connection strings. http://connectionstrings.com/sharepoint

JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hi Sam,
Thank you for your help!

When i try to access on of the Services through my Browser, the following error appears:

"The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference)?"
Do you know how to solve this issue?

Thanks,
Joe

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Are you using Linq? If you are you need to add references to System.Linq and System.Data.Linq, In the webservice C# code you need to add
using namespace System.Linq;
using namespace System.Data.Linq;
Hope this helps.

JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hi Sam!

Yes, it helped. I'm really grateful for you help.

But i got still problems with adding the .aspx and .aspx.cs files to my IIS. I'm using IIS version 7.5.
I'm new to this environment. Can you explain more detailed how to add the files to the IIS, so i can access them
through the browser.

Kind Regards,
Joe

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Hi Joe, try this: http://technet.microsoft.com/en-us/library/cc772350(WS.10).aspx

JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hi Sam,
Thank you for this helpful link. I finally have added the Website to the IIS.
But when i try to execute a Query from Mosync i get the following output in the console:

[1] Creating Query
[1] Executing Query
[1] Executing query for http://192.168.1.117:81/ExecuteQuery.aspx?sql=U0VMRUNUIFtQcm9qZWN0VUlEXSwgW1Byb2plY3ROYW1lXSBGUk9NIFtNU1BfRXBtUHJvamVjdF0%3D
[1] Query Complete
[1] Status Code : 400
[1] 200 is successful, 500 (or other) is error
[1] Found 0 tables
[1] The contents of the field Text is:
[1] User Panic: "BFE Query.cpp:93"
[1] IP:0x12a52: crtlib.s:1
[1] IP:0x5ec: C:/MoSync/HelloWorld/MoSyncData/main.cpp:94
[1] IP:0x24169: c:/slave/buildScripts/temp_kmzl/source/libs/MAUtil/Moblet.cpp:113
[1] IP:0x1f83a: c:/slave/buildScripts/temp_kmzl/source/libs/MAUtil/ListenerSet.h:60
[1] IP:0x1b21e: c:/slave/buildScripts/temp_kmzl/source/libs/MAUtil/Connection.cpp:185
[1] IP:0x14db: C:/MoSync/HelloWorld/MoSyncData/Utilities/XmlConnection.cpp:93
[1] IP:0x2b18: C:/MoSync/HelloWorld/MoSyncData/Query.cpp:212
[1] IP:0x2c2: C:/MoSync/HelloWorld/MoSyncData/main.cpp:79
[1] IP:0x2a33: C:/MoSync/HelloWorld/MoSyncData/Query.cpp:93
[1] IP:0x59f9: c:/slave/buildScripts/temp_kmzl/source/libs/MAStd/maassert.c:53
[1] Exit @ IP 0x59f7 SP 0x1ffac4
[1] IP:0x59f7: c:/slave/buildScripts/temp_kmzl/source/libs/MAStd/maassert.c:53

But when i paste the URL with the encoded Query into my browser, a XML with my data appears.
What can i do to see my data in MoSync?

Kind Regars,
Joe

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

These are the important lines
1] Status Code : 400
[1] 200 is successful, 500 (or other) is error

The server is returning an error. If you download and install Wireshark (www.wireshark.org) we can look at the communication. I'm going to bed now, but we'll discuss this off the forum tomorrow if you want - I'm sure you don't want to publish anything here which may effect your security. You can get me privately on Skype at as 'samuelpickard'

JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hi Sam!

I'm trying to link the GUI with the Connection to the Database. I adjusted the TestQuery class and copied all neceassary files to my GUI project. But when i try to execute the Project i get the following output:

C:\MoSync\Projekt\TestProjekt\Utilities\XmlConnection.cpp:88: Error: Unresolved symbol '__ZN3Mtx7Context4initEPNS_11MtxListenerEPNS_11XmlListenerE',
C:\MoSync\Projekt\TestProjekt\Utilities\XmlConnection.cpp:124: Error: Unresolved symbol '__ZN3Mtx11ContextBase4stopEv',
C:\MoSync\Projekt\TestProjekt\Utilities\XmlConnection.cpp:124: Error: Unresolved symbol '__ZN3Mtx11ContextBase4stopEv',
C:\MoSync\Projekt\TestProjekt\Utilities\XmlConnection.cpp:107: Error: Unresolved symbol '__ZN3Mtx7Context4feedEPc',
C:\MoSync\Projekt\TestProjekt\Utilities\XmlConnection.cpp:128: Error: Unresolved symbol '__ZN3Mtx7Context7processEPc',
C:\MoSync\Projekt\TestProjekt\Query.cpp:54: Error: Unresolved symbol '__ZN3Mtx11XmlListener19mtxUnicodeCharacterEi',
C:\MoSync\Projekt\TestProjekt\Query.cpp:203: Error: Unresolved symbol '__ZN3Mtx11XmlListener19mtxUnicodeCharacterEi',

What can i do to solve this problem?

Kind Regards,
Joe

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

You need to add MTXml.lib to the list of libaries. In the IDE, right-click the project name, and select Properties. Find MoSync Project->Build Settings, and in Additional Libraries, add MTXml.lib to the current list.

JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hi Sam!

I forward the SQL Query to the TestQuery class. When the Query is done the queryReady function is called. But how can i tell the GUI class that the Data is ready to display? I tried to forward an instance of the GUI class to the TestQuery class. So when data is received a function in the GUI class should be called. When i do it like that i get a Heap Memory error.
So what's the best way to communicate between the GUI class and the TestQuery class?

Thanks in advance
Joe

JoeKingman
JoeKingman's picture
Offline
Joined: 20 Sep 2011
Posts:

Hi Sam!
I'm still trying to show the data in the GUI. But i always get the following error message "BFE Query.cpp:93". What does this error mean?

Kind regards,
Joe

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Hi Joe,

Query.cpp line 93 is this assertion

DataTable& DataSet::operator[](int index) { MAASSERT(index < mTables.size()); return mTables
; }

This means that you are using the subscript operator [] to access a table which doesn't exist.  For instance you are doing something like this:

DataTable& users = myDataSet[1];

When there is only 1 table.  Remember that this index is zero-based, so to get the first table you would write

DataTable& users = myDataSet[0];

 

Sam Pickard
rival's picture
Online
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Sorry, the formatting doesn't seem quite right now. I'll have someone look into it :-)

Chris Hughes
qlmhuge's picture
Offline
Mobile Archmage
Joined: 11 Jan 2010
Posts:

The MoSync Database (DB) API, introduced in MoSync SDK 3.0, provides a set of C syscall functions that enable you to create and access SQLite database from your code using the SQL query language.

User guide: http://www.mosync.com/documentation/manualpages/sql-database-api

API Reference: http://www.mosync.com/files/imports/doxygen/latest/html/group___d_b_a_p_i.html