开发者

SQLite Random not working in browser

I'm using jQuery and HTML to write a web app.

Currently all it does is create a database, add data to the database (checked and working), then displays the data.

The function I'm using has this bit of SQL in it:

tx.executeSql('SELECT * FROM entries ORDER BY RANDOM() LIMIT 1', [], renderResults);

Which absolutely doesn't work.

The second I remove the "ORDER BY RANDOM()" part, it works fine.

It's driving me crazy!!!

Update - here's my code:

This is my code:

<script src="http://www.google.com/jsapi"></script>
<script>
  google.load("jquery", "1.4.1");
</script>
<script>


var db = window.openDatabase("scores", "", "Previous Scores", 1024*1000);


  $(document).ready(function() {

    db.transaction(function(tx) {
      tx.executeSql('CREATE TABLE IF NOT EXISTS Strokes(id INTEGER PRIMARY KEY, sample TEXT, sample2 TEXT)', []);
    });


  insertScores();
  extractScores()

  });



  function insertScores() {

  var example = "One";
  var example2 = "Two"; 

    db.transaction(function开发者_JS百科(tx) {
      tx.executeSql('INSERT INTO Strokes (sample, sample2) VALUES (?, ?)', [example, example2]);
    });



    db.transaction(function(tx) {
      tx.executeSql('INSERT INTO Strokes (sample, sample2) VALUES ("example3", "example4")', []);
      tx.executeSql('INSERT INTO Strokes (sample, sample2) VALUES ("example5", "example6")', []);
    });


  }


  function extractScores() {

    db.transaction(function(tx) {
      tx.executeSql('SELECT * FROM  Strokes ORDER BY RANDOM() LIMIT 1;', [], displayResults);


    });

  }

  function displayResults(tx, rs){


       var $selectedAnswer = "";


          for (var i=0; i < rs.rows.length; i++) {
                    var row = rs.rows.item(i);

            $selectedAnswer =($selectedAnswer + 'sample: ' + row['sample'] + ', sample2: ' + row['sample2']);

                }

         alert($selectedAnswer);

  }



</script>

If I remove "ORDER BY RANDOM()" it works, if I leave it in, nothing happens.


RANDOM() returns a random number. ORDER BY RANDOM() would mean "order by column X", where X is a random column number, starting at column 1. Probably not what you're looking for.

Are you trying to use LIMIT and OFFSET?


I've not solved the problem, but I have created a work around. As random doesn't work, and select count wouldn't give me a digit to play with, this is a long way around, but it will give us a random digit in which to query our database with.

    function finalScores() {

        //This gets a the last id in the table "Strokes", we'll use this to generate a random number
        db.transaction(function(tx) {
           tx.executeSql('SELECT id FROM  Strokes ORDER BY id DESC limit 1;', [], lastNumber);
        });
    }


    function lastNumber(tx, rs){

        var $lastNo = "";

            //This sets the last id as $lastNo to use later
            for (var i=0; i < rs.rows.length; i++) {
            var row = rs.rows.item(i);
            var $lastNo = row['id'];        
        }

    //This should be the last number    
    alert($lastNo);


    //We use this to avoid getting Zero as an answer

    function randomFromTo(from, to){
    return Math.floor(Math.random() * (to - from + 1) + from);
    }
        //This generates a random number between [and including] 1 and your last number
        var $randomNum = randomFromTo(1, $lastNo);

    //This is our random number
    alert($randomNum);

    }


I don't see what is wrong with your query.

SELECT * FROM entries ORDER BY RANDOM() LIMIT 1

From another StackOverflow question:

SELECT * FROM table ORDER BY RANDOM() LIMIT 1;

And here too:

http://www.carlj.ca/2007/12/16/selecting-random-records-with-sql/


try by using the following:

ORDER BY RAND()


my query follows:

select * from circles order by random() limit n

where n is the number of rows needed. It works fine, returns random rows from one of my tables.

@Richard, you should post your db schema. will be easier to suggest features.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜