- Home /
Question by
Strangerweather · Mar 31, 2016 at 08:00 AM ·
androidscripting problemerrordatabasesqlite
Help with SQLite for Android Errors in Unity3D?
Hi guys! I am trying to adapt a script to work with SQLite4Unity3D but I am struggling with 3 errors. I was wondering if anyone could help. Here is the script. I have adapted it as far as I could:
using UnityEngine;
using SQLite4Unity3d;
using System;
public class QuizStart : MonoBehaviour
{
private SQLiteConnection connection;
private int[] rowIndices;
private int questionIndex;
void Start()
{
// Initialization
string connectionString = "URI=file:" + Application.dataPath + "/quizz.db";
connection = (SQLiteConnection)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
SQLiteCommand 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.
SQLiteCommand 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;
}
}
}
My 3 errors are with : CreateCommand() ll. 34 and 47/ ExecuteScalar l. 36/ IDataReader l. 51
Thanks for any help! :)
Comment
Your answer
Follow this Question
Related Questions
SQLite Transaction Update Problem on Android 0 Answers
SqlLite , android and unity 1 Answer
Unity sqlite to android: no such table 0 Answers