6

I developed an applicaiton that works fine <= Windows 7. It uses SQLlite database with VB and C#. However on Windows 8 (Never had this specific problem on other windows os) there is an issue when trying to write to database

System.Data.SQLite.SQLiteException: Attempt to write a read-only database

I created database file on windows 8 pc like:

 Try
     If (Not File.Exists(System.Environment.CurrentDirectory & "\MY_DB.db")) Then
                Dim conn = New SQLite.SQLiteConnection("Version=3;New=True;Compress=False;Read Only=False;Data Source=" & System.Environment.CurrentDirectory & "\MY_DB.db")
                conn.Open()
                '...DO STUFF  
                conn.Close()
                conn.Dispose()
                conn = Nothing
     End If

 Catch ex As Exception
            'Throw ex
            Return False
 End Try

But didn't work.

So basically I have tried:

  1. Added Read Only=False when creating db file but didn't work.
  2. The folder the database resides in must have write permissions, as well as the actual database file. So did it, didn't work (on windows 7 would look like enter image description here).

What can I do to make databse writable ?

3
  • 1
    I've had the same issue and it was permissions on the folder. Did you try to give the "Everyone" group the modify & write permission? Commented Dec 24, 2013 at 17:17
  • Why the db is accessed via System.Environment.CurrentDirectory? This won't work if the user opened the file dialog (which changes the current directory to the selection). And the db should be put in application data, not with the program. Commented Dec 24, 2013 at 17:40
  • actually, database is just for log issues that is reason of existing there, however when trying to write any event I get this error... Commented Dec 24, 2013 at 19:07

1 Answer 1

6

I don't know what is the current directory at the instant of your call, but I would be sure to put my database in a folder where every user have read/write permission by design.

Why don't use the folders represented by the Environment.SpecialFolder enum?

You could change your code to something like this

 Try
      Dim appFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
      appFolder = Path.Combine(appFolder, "MyAppDatabaseFolder")
      If Not Directory.Exists(appFolder) Then
           Directory.CreateDirectory(appFolder)
      End If
      Dim myDB = Path.Combine(appFolder, "MY_DB.db")
      If (Not File.Exists(myDB)) Then
          .....
      End If
 Catch ex As Exception
      'Throw ex
      Return False
 End Try
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to give permissions to folder and give give the "Everyone" group the modify & write permission programatically as @Mark PM suggested?
You could look at this question and check if it is feasible for you. However I suggest to avoid this path. Follow the rules estabilished by the OS. The folder to store application data for all user (a file database is the perfect example) is defined using the SpecialFolder.CommonApplicationData. Using different folders is possible but, why start a fight with the OS? In a way or another you will lose something (surely the time required to make things right is lost)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.