缺少运算符使用数据库(unity3d)

时间:2013-12-15 16:56:27

标签: database

我在我的游戏中使用数据库,查询是错误的运算符,我的代码是

public void Execute(){

jsScript = Camera.main.GetComponent(); ReadStudent(Application.dataPath+"/dictionary.accdb","dict","word","word","=",jsScript.words);

}
internal void ReadStudent(string filetoread,string tableName, string itemToSelect, string wCol, string wPar, string wValue){

  string connection = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" + filetoread;
  Debug.Log(connection);

    string sqlQuery ="SELECT word FROM"+ tableName +"WHERE" + wCol + wPar+"'"+wValue+"";

    OdbcConnection con = new OdbcConnection(connection);
    OdbcCommand cmd = new OdbcCommand(sqlQuery,con);
    DataTable dt = new DataTable("dic");


    try{
        con.Open();
        OdbcDataReader reader = cmd.ExecuteReader();
        dt.Load(reader);
        reader.Close();
        con.Close();
    }

    catch (Exception ex){
     //text = dt.Rows[3][1].ToString();
        Debug.Log(ex.ToString());
    }

    finally{
        if (con.State!=ConnectionState.Closed){
            con.Close();
        }
        con.Dispose();
    }

    if (dt.Rows.Count>0){
            text = dt.Rows[0]["word"].ToString();
    }
}

它给出错误:System.Data.Odbc.OdbcException:ERROR [42000] [Microsoft] [ODBC Microsoft Access Driver]查询表达式'word FROMdictWHEREword ='MX'中的语法错误(缺少运算符)。

2 个答案:

答案 0 :(得分:0)

错误显而易见,您的SELECT语句缺少空格:

[SELECT] 'word FROMdictWHEREword='MX'.

这个格式错误的SQL由以下行创建:

string sqlQuery ="SELECT word FROM"+ tableName +"WHERE" + wCol + wPar+"'"+wValue+"";

只需添加空格即可,你应该没问题:

string sqlQuery = "SELECT word FROM "+ tableName + " WHERE " + wCol + " " + wPar + " '" + wValue + "'";

答案 1 :(得分:0)

看起来你错过了命令中的空格:

select word FROMdictWHEREword='MX'

应该读取类似

的内容
select word FROM dict WHERE word = 'MX'