2016-03-24 132 views
-1

我創建了幾個連接到同一個數據庫的程序。而這種代碼在一個點上停止了與例外的工作:Сonstraint失敗UNIQUE約束失敗

約束失敗UNIQUE約束失敗:moz_cookies.name, moz_cookies.host,moz_cookies.path,moz_cookies.origin屬性。

接下來應該做什麼?

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


namespace ReflCookie 
{ 
    class CookieSQLite 
    { 
     protected SQLiteConnection SqLiteConnection; 
     protected MyDBContext MyDbContext = new MyDBContext(); 

     public CookieSQLite() 
     { 
     } 

     public CookieSQLite(string database) 
     { 
      SqLiteConnection = new SQLiteConnection(@"DataSource= "+database); 
      try 
      { 
       SqLiteConnection.Open(); 
      } 
      catch (Exception ex) 
      { 
       throw; 
      } 
     } 

     public List<CookieRow> GetCookie() 
     { 
      SQLiteCommand sqLiteCommand = new SQLiteCommand(); 
      sqLiteCommand.CommandText = "Select * from `moz_cookies` order by `id` asc"; 
      sqLiteCommand.Connection = SqLiteConnection; 
      SQLiteDataReader sqLiteDataReader = sqLiteCommand.ExecuteReader(); 
       DataTable dataTable = new DataTable(); 
       List<CookieRow> cookies = new List<CookieRow>(); 
       dataTable.Load(sqLiteDataReader); 
       foreach (DataRow row in dataTable.Rows) 
       { 
        CookieRow cookie = new CookieRow(); 
        cookie.Id = Convert.ToInt32(row.ItemArray[0].ToString()); 
        cookie.BaseDomain = row.ItemArray[1].ToString(); 
        cookie.OriginAttributes = row.ItemArray[2].ToString(); 
        cookie.Name = row.ItemArray[3].ToString(); 
        cookie.Value = row.ItemArray[4].ToString(); 
        cookie.Host = row.ItemArray[5].ToString(); 
        cookie.Path = row.ItemArray[6].ToString(); 
        cookie.Expiry = row.ItemArray[7].ToString(); 
        cookie.LastAccessed = row.ItemArray[8].ToString(); 
        cookie.CreationTime = row.ItemArray[9].ToString(); 
        cookie.IsSecure = Convert.ToInt32(row.ItemArray[10].ToString()); 
        cookie.IsHttpOnly = Convert.ToInt32(row.ItemArray[11].ToString()); 
        cookie.AppID = Convert.ToInt32(row.ItemArray[12].ToString()); 
        cookie.InBrowserElement = Convert.ToInt32(row.ItemArray[13].ToString()); 

        cookies.Add(cookie); 
       } 
       return cookies; 
      } 

     public void DeleteCookieRows() 
     { 
      SQLiteCommand sqLiteCommand = new SQLiteCommand(); 
      sqLiteCommand.CommandText= "DELETE FROM moz_cookies where Name = 'NID'"; 
      sqLiteCommand.Connection = SqLiteConnection; 
      sqLiteCommand.Parameters.AddWithValue("Name", "NID"); 
      sqLiteCommand.ExecuteNonQuery(); 

     } 

     public void IdCookieIsEmpty(List<CookieRow> c, List<CookieRow> cookiesRows, string profileCookies) 
     { 
      if (cookiesRows != null && cookiesRows.Count != 0) 
      { 
       UpdateCookie(c, profileCookies); 
      } 
      else 
      { 
       List<CookieRow> cookieRows = GetCookie(); 
       List<CookieRow> idCookies = cookieRows.Where(row => row.Name == "_gads").ToList(); 
       if (idCookies != null && idCookies.Count != 0) 
       {     
        foreach (CookieRow cookie in idCookies) 
        { 
         MyDbContext.Cookie.Add(cookie); 
        } 
        MyDbContext.SaveChanges(); 
        Console.WriteLine("Cookies saved to 'AMAZONE_db' file"); 
        Console.WriteLine("Press any key to close application"); 
       } 
       List<CookieRow> qLiteCookie = MyDbContext.Cookie.ToList(); 
       UpdateCookie(qLiteCookie, profileCookies); 

      } 

     } 

     private void UpdateCookie(List<CookieRow> c, string profileCookies) 
     { 
      foreach (CookieRow cookie in c) 
      { 
       SQLiteCommand sqLiteCommand = new SQLiteCommand(); 
       SQLiteConnection qLiteConnection = new SQLiteConnection(@"DataSource= " + profileCookies); 
       qLiteConnection.Open(); 
       sqLiteCommand.CommandText = 
        "UPDATE moz_cookies SET Id='"+cookie.Id+"',BaseDomain = '" + cookie.BaseDomain + "', " + 
        "originAttributes='" + cookie.OriginAttributes + "'," + 
        "name='" + cookie.Name + "', value='" + cookie.Value + "', " + 
        "host='" + cookie.Host + "',path='" + cookie.Path + "', " + 
        "expiry='" + cookie.Expiry + "', lastAccessed='" + cookie.LastAccessed + "', " + 
        "creationTime='" + cookie.CreationTime + "', isSecure='" + cookie.IsSecure + "', " + 
        "isHttpOnly='" + cookie.IsHttpOnly + "', appId='" + cookie.AppID + "', " + 
        "inBrowserElement='" + cookie.InBrowserElement + "'"; 

       sqLiteCommand.Connection = SqLiteConnection; 
       sqLiteCommand.ExecuteNonQuery(); 

       qLiteConnection.Close(); 

       Console.WriteLine("D"); 
      } 
     } 
    } 
} 
+0

顯然,您需要找出違反您的唯一約束的原因。既然你沒有發佈你的模式,我只能假設這些字段有UNIQUE,當你在這些列中插入相同的數據時,它在邏輯上失敗。 –

+0

你應該做的下一步是讀[SQL注入](http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work )並使用參數化查詢。之後,找出你想如何處理已經在你的數據庫中的cookie記錄。 – Rik

回答

0

你得到的錯誤消息簡直是在告訴你什麼是錯的:有已經存在於你的表格,表格中moz_cookies.namemoz_cookies.hostmoz_cookies.pathmoz_cookies.origin相同值的記錄,並有防止添加新的約束。

你想如何處理,取決於你。

此外,我建議你閱讀SQL Injection,因爲你的代碼非常易受攻擊。