![]() ![]() Note: We'll also use the Q library so we don't have to deal with icky callbacks //widgetsDb. Let's add a method to get all of the widgets, and another to delete a widget by its id. Typically one would prefer something like CommonJS Modules, instead of adding to global scope, but we'll keep it super simple here. line in the first code snippet? That's how we're going to expose this simple service to the rest of our app. dbLoaded variable will be the promise - that way we can always safely query the db We're making our dbLoaded variable a promise - this will come in handy later to avoid any potential race conditions of querying for items before the seeding is done. Note the use of promise library, Q, here. Here's an example of seeding the database. This is for instances where I have a collection of items to seed - it's much more handy to loop over those items to seed the DB than to write them all out in SQL. ' widgets.db') that contains SQL for seeding the database. ![]() I actually prefer seeding sqlite databases inside the javascript file, as opposed to having a file named the same (i.e. One thing to point out here is that seedDb() function call. Good deal, we've initiated our database file, and have some variables set up to help us later. SeedDb() //function that seeds initial data if needed WidgetsDb = db //private reference available only within this closure Window.WidgetsDb = db //If debug build, handy to have accessible to window Window.sqlitePlugin //cordova sqlite plugin widgets.db.jsĭocument.addEventListener('deviceready', function initWidgetsDb) We'll call it widgets.db.js, and in this file we'll tap into the deviceready event so that we know we can access the sqlite plugin. Now that we have the plugin installed, let's create a javascript file that will act as a service layer of sorts over our database file. A write transaction allows both reading and writing. A read transaction is used for reading only. SQLite supports multiple simultaneous read transactions coming from separate database connections, possibly in separate threads or processes, but only one simultaneous write transaction. Next, navigate to your projects root and install the cordova sqlite plugin: Read transactions versus write transactions. The list (tableName) method that will be used for reading and listing data rows from the SQLite database. There are a few tips and such that I've found handy and thought I'd share here.įirst, make sure you have cordova installed: npm install -g cordova Open the src/app/ file and lets add the following four methods for working with our SQLite database: The create (tableName, item) method that will be used for creating data rows and inserting items in the SQLite database. This allows me to use one (or more) sqlite database files as my data storage. I'm working on building my first hybrid mobile application of substance and am using Cordova and a nifty sqlite plugin - cordova-sqlite-storage. then(() => console.August 24th, 2015 Getting Started with Cordova SQLite So I have to use SQLite and SQLiteObject now I think? I want to add it as a provider in my app. I think key value store get’s complicated if the data model grows. I don’t want to use SQLite as Localstorage. I am searching now for quite some time and I’m totally confused by the changes in using SQLite in Ionic. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |