SQLite Random 在浏览器中不起作用 [英] SQLite Random not working in browser

查看:48
本文介绍了SQLite Random 在浏览器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery 和 HTML 编写一个网络应用程序.

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.

我正在使用的函数中有这样的 SQL:

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

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

这绝对行不通.

第二个我删除了ORDER BY RANDOM()"部分,它工作正常.

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

快把我逼疯了!!!

更新 - 这是我的代码:

这是我的代码:

<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(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>

如果我删除ORDER BY RANDOM()",它会起作用,如果我保留它,则什么也不会发生.

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

推荐答案

我还没有解决这个问题,但我已经创建了一个解决方案.由于 random 不起作用,并且 select count 不会给我一个数字来玩,这是一个很长的路要走,但它会给我们一个随机数字来查询我们的数据库.

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);

    }

这篇关于SQLite Random 在浏览器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆