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

  • Home
  • SEARCH
  • 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 6715365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:36:22+00:00 2026-05-26T08:36:22+00:00

Spent hours but couldn’t find what is the problem with the code. The code

  • 0

Spent hours but couldn’t find what is the problem with the code.

The code simply creates a table if not exits and adds the text that typed in a textbox. On every load it loads these and writes to a list.

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        var mydb = null;

        function initDatabase() {
            try {
                if (!window.openDatabase) {
                    alert('Databases are not supported in this browser.');
                } else {
                    var shortName = 'mydb';
                    var version = '1.0';
                    var displayName = 'sample db';
                    var maxSize = 1024*1024*2;
                    mydb = openDatabase(shortName, version, displayName, maxSize, null);

                    mydb.transaction(
                        function (transaction) {
                            transaction.executeSql('CREATE TABLE IF NOT EXISTS sampletable(term TEXT NOT NULL);', [], nullDataHandler, errorHandler);
                        }
                    );

                    loadDB();
                }
            } catch(e) {
                if (e == 2) {
                    alert("Invalid database version.");
                } else {
                    alert("Unknown error "+e+".");
                }
                return;
            }
        }

        function loadDB() {
            $(".debugger").html("");

            mydb.transaction(
                function (t) {
                    t.executeSql("SELECT * FROM sampletable", [],dataSelectHandler,errorHandler);
                }
            );
        }

        function saveDB(_term) {
            mydb.transaction(
                function (transaction) {
                    transaction.executeSql("INSERT INTO sampletable(_term) VALUES (\""+_term + "\");");
                }
            );
        }

        function dataSelectHandler(transaction, results){
            $(".debugger").append("<li>Length: " + results.rows.length + "</li>");

            for (var i=0; i<results.rows.length; i++)
            {
                var row = results.rows.item(i);         
                addToHistoryUI(row['_term']);
            }
        }

        function errorHandler(transaction, error){
            if (error.code==1){
                alert("DB Table already exists");
            } else {
                // Error is a human-readable string.
                alert('Oops.  Error was '+error.message+' (Code '+error.code+')');
            }
            return false;
        }


        function nullDataHandler(){
            //alert("SQL Query Succeeded");
        }

        function addToHistoryUI(_term)
        {
            var newH = "<li>" + _term + "</li>";
            $(".addedQueries").append(newH);
        }

        $(function () {
            initDatabase();
        });

        $(document).ready(function () {
            $(".addBtn").click(function () {      
                var _term = $("#query").val();

                addToHistoryUI(_term);                  
                saveDB(_term);

                loadDB();
            });
        });
</script>
</head>
<body>
    <div data-role="page" id="index">
        <div class="panel">
                <input id="query" type="text" name="query" />
                <a class="navigationLink addBtn" href="#">add</a>
            </div>
            <div class="content">
                <ul class="addedQueries">
                </ul>
            </div>

            <ul class="debugger"></ul>
        </div>
</body>
</html>
  • 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-05-26T08:36:23+00:00Added an answer on May 26, 2026 at 8:36 am

    In your saveDB() method it looks like you are trying to use double quotes in your WHERE clause. Am I right about that?

    Double quotes don’t fly in SQL.

    I am not sure why your SELECT would not be working. Please post the error message.

    UPDATE: You’re INSERT is also failing because you have a typo.

    transaction.executeSql("INSERT INTO sampletable(_term) VALUES (\""+_term + "\");");
    

    You have INTO sampleTable(_term), but you don;t have a column called _term, the column name is term (with no underscore). I made that change and the inserts now happen.

    You also had some other minor stuff. This seems to do what you want.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js" type="text/javascript"></script>
        <script type="text/javascript">
            var mydb = null;
    
            function initDatabase() {
                try {
                    if (!window.openDatabase) {
                        alert('Databases are not supported in this browser.');
                    } else {
                        var shortName = 'mydb';
                        var version = '1.0';
                        var displayName = 'sample db';
                        var maxSize = 1024*1024*2;
                        mydb = openDatabase(shortName, version, displayName, maxSize, null);
    
                        mydb.transaction(
                            function (t) {
                                t.executeSql('CREATE TABLE IF NOT EXISTS sampletable(term TEXT NOT NULL);', [], nullDataHandler, errorHandler);
                            }
                        );
    
                        loadDB();
                    }
                } catch(e) {
                    if (e == 2) {
                        alert("Invalid database version.");
                    } else {
                        alert("Unknown error "+e+".");
                    }
                    return;
                }
            }
    
            function loadDB() {
                $(".debugger").html("");
                $("li").remove();
    
                mydb.readTransaction(
                    function (t) {
                        t.executeSql("SELECT term FROM sampletable", [],dataSelectHandler,errorHandler);
                    }
                );
            }
    
            function saveDB(_term) {
                mydb.transaction(
                    function (transaction) {
                        transaction.executeSql("INSERT INTO sampletable(term) VALUES ('"+_term + "');");
                    }
                );
            }
    
            function dataSelectHandler(t, results){
    
                $(".debugger").append("<li>Length: " + results.rows.length + "</li>");
    
                for (var i=0; i<results.rows.length; i++)
                {   
                    var row = results.rows.item(i);         
                    addToHistoryUI(row['term']);     
    
                }
            }
    
            function errorHandler(transaction, error){
                if (error.code==1){
                    alert("DB Table already exists");
                } else {
                    // Error is a human-readable string.
                    alert('Oops.  Error was '+error.message+' (Code '+error.code+')');
                }
                return false;
            }
    
    
            function nullDataHandler(){
                //alert("SQL Query Succeeded");
            }
    
            function addToHistoryUI(_term)
            {
                var newH = "<li>" + _term + "</li>";
                $(".addedQueries").append(newH);
            }
    
            $(function () {
                initDatabase();
            });
    
            $(document).ready(function () {
                $(".addBtn").click(function () {      
                    var _term = $("#query").val();
    
                    addToHistoryUI(_term);                  
                    saveDB(_term);
    
                    loadDB();
                });
            });
    </script>
    </head>
    <body>
        <div data-role="page" id="index">
            <div class="panel">
                    <input id="query" type="text" name="query" />
                    <a class="navigationLink addBtn" href="#">add</a>
                </div>
                <div class="content">
                    <ul class="addedQueries">
                    </ul>
                </div>
    
                <ul class="debugger"></ul>
            </div>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've spent the past few hours searching, but can't seem to find the answer.
i spent around 4 hours on this but i couldnt find answer, i want
NOTE: THE PROBLEM IS NOT IN THIS CODE, BUT THE ANSWER FOR EXC_BAD_ACCESS is
this may sound funny, but i spent hours trying to recreate a knob with
I just spent 5 hours by checking Google gadgets websites and FAQs, but I
I'm sure this has been answered before, but I've spent the last three hours
I spent hours researching the problem, and just want to share a solution in
I've spent hours trying to get my code to work, its a rats nest
I hope this is a relatively easy problem although I have spent hours websearching
I have a problem with php header redirect. I already spent hours trying to

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.