lundi 14 décembre 2015

Trying to insert into an SQLite database in c# on unity

i'm currently doing an assignment on an elevator and the final part is to have a database that records when the lift has opened and on what floor.

in unity i am getting this exception:

"InvalidOperationException: No connection associated with this command"

copy of full project if you want to have a look.

http://ift.tt/1Upwy1o

this is the code i have used to try and create an insert statement

(sorry if the comments make it messy i do it so i can keep track of what everything does)

 void DataBase()
{
    string currenttime = DateTime.Now.ToShortTimeString();                      //sets currenttime variable to the current time
    string currentdate = DateTime.Now.ToShortDateString();                      //sets currentdate variable to the current date
    int currentfloor = 0;                                                       //creates currentfloor variable

    if (FG)                                                                     //if FG is true
    {
        currentfloor = 1;                                                       //sets current floor to 0
    }
    else if (F1)                                                                //if F1 is true
    {
        currentfloor = 2;                                                       //sets current floor to 1
    }
    else if (F2)                                                                //if F2 is true
    {
        currentfloor = 3;                                                       //sets current floor to 2
    }


    IDbConnection dbconn;

    string conn = "URI=file:" + Application.dataPath + "/lift_DB.s3db";         //Path to database.

    dbconn = (IDbConnection)new SqliteConnection(conn);                         //creates database connection

    dbconn.Open();                                                              //Open connection to the database.

    IDbCommand dbcmd = dbconn.CreateCommand();                                  //creates command on connection

    string sqlInsert = "INSERT INTO lift_TB (Date,Time,Floor) VALUES (@currentdate,@currenttime,@currentfloor);";   // creates insert statement on sql insert string

    SqliteCommand command = new SqliteCommand();                                //creates new sqlite command
    dbcmd.Parameters.Add(new SqliteParameter("@currentdate", currentdate));     //gives @currentdate sqlite parrameter data from current date variable
    dbcmd.Parameters.Add(new SqliteParameter("@currenttime", currenttime));     //gives @currenttime sqlite parrameter data from current time variable
    dbcmd.Parameters.Add(new SqliteParameter("@currentfloor", currentfloor));   //gives @currentfloor sqlite parrameter data from current floor variable

    dbcmd.CommandText = sqlInsert;                                              // sets dbcmd.CommandText to be equal to the insert statement created above
    command.ExecuteNonQuery();                              
   // not sure what this is or if its needed
    IDataReader reader = dbcmd.ExecuteReader();
    while (reader.Read())
    {


    }

    reader.Close();                                                             //closes reader ----- not sure if this is need since im not reading from a database only writing to
    reader = null;
    //i assume below here just closes the connections to the data base
    dbcmd.Dispose();                                                            //disposes of command
    dbcmd = null;
    dbconn.Close();                                                             //closes connection to database
    dbconn = null;
}

Aucun commentaire:

Enregistrer un commentaire