SQLite RANDOM()问题

时间:2011-02-03 11:06:02

标签: javascript jquery sqlite html5 local-storage

我有一些JS创建一个SQLite数据库并插入一些数据。

我有一个查询它的函数,然后将结果信息作为警报吐出。

如果我尝试从数据库中取回一个随机字段,我得不到任何结果(也没有明显错误)。


  

<title>Golf score keeper</title>

<script src="http://www.google.com/jsapi"></script>

<script>
  google.load("jquery", "1.4.1");
</script>

<script>

  $(document).ready(function() {

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

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

      //THIS ONE FAILS

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

      tx.executeSql('SELECT * FROM  Strokes 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>

    

有人能理解这个吗?

3 个答案:

答案 0 :(得分:0)

这根本与SQLite无关。 (它只是SQLite是实现的SQL方言)
这是HTML5 Local Storage

示例:HTML5 Web Storage / localStorage(点击查看来源获取更多信息)

答案 1 :(得分:0)

我已经开始使用Chrome了。 RANDOM()不支持作为SQL Dialect中的函数。 Firefox 4 Beta 9中的结果相同。查询被视为无效!

答案 2 :(得分:0)

我没有解决问题,但我创造了一个解决方案。 由于随机不起作用,并且选择计数不会给我一个数字,这是一个很长的路要走,但它会给我们一个随机数字,在其中查询我们的数据库。

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

    }