你好,我的C#脚本有点问题,它应该运行在一个列表数组中,然后更新sqlite数据库上的值,但是当试图更新数据库时,它似乎被锁定了--这是我的代码
using UnityEngine;
using System.Collections;
using Parse;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Mono.Data.Sqlite;
using System.Data;
using System.Threading.Tasks;
public class MyTestArray : MonoBehaviour {
public int MyInt;
// Use this for initialization
void Start () {
int[] pets = { 1, 2, 3, 4, 5 };
// ... Loop with the foreach keyword.
foreach (int value in pets) {
Debug.Log (value);
MyInt = value;
string sqlQuery;
string conn = "";
#if UNITY_EDITOR
conn = "URI=file:" + Application.persistentDataPath + "/" + "DB.db";
#elif UNITY_IPHONE
conn = "URI=file:" + Application.persistentDataPath + "/" + "DB.db";
#elif UNITY_STANDALONE_WIN
conn = "URI=file:" + Application.persistentDataPath + "/" + "DB.db";
#elif UNITY_ANDROID
conn = "URI=file:" + Application.persistentDataPath + "/" + "DB.db";
#endif
IDbConnection dbconn;
dbconn = (IDbConnection)new SqliteConnection (conn);
dbconn.Open (); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand ();
PlayerPrefs.SetString ("Question", "1");
sqlQuery = "select SUM(qo.Score) from [Answer] as a inner join Questionas qo on a.Question= qo.QuestionI inner join Question as q on qo.QuestionId = q.QuestionId where q.QID=" + value;
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader ();
while (reader.Read()) {
int RiskNumber = reader.GetInt32 (0);
Debug.Log (RiskNumber);
string sqlQuery2 = "";
string conn2 = "";
#if UNITY_EDITOR
conn2 = "URI=file:" + Application.persistentDataPath + "/" + "DB.db";
#elif UNITY_IPHONE
conn2 = "URI=file:" + Application.persistentDataPath + "/" + "DB.db";
#elif UNITY_STANDALONE_WIN
conn2 = "URI=file:" + Application.persistentDataPath + "/" + "DB.db";
#elif UNITY_ANDROID
conn2 = "URI=file:" + Application.persistentDataPath + "/" + "DB.db";
#endif
IDbConnection dbconn2;
dbconn2 = (IDbConnection)new SqliteConnection (conn2);
dbconn2.Open (); //Open connection to the database.
IDbCommand dbcmd2 = dbconn2.CreateCommand ();
if (MyInt == 1){
sqlQuery2 = "UPDATE UserScore SET MyScore1="+RiskNumber;
} else if (MyInt == 2){
sqlQuery2 = "UPDATE UserScore SET MyScore2="+RiskNumber;
} else if (MyInt == 3){
sqlQuery2 = "UPDATE UserScore SET MyScore3="+RiskNumber;
} else if (MyInt == 4){
sqlQuery2 = "UPDATE UserScore SET MyScore4="+RiskNumber;
} else if (MyInt == 5){
sqlQuery2 = "UPDATE UserScore SET MyScore5="+RiskNumber;
}
Debug.Log (sqlQuery2);
dbcmd2.CommandText = sqlQuery2;
IDataReader reader2 = dbcmd2.ExecuteReader ();
reader2.Close ();
reader2 = null;
dbcmd2.Dispose ();
dbcmd2 = null;
dbconn2.Close ();
dbconn2 = null;
}
reader.Close ();
reader = null;
dbcmd.Dispose ();
dbcmd = null;
dbconn.Close ();
dbconn = null;
}
}
}这是我创建查询的方式,还是我错过了一些东西,任何帮助都会是美妙的欢呼。
发布于 2015-06-18 14:43:51
哦,伙计,你真的需要考虑重构一些代码,这对性能来说也不是最好的,但是问题是,在打开主连接的同时,仍然打开了另一个连接,只是重复使用相同的连接。
它发生在你的时间循环中。
编辑:注意到您的代码,看起来您应该能够执行单个sql查询,方法是从select进行更新,因此不需要过度循环。
https://stackoverflow.com/questions/30918143
复制相似问题