Tips for effectively passing arguments to PhoneGap database transaction function

I have successfully implemented a PhoneGap database transaction that allows me to run an SQL query and process the results. However, I am looking for a way to make this more reusable by passing arguments to the querying function without relying on global variables. Any suggestions on how to improve the following code would be greatly appreciated:

    //update images function
    function updateGalleryCovers() {
        var db = window.openDatabase("test db", "1.0", "Cordova DB", 200000);
        db.transaction(queryDB_u_g, errorCB);
    }
    //Query the database
    function queryDB_u_g(tx) {
        var query = 'SELECT cover_img, objectId FROM USER_GALLERY WHERE userId="'+getUserId()+'"';
        tx.executeSql(query, [], querySuccess_u_g, errorCB);
    }
//Query success callback
function querySuccess_u_g(tx, results) {
    var len = results.rows.length;
    for (var i=0; i<len; i++){
        // process results
    }
}

I'm trying to convert it to something like this:

    //update images function
    function updateGalleryCovers(userid) {
        var db = window.openDatabase("test db", "1.0", "Cordova DB", 200000);
        db.transaction(function(tx){queryDB_u_g(tx, userid)}, errorCB);
    }
    //Query the database
    function queryDB_u_g(tx, userid) {
        var query = 'SELECT cover_img, objectId FROM USER_GALLERY WHERE userId="'+userid+'"';
        tx.executeSql(query, [], querySuccess_u_g, errorCB);
    }
//Query success callback
function querySuccess_u_g(tx, results) {
    var len = results.rows.length;
    for (var i=0; i<len; i++){
        // process results
    }
}

Any insights or help are welcome!

Answer №1

The transaction functions provided by sqlite are not supported by phonegap directly. It is accurate that passing additional variables to the functions is not possible due to the method signature accepted by sqlite.

However, there is a workaround available:

db_conn.transaction( function(tx){ your_function(tx, parameter1, parameter2) }, ErrorCallBack );

By using this approach, you can pass a dummy function to the transaction success callback and include the transaction object with it.

I hope this solution proves helpful.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The React context is currently yielding an undefined value

I am puzzled by this issue. I have double-checked to ensure that the states value is not undefined, and it isn't. However, I am unable to pinpoint where I may be making a mistake. Here is my authContext.js file: const initialState = { isAuthorized: ...

Adjust the increment of the CSS class property

Just a quick query! I'm attempting to adjust the css width property through JavaScript in this manner: document.getElementById("progressvalue").style.width = "80%"; .progress { width: 100%; max-width: 500px; border: 3px solid black; height: ...

Combining Versioning with BundleConfig.cs: A Comprehensive Guide

Encountering a recurring issue where changes made to CSS or Javascript files do not reflect in client browsers unless they manually refresh the page with ctrl+F5. This poses a challenge, especially in a school system with numerous users who may not be awar ...

What is the best way to include an external Javascript file from a different server?

Below is the Express code snippet I have for the GET method: const express = require('express'); const app = express(); app.get('/', function(req, res) { res.sendFile(path.join(__dirname, '../public/index.html')); }); app ...

The wonders of jQuery popup windows

A code was discovered that displays a popup, but it currently relies on transparency (opacity: 0). I would like to modify it to use display: none instead, as the transparent window in the center of my website is causing issues. Here is the JavaScript code ...

How do you refresh a manifest file in GIT repository when using a mirror?

Our local repository was created using an existing manifest file. repo init -u ssh://ger-repository/my_project-manifests -m my_manifest.xml --mirror However, the manifest file has now been updated and running the init command is not possible: fatal: --m ...

Blank white screen issue in Three JS while loading JSON object

I have recently downloaded threejs and am attempting to create a 3D environment where users can freely move around in an "ego perspective". To start, I used the pointlock-control example as a reference and now I'm trying to add my own elements, beginn ...

How can I incorporate custom text when hovering over dates on fullcalendar?

I'm looking to customize the mouseover text inside fullcalendar. Currently, the script only displays the event title on mouseover. You can see a working example here: JS Fiddle Example. How can I modify the code to show my own personalized text on mo ...

Obtaining a csv file from a local directory in order to create a Chart without relying on a server

Using the D3 library, I am creating a GeoMap chart. Recently, I downloaded a dataset from github called "geonames" and wanted to incorporate this data into my map to test my code. I utilized console.log to ensure everything was functioning properly, and it ...

Get the value of an input by clicking a button in Javascript and then send a POST

I'm working on a form that has the following structure: <form method="post"> <input type="text" name="username" id="username"> <input type="text" name="password" id="password"> <select id="item" name="item"> ...

Tips for creating a client-side library and an npm library

I am interested in creating a unique JavaScript testing library that can be used for both client-side and node.js. Do I need to develop separate repositories for each platform? Let's consider QUnit as an example: For client-side development, I will u ...

A method for altering the color of the text in list view

Is there a way to change the text color of a list view without using a base adapter? I'm currently using android.R.layout.simple_list_item_single_choice to display an option button, but the default text color inside the list view is white and I need i ...

Is there a way to attach a hidden input to the file input once the jquery simpleUpload function is successful?

Attempting to add a hidden form field after the file input used for uploading a file through the simpleUpload call. Here is the HTML (loaded dynamically): <div class="col-md-6"> <div class="form-group"> ...

Instructions on implementing linear gradient color borders on buttons using Tailwind CSS

Currently experimenting with Nextjs and tailwind css. I managed to create a basic web page using these technologies, but now I'm stuck on attempting to add a linear gradient color border to a button in tailwind css. Any ideas on how I can achieve this ...

Prepending a string to the value using Angular's ngOptions

I've been working on creating a custom directive for Angular that includes a label and select element with necessary classes. This is what my directive code looks like: return { restrict: 'E', scope: { text: &a ...

Running a callback in a CucumberJS step definition utilizing Selenium

Currently, I'm exploring CucumberJS with Selenium and PhantomJS. With the help of a helpful StackOverflow answer, I've successfully built a World object to guide me through. Now, as I delve into testing basic step definitions, I find myself conf ...

Is Vercel deploying an incorrect version?

After working on a project for a potential employer and fixing all errors, I deployed it using Vercel. However, upon deployment, the version that was sent to me was displayed instead of the completed one I see when running npm start locally. I would great ...

Leveraging $scope methods from a separate controller within AngularJS

I want to transfer the $scope functions from one controller to another controller, specifically for an AngularUI dialog. In this scenario, I need $scope.scopeVar to be accessible in PopupCtrl. Check out this Plunkr Solve the code as per mlarcher's ...

Is the array State not setting properly in React Native?

I apologize for the strange request, but I need help with a validation method for form inputs within a modal. Currently, I am checking each this.state.variable and pushing them to an aux array, which is then supposed to be set to the original fieldErrors ...

Optimal method for displaying content in Vue.js in read-only mode

I am currently working on a code snippet used to display messages exchanged between two users. <template> <main> <form action="" method="post" @submit.prevent="sendMessage"> <textarea name="message" id="messag ...