如何将SQLite数据库中的文本与本地保存在文本文件中的某些文本进行比较?

时间:2018-07-15 09:57:17

标签: .net vb.net sqlite

我有一个程序,可以从Chrome的SQLite database 获取Google Chrome中的历史记录。效果很好,结果是这样的:

Image of the program.

使用此代码:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim google As String = (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\Google\Chrome\User Data\Default\History")
        Dim fileName As String = DateTime.Now.Ticks.ToString
        File.Copy(google, (Application.StartupPath + ("\" + fileName)))
        Dim con As SQLiteConnection = New SQLiteConnection(("DataSource = " + (Application.StartupPath + ("\" + (fileName + ";Versio=3;New=False;Compress=True;")))))
        Dim da As SQLiteDataAdapter = New SQLiteDataAdapter("select * from urls order by last_visit_time desc", con)
        Dim ds As DataSet = New DataSet
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        con.Close()
    End Sub

现在,这是给我带来麻烦的原因:

如何将数据库URL文本与具有白名单网站列表的本地托管.txt文件进行比较?

这是我到目前为止研究的内容,它是该程序结构的一部分:

Dim file As String
file = My.Computer.FileSystem.ReadAllText("websitelist.txt").ToString

If file.Contains("exampledomain.com") Then // from SQLite Database
    MessageBox.Show("A query exists in the database.")
End If

如何使用 SQLite 来做到这一点?

  

您需要的任何信息,请在下面评论。

1 个答案:

答案 0 :(得分:0)

我添加了using ... End Using块,以确保即使有错误,也可以关闭并正确放置对象。我只是使用了DataTable和DataReader来减轻负担。将url字段添加到List(Of T),然后使用Linq查询获取匹配的数据。我只是将其打印到立即窗口中,但是您可以根据需要使用它。

Private Sub btnGoogleHistory_Click(sender As Object, e As EventArgs) Handles btnGoogleHistory.Click
        Dim google As String = (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\Google\Chrome\User Data\Default\History")
        Dim fileName As String = DateTime.Now.Ticks.ToString
        File.Copy(google, (Application.StartupPath + ("\" + fileName)))
        Dim dt As New DataTable()
        Using con As SQLiteConnection = New SQLiteConnection($"DataSource = {Application.StartupPath }\{fileName};Versio=3;New=False;Compress=True;")
            Using cmd As New SQLiteCommand("select * from urls order by last_visit_time desc ", con)
                con.Open()
                Using dr As SQLiteDataReader = cmd.ExecuteReader()
                    dt.Load(dr)
                End Using
            End Using
        End Using
        DataGridView1.DataSource = dt
        Dim lstGoogleHistory As New List(Of String)
        For Each row As DataRow In dt.Rows
            lstGoogleHistory.Add(row("url").ToString)
        Next
        Dim WhiteListText As String = My.Computer.FileSystem.ReadAllText("websitelist.txt").ToString
        Dim InWhiteList = From uri In lstGoogleHistory
                          Where uri.All(Function(x) WhiteListText.Contains(uri))
                          Select uri
        For Each URL In InWhiteList
            Debug.Print(URL)
        Next
    End Sub