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.
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.
-
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?
-
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.