Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Strangerweather · Mar 26, 2016 at 12:56 PM · randomstringdatabasesqliterepeat

Quick SQLite question for Unity Game Database

Hi guys, quick sqlite3 question. Can anyone help? I need random selection but no repeats.

Currently I have:

 string sqlQuery = "SELECT * FROM english ORDER BY RANDOM () LIMIT 1;";

any ideas?

Thanks a lot for any help! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Paul-Jan · Mar 26, 2016 at 07:41 PM

Usually you can get away with determining the row index to select using code, then retrieving that row using SQL... unless you have a really large table. Here is what you do

  1. Generate an array with length COUNT, containing simply the indices 0...[Count-1]

  2. Randomize the array using repeated shuffle.

  3. Now simply walk through the array, and use the value as the row index, i.e.

    string sqlQuery = String.Format("SELECT * from english LIMIT 1 OFFSET %d", rowIndex);

Once you reach the end of the array, you've visited all rows exactly once and you can either start over or generate a new array.

Example code

 using UnityEngine;
 using System.Data;
 using Mono.Data.Sqlite;
 using System;
 
 public class QuizStart : MonoBehaviour {
 
     private IDbConnection connection;
     private int[] rowIndices;
     private int questionIndex;
 
 
     void Start () {
         // Initialization
         string connectionString = "URI=file:" + Application.dataPath + "/quizz.db"; 
         connection = (IDbConnection)new SqliteConnection(connectionString);
         connection.Open();
     
         InitializeQuestions();
 
         // Execute GetNextQuestion when you need to access the next question
         GetNextQuestion();
     }
 
     void OnDestroy() {
         connection.Close();
     }
 
     private void InitializeQuestions()
     {
         // Retrieve amount of questions
         IDbCommand cmd = connection.CreateCommand();
         cmd.CommandText = "SELECT COUNT(*) FROM english";
         var count = (Int64) cmd.ExecuteScalar();
 
         // Generate a shuffled array of question indices.
         rowIndices = new int[count];
         for (int i = 0; i < count; i++)
             rowIndices[i] = i;
         Shuffle(rowIndices);
     }
 
     public void GetNextQuestion()
     {
         // Read question from database. Do note that you'll have to use your own columns here.
         IDbCommand dbcmd = connection.CreateCommand();
         string sqlQuery = string.Format("SELECT * " + "FROM english LIMIT 1 OFFSET {0}", rowIndices[questionIndex]);
         dbcmd.CommandText = sqlQuery;
         using (IDataReader reader = dbcmd.ExecuteReader())
         {
             reader.Read();
             string question = reader.GetString(1);
             Debug.Log("question: " + question);
             reader.Close();
         }
 
         questionIndex++;
         if (questionIndex > rowIndices.Length)
             questionIndex = 0;
     }
 
     private static void Shuffle<T>(T[] array)
     {
         var rnd = new System.Random();
         int n = array.Length;
         while (n > 1)
         {
             int k = rnd.Next(n--);
             T temp = array[n];
             array[n] = array[k];
             array[k] = temp;
         }
     }
 }

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Strangerweather · Mar 26, 2016 at 07:59 PM 0
Share

Wow, this looks much more complex than I thought! :-) It will be a big table once I finish, with hundreds of entries (for a trivia game)... Thank you so much for explaining the process in detail. I really appreciate it! Now I'm off to try and implement it! ;-)

avatar image Paul-Jan · Mar 26, 2016 at 08:39 PM 1
Share

The good news is "hundreds of entries" will work perfectly fine with this approach, as you only need to create a single array of integers and enumerate it once more to swap the entries.If you run into any problems in the implementation make sure to post back and I'll extend the answer.

avatar image Strangerweather Paul-Jan · Mar 27, 2016 at 07:51 AM 0
Share

Hi again! So, this is what I have at present. No errors showing but not working. I'm sure I did something wrong. I'm still learning C#:

 public void GetData()
     {
         using (IDbConnection dbConnection = new SqliteConnection(connectionString))
         {
             dbConnection.Open();
             using (IDbCommand dbCmd = dbConnection.CreateCommand())
             {
                 string sqlQuery = String.Format("SELECT * from english LI$$anonymous$$IT 1 OFFSET 20", 0,19);
 
                 dbCmd.CommandText = sqlQuery;
 
                 using (IDataReader reader = dbCmd.ExecuteReader())
                 {
                     while (reader.Read())
                     {
                         questionText.text = reader.GetString(1);
                         
 
                         for (int i = 0; i < answerButtons.Length; i++)
                         {
                             answerButtons[i].GetComponentInChildren<Text>().text = reader.GetString (2 + i);
                         }
 
                         correctAnswerText = reader.GetString(7);
                         Debug.Log(correctAnswerText);
 
                         correctAnswerNum = reader.GetInt32(8);
                         Debug.Log(correctAnswerNum);                    
 
                        
 
                         Debug.Log (reader.GetString(1) + reader.GetString(2) + reader.GetString(3) + reader.GetString(4) + reader.GetString(5) + reader.GetString(6));
                     }
 
                     dbConnection.Close();
                     reader.Close();
                 }
             }
 
         }
 
        }
 
     int[] array = Enumerable.Range(0, 19).ToArray();
 
 
     public static void Shuffle<T>(T[] array)
     {
         var rnd = new System.Random();
         int n = array.Length;
         while (n > 1)
         {
             int k = rnd.Next(n--);
             T temp = array[n];
             array[n] = array[k];
             array[k] = temp;
        
         }
     }
 }

Any ideas? :)

avatar image Paul-Jan Strangerweather · Mar 27, 2016 at 08:03 PM 1
Share

I've added working code that illustrates the approach. Hope that helps! :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

41 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to avoid repeating elements on list (random.range) 1 Answer

Unity 4 Android Game cannot access SQLite DB (C# Script) 1 Answer

I am trying to Randomize my Answers? 1 Answer

SQLite Transaction Update Problem on Android 0 Answers

How to Detect Touch on a Random String from a List? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges