Hello Team,
I am using this library to lock the database with a password.
Now I would like to share the database file using UIActivityViewController where I need to convert sqlite database to Data which can be shared using it.
When I try to convert sqlite database from it's URL I do read permission error like "The file āfile.sqliteā couldnāt be opened."
Is there any way I can open the database with passphase and share the Sqlite file using UIActivityViewController?
Thanks,
Malav Soni
Hello @malavagile,
When I try to convert sqlite database from it's URL I do read permission error like "The file āfile.sqliteā couldnāt be opened."
A SQLite file is a regular file. The SQLite connections opened by GRDB database queues and pools open database files for writing: this may be the reason why you get this error. I'm not 100% sure of this.
Still: it is dangerous to read or share a database file that is currently open by a database connection, since any background thread could perform database modifications while the file is being read. This is the recipe for data corruption or random errors.
I need to convert sqlite database to Data which can be shared using it.
Is there any way I can open the database with passphase and share the Sqlite file using UIActivityViewController?
You did not say the kind of conversion you want to perform, nor if the export should be encrypted.
I'll thus suggest something without being sure if it fits your needs: share a copy of the database. First export your database in a temporary path, and then share the content of this file.
An export technique is documented in the Backup chapter. I'm not familiar enough with SQLCipher to know if it supports the standard SQLite backup technique: you will have to try. You can also have a look at the sqlcipher_export SQLCipher function, introduced in the Encryption chapter.
Below, a sample code that exports non-encrypted data from a database queue with the backup technique:
func databaseContent(from dbQueue: DatabaseQueue) throws -> Data {
// Build a temporary path
let exportName = ProcessInfo.processInfo.globallyUniqueString
let exportPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(exportName)
// Export
try dbQueue.backup(to: DatabaseQueue(path: exportPath))
// Now there is no database connection left to the exported file.
// Reading is safe:
let data = try Data(contentsOf: URL(fileURLWithPath: exportPath))
// Drop exported file
try fileManager.removeItem(atPath: exportPath)
return data
}
Please share your findings and experience with us!
Any news, @malavagile?
Hello @groue ,
I found a way to share the SQLite file but the issue is when I share the SQLite file the table of the database removed and blank database is shared.
Do you have any idea about it?
Here is the way I share the database.
let activityViewController = UIActivityViewController(
activityItems: [NSURL.fileURL(withPath: databasePath)],
applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Thanks,
Malav Soni
@malavagile, you don't ask questions in a way that allows anyone to provide any useful answer.
Read again your own question above. Ask yourself how could UIActivityViewController be related to missing database tables. Seriously. It's like asking if the price of oranges can explain how the international space station is floating in space. And you ask me if I have any idea about it? Of course I don't have any idea about it, none, zilch, nada, nihil, rien du tout. Think more before asking, please. Write your questions as if you cared getting an answer.
@groue Of course I am finding the answer.
There is no defined way you have provided using which I can do this operation. I am finding alternative options of it.
Now when I share the URL or even copy file from one location to another location in my mac tables of it disappeared.
May be someone face any issue like that.. That's why I ask here.
Hello @groue : I found the way to export the database.
Now I want to replace the current database so right now I am just replacing the the file in document directory but it seems to not working.
Can you tell me how do I close the DB first and then replace it with the one I am importing from other apps?
Thanks,
Malav Soni
Jesus christ... @malavagile It's a thread. People come here trying to solve the same issue and your answer is "I found a way." omg brother... Would you mind to please share your solution with us?
Most helpful comment
@malavagile, you don't ask questions in a way that allows anyone to provide any useful answer.
Read again your own question above. Ask yourself how could UIActivityViewController be related to missing database tables. Seriously. It's like asking if the price of oranges can explain how the international space station is floating in space. And you ask me if I have any idea about it? Of course I don't have any idea about it, none, zilch, nada, nihil, rien du tout. Think more before asking, please. Write your questions as if you cared getting an answer.