Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:08:07+00:00 2026-06-08T17:08:07+00:00

I have created a database called todo with table name todo having fields like

  • 0

I have created a database called todo with table name todo having fields like title,date etc. The table is empty right now.

I have defined a function which takes title as parameter and check whether table contains that title or not.

It should return the number of rows.

GetTitle function:

todo.webdb.GetTitle = function(title) {
    var db = todo.webdb.db;
    db.transaction(function(tx){
      tx.executeSql("SELECT title FROM todo WHERE title=?", [title],
          loadTitle,
          todo.webdb.onError);
      });
  } 

loadTitle Function:

function loadTitle(tx, rs) {
    return rs.rows.length;
  }

GetTitle Function is called:

row=todo.webdb.GetTitle("Hello");
alert(row);

I get alert 'undefined' it should return 0

I am confused right now how to resolve this issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:08:08+00:00Added an answer on June 8, 2026 at 5:08 pm

    I think the transactions are asynchronous, so you need to callback function to receive the value.

    todo.webdb.GetTitle = function(title, callback) {
        var db = todo.webdb.db;
        db.transaction(function(tx){
              tx.executeSql("SELECT title FROM todo WHERE title=?", [title],
                  (function loadTitle(tx, rs){
                        callback( rs.rows.length );
                  }),
                  todo.webdb.onError);
          });
    } 
    todo.webdb.GetTitle( "Hello", function(count){
        alert( "count = " + count );
    });
    

    Here’s an excellent tutorial for more information.

    http://blog.darkcrimson.com/2010/05/local-databases/

    Update

    Don’t make functions inside a loop for this main reason.

    Both i and title are referencing a value created out of local scope.
    So the last value of assigned to both i and title will be displayed.

    for (i = 0; i < itemcount; i++) {
        alert('i outside if:' + i);
        var title = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
        
        todo.webdb.GetTitle( title, function (count) {
            if (!count) {
                alert('i inside if:' + i);
                alert( title ); 
            }
        });
    }
    

    Fix:
    Generate a function that has the values locked in a closure.

    var createTitleCallBack = function( i, title ){
        return function (count) {
            if (!count) {
                alert('i inside if:' + i);
                alert( title ); 
            }
        };
    };
    for (i = 0; i < itemcount; i++) {
        alert('i outside if:' + i);
        var title = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
        
        todo.webdb.GetTitle( title, createTitleCallBack( i, title ) );
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql database table called character with columns like name, strength, intelligence,
I have 1 table in my mysql database called clubusers which lists users emails
In my database I have a table called Person with rows for ID and
I have a database called ADB. When I create a new user/role, that user
I have created my database by this sql query: (CREATE DATABASE + DBName +
I have created a database of my movies and another with my actors in
I have created my database, and used cake bake my_project to create my project,
I have created SQL Server database by Visual Studio 2010 Ultimate means in Visual
I have created a MySQL database using MySQL Workbench. It has about 20 tables.
I have created the following database layout, and started coding the application. The more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.