我正在尝试在sqlite数据库中连接我的单位,但出现错误。错误是什么意思?
IndexOutOfRangeException: Array index is out of range.
Mono.Data.Sqlite.SqliteDataReader.GetSQLiteType (Int32 i)
Mono.Data.Sqlite.SqliteDataReader.VerifyType (Int32 i, DbType typ)
Mono.Data.Sqlite.SqliteDataReader.GetString (Int32 i)
callOneUser.GetOneUsername () (at Assets/callOneUser.cs:41)
callOneUser.Start () (at Assets/callOneUser.cs:22)
我正在尝试使用此代码。这是我无效的Start()
void Start()
{
Connection = "URI=file:" + Application.dataPath + "/English_Booster_Game_DB.s3db"; //Path to database.
GetOneUsername();
}
这是GetOneUsername()进程。
public void GetOneUsername()
{
using (IDbConnection dbConnection = new SqliteConnection(Connection))
{
dbConnection.Open();
using (IDbCommand dbCmd = dbConnection.CreateCommand())
{
string selectoneusername = "select UserData_name from user_data where UserData_status = 1";
dbCmd.CommandText = selectoneusername;
using (IDataReader reader = dbCmd.ExecuteReader())
{
while (reader.Read())
{
name.text = reader.GetString(1);
}
dbConnection.Close();
reader.Close();
}
}
}
}
答案 0 :(得分:0)
在查询字符串中,您获得一列UserData_name
。
string selectoneusername = "select UserData_name from user_data where UserData_status = 1";
它只会返回C#中从“零” 开始的列和索引。因此,您应该更改脚本的这一行:
name.text = reader.GetString(1);
收件人:
name.text = reader.GetString(0);
希望对您有帮助