检查文件是否是有效的SQLite数据库 [英] Check if a file is a valid SQLite database
问题描述
我需要检查一个文件(具有未知的扩展名)是否是有效的SQLite数据库。
I need to check whether a file (with unknown extension) is a valid SQLite database.
我的函数很好,但是当它失败,退出函数后锁定:
My function works fine, but when it fails, the file is still locked after exiting the function:
Public Function IsSqliteDB(ByVal uPath As String) As Boolean
'Workaround for my problem: Make a copy and work on this because this function is going to lock the file unfortunately
Dim sNewPath As String
sNewPath = Settings.Locations.Folder_LocalAppData_Temp & "\temp_" & Replace(Now.ToString, ":", "_") & ".db"
modIO.FileForceCopy(uPath, sNewPath)
Dim bIsSQLiteDB As Boolean = False
Dim c As New dhRichClient3.cConnection
Dim r As dhRichClient3.cRecordset
Try
Dim b As Boolean = c.OpenDB(sNewPath) 'returns true although this is not an sqlite-db. Can't do anything about it
R = c.OpenRecordset("SELECT * FROM sqlite_master")
bIsSQLiteDB = True
Catch ex As Exception
r = Nothing
c = Nothing
Finally
r = Nothing
c = Nothing
End Try
modIO.DeleteFile(sNewPath)'this fails. File is locked
Return bIsSQLiteDB
End Function
有没有人看到我可以确保该文件不再被锁定?或者我真的需要处理文件的副本,因为COM组件的行为不是真正已知的(关闭源不幸的是)?
我真的可以使用备份,但文件可能真的很大(> 1 GB),所以我想避免做一个副本,如果我可以避免它。
Does anybody see where I could ensure that the file is not locked anymore? Or do I really have to work with a copy of the file because the behaviour of the COM component is not really known (closed source unfortunately)? I could indeed work with a backup, but the file may be really large (> 1 GB), so I would like to avoid making a copy to work on if I could avoid it.
dhRichClient中没有close函数。 关闭一旦cConection出来的范围内调用,我猜。
There is no "close" function in dhRichClient. "Close" is called internally once the cConection goes out out scope, I guess. Calling GC.Collect() at the end of the function does not help.
推荐答案
要确定一个文件是否是SQLite数据库,请在函数末尾调用GC.Collect ,只需检查数据库标头的前16个字节即可。
To determine if a file is an SQLite database, just check the first 16 bytes of the database header.
来自tmighty的注释:
from tmighty's comment:
Public Function IsSqliteDB(ByVal uPath As String) As Boolean
Dim bytes(16) As Byte
Using fs As New IO.FileStream(uPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
fs.Read(bytes, 0, 16)
End Using
Dim text As String = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Return text.Contains("SQLite format")
End Function
这篇关于检查文件是否是有效的SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!