title: SQLite Database becomes read only
body: <p>I have a c# plug-in that writes data to an SQLite database on our networked directory.  We implement a Panzura Server at our office (That I don't know much about). <a href="https://panzura.com/solutions-now/aec-solution/" rel="nofollow noreferrer">https://panzura.com/solutions-now/aec-solution/</a></p>
  
  <p>Two weeks in a row, at about 12:30pm - this Sqlite database became read only. On the first occasion, the "read-only" status was was released over the weekend.  Unfortunately, that did not happen this weekend.</p>
  
  <p>when I step through the code, i get an <code>SQLiteException 'attempt to write to a readonly database'.</code> error.</p>
  
  <p>If i use <code>FileInfo</code> on the db file, the <code>'readonly'</code> attribute is false. </p>
  
  <p>most of the sql statements are as following...</p>
  
  <pre><code>                    conn.Open();
                      using (SQLiteCommand cmd = new SQLiteCommand(conn))
                      {
                          cmd.CommandText = "INSERT INTO " +
                              "`User`(`UserName`, `UserLevel`) " +
                              "VALUES('" + System.Environment.UserName + "', '0')";
                          cmd.ExecuteNonQuery();
                      }
                      conn.Close();
  </code></pre>
  
  <p>My first observation is that the opening and closing of the connection may be redundant with the using statement.  </p>
  
  <p>I also noticed this sql statement...</p>
  
  <pre><code>            conn.Open();
  
              string sel = "SELECT * FROM User WHERE `userName`='"+System.Environment.UserName+"' LIMIT 1";
  
              using (SQLiteCommand cmd = new SQLiteCommand(sel, conn))
              {
                  using (SQLiteDataReader rdr = cmd.ExecuteReader())
                  {
                      while (rdr.Read())
                      {
                          NumOfResults++;
  
                          if (rdr.GetString(2) == "1")
                          {
                              count = 1;
                          } else { count = 0;  }
                      }
                  }
              }
  </code></pre>
  
  <p>In this case, the connection is opened, a using statement is used, but the connection is not closed.  I wonder (even though this routing runs a number of times a week) if this is leaving the database in a read only state? Although, i thought leaving a connection open would return a <code>SQLITE BUSY</code> exception.</p>
  
  <p>At this point, I don't know if this read-only issue is being caused by my code or the server.  I've been running this plugin for months with out issue - although the number of users accessing it is increasing.</p>
  
  <p>I'm not really sure how to trouble shoot this because once the SQLIte database is in a read only state, it's not really usable.  How do i force it out of a read only state?</p>
  
  <p>I'm not even really sure what the read-only state means right now - is the file read-only? or does the database have connections open that need to be closed?</p>
  
  <p>another detail; could the compress attribute on the connection string be an issue?</p>
  
  <pre><code>@"Data Source=I:\Practice Standards\BCMS\families.db; Version = 3; New = True; Compress = True; ";
  </code></pre>