加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite – 读取chrome历史记录时数据库文件锁定错误c#

发布时间:2020-12-12 18:53:47 所属栏目:百科 来源:网络整理
导读:我正在开发一个需要Chrome浏览器历史记录的应用程序.我已经编写了一个C#代码来获取历史记录.但是我的代码中有两个问题,我无法弄清楚. 有这个警告. 警告1正在构建的项目的处理器体系结构“MSIL”与参考“System.Data.SQLite”,“AMD64”的处理器体系结构之间
我正在开发一个需要Chrome浏览器历史记录的应用程序.我已经编写了一个C#代码来获取历史记录.但是我的代码中有两个问题,我无法弄清楚.

>有这个警告.

警告1正在构建的项目的处理器体系结构“MSIL”与参考“System.Data.SQLite”,“AMD64”的处理器体系结构之间存在不匹配.这种不匹配可能会导致运行时故障.请考虑通过Configuration Manager更改项目的目标处理器体系结构,以便在项目和引用之间调整处理器体系结构,或者依赖于具有与项目的目标处理器体系结构相匹配的处理器体系结构的引用. ChromeData

>有这个错误

SQLite错误(5):数据库被锁定

我试过关闭浏览器,但仍然有这个错误.但是,当我创建历史文件的副本并重命名它时,给出了它的路径而不是历史记录,程序正在运行,它可以读取文件并获取数据.
我无法弄清楚错误的位置.所以,请帮忙.我发布了我的3类文件.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SQLite;
using System.Data;

namespace ChromeData
{
    class GoogleChrome
    {
    public List<URL> Urls = new List<URL>();
    public IEnumerable<URL> GetHistory()
    {
        string DocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        //Console.WriteLine(DocumentsFolder);
        string[] tempstr = DocumentsFolder.Split('');
        foreach(string s in tempstr)
        {
            Console.WriteLine(s);
        }
        string tempstr1 = "";
        DocumentsFolder += "GoogleChromeUser DataDefault";
        if(tempstr[tempstr.Length-1] != "Local")
        {
            for(int i =0; i<tempstr.Length-1;i++)
            {
                tempstr1 += tempstr[i] + "";
            }
            DocumentsFolder = tempstr1 + "LocalGoogleChromeUser DataDefault";
        }
        Console.WriteLine(DocumentsFolder);
        if(Directory.Exists(DocumentsFolder))
        {
            return ExtractUserHistory(DocumentsFolder);
        }
        return null;
    }

    public IEnumerable<URL> ExtractUserHistory(string folder)
    {
        DataTable HistoryData = ExtractFromTable("urls",folder);

        foreach(DataRow row in HistoryData.Rows)
        {
            string url = row["url"].ToString();
            string title = row["title"].ToString();

            URL u = new URL(url.Replace(''',' '),title.Replace(''',"Google Chrome");
            Urls.Add(u);
        }

        return Urls;
    }

    DataTable ExtractFromTable(string table,string folder)
    {
        SQLiteConnection sql_con;
        SQLiteDataAdapter DB;
        SQLiteCommand sql_cmd;
        string dbpath = folder + "History";

        DataTable DT = new DataTable();

        if(File.Exists(dbpath))
        {
            try
            {
                sql_con = new SQLiteConnection("Data Source=" + dbpath + ";Version=3;New=False;Compress=True;");

                sql_con.Open();
                sql_cmd = sql_con.CreateCommand();

                string CommandText = "select * from " + table;

                DB = new SQLiteDataAdapter(CommandText,sql_con);

                DB.Fill(DT);
                sql_con.Close();

            }
            catch(Exception e)
            {
                TextWriter errorWriter = Console.Error;
                errorWriter.WriteLine(e.Message);
            }
        }
        return DT;
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ChromeData
{
    class TestClass
    {
    public static List<URL> Urls = new List<URL>();

    public static void Main()
    {
        string path = @"C:UsersPublicDesktophistory.txt";
        GoogleChrome g = new GoogleChrome();
        Urls = (List<URL>)g.GetHistory();

        using(StreamWriter sw = File.CreateText(path))
        {
            foreach(URL u in Urls)
            {
                sw.WriteLine(u.url);
            }
        }

        Console.ReadLine();
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChromeData
{
    class URL
    {
    public string url;
    public string title;
    public string browser;

    public URL(string url,string title,string browser)
    {
        this.browser = browser;
        this.title = title;
        this.url = url;
    }


}

解决方法

一种解决方案是将文件复制到临时位置并从那里读取.

string source = @"C:Users{USERNAME}AppDataLocalGoogleChromeUser DataDefaultHistory";
string target = @"C:TempHistory";

if (File.Exists(target))
{
    File.Delete(target);
}

File.Copy(source,target);

string cs = @"Data Source=" + target;
string sql = "Select * From urls";

using (SQLiteConnection c = new SQLiteConnection(cs))
{
    c.Open();
    using (SQLiteCommand cmd = new SQLiteCommand(sql,c))
    {
        using (SQLiteDataReader rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {
                Console.WriteLine(rdr[1].ToString());
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读