Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Opening SQLite in webview

I first want to say that I like Titanium and believe its a great platform for developers. IMHO, my own greatest interest is based on the fact that I can utilize the web page and javascript format and export to multiple platforms, so I am doing a lot of pages as webviews. For the most part, I have been learning a lot by reading through these questions and answers but I have finally run into a problem that seems to be under documented so I guess its time to ask…

My app opens with standard titanium tabs and tool bars but also immediately uses webviews that are database driven. I made earlier .js views that access the SQLite database fine, but then I put the same code into .html pages and try to open them with:

var win0 = Ti.UI.createWebView({url:profile.html})

They open fine and most of the Ti. vars work OK, but it doesn't want to open the database for some reason. I use to open the database, and it worked on .js page as:

var db = Titanium.Database.open('esNetworkDB');

var dbrows = db.execute(query);

I know the query is fine and my function works flawlessly when called from within .js page, so I am wondering what else could it be? In particular, I don't want to be stupid but I found myself wondering if Titanium.Database.open() is not defined for webviews, or is there some other little thing I am missing? And if it's not defined, can it be added as an include or framework or what other options?

Thanks in advance for help.

— asked March 7th 2011 by David Gaddis
  • database
  • javascript
  • mobile
  • open
  • titanium
  • webview
0 Comments

3 Answers

  • From my limited understanding of webviews (I never use them), I'm going to say you can't do this. The webviews don't run within your app's context they run within the browser's space. So, the database isn't accessible. You could pull the needed data and pass it as variables to your window. Or, you could create a native UI component instead. Or put the database on the web and use HTML/JS within the webview to load the data over the 'net.

    — answered March 7th 2011 by Tim Poulsen
    permalink
    0 Comments
  • Hi Tim,

    Thanks for quick reply. I am using local html files so loading over the net isn't a solution. I wasn't sure how titanium was handling webviews and realized that the html was running in browser space but hoped titanium was managing the javascript in context.

    As for the native UI, like others whom I have read here, I have a couple of issues that lead me to believe that I will have to use webviews to get the effects I want. Admittedly, I am quite new to Titanium and haven't learned all of it's features, functions, and classes yet. I can play around with the native UI some more and eventually figure it out, but if you don't mind, maybe I can take a short cut by picking your brain for a quick answer?

    My app utilizes methods such as expanding/contracting the height of table rows for displaying/hiding additional information rather than sliding in modal views or using alerts and captions, which is simple to do with css styling and appending className(s) but I don't see how to do that with native UI. I haven't spent much time on it yet, but from the discussions I have read, it appears to me that titanium only uses class names to specify design criteria when initially creating views, and I am under the impression that dynamically changing a class name for a table row as an event response would not affect the properties of the view such as height and content, and that the only way to achieve the desired effect might be to constantly reload the view each time an event was triggered and manage the variety of views that are used each time. Am I missing something here?

    — answered March 7th 2011 by David Gaddis
    permalink
    1 Comment
    • To do this with Titanium UI views you would want to use a Animation (animate heights from 0 to desired hight to show or vice versa). You can still control the animation similar to DOM events using to UI views events or your own custom Ti App Events. More information about Ti UI Animation: https://developer.appcelerator.com/apidoc/mobilea/1.0/Titanium.UI.Animation

      p.s. Not sure if dynamically changing the class name of a view on the fly after the view is created would have any affect. This may depend on when Ti adds the class properties to the view. You could always create a simple app to test this. However, I would guess that the Titanium UI animation should be used instead of this method.

      Also I would guess that you may want to look at the layout property of the views to help ease your layout design.

      — commented March 7th 2011 by Dan Boorn
  • I agree with Tim. You would want to use Titanium App Events to communicate between your web view and Titanium App code. Use the event data parameter to pass information between such as data requests and result data. Usually I create a centralised database JS file that has a main database JS class that I use for all sqllite3 queries. This same class also returns data as JSON, array or XML since passing a database result item via events to web views doesn't work.

    — answered March 7th 2011 by Dan Boorn
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.