2015-11-29 128 views
-1

我想插入一些數據到一個SQLite文件,但當我嘗試這樣做沒有東西被添加到文件,這是我第一次與SQLite處理,所以不太確定我在做它對。請詢問是否需要其他信息。C#SQLite插入不工作

這裏是源代碼。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Data.SQLite; 

namespace WpfApplication1 
{ 

    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      Readlines(); 
     } 

     public void Readlines() 
     { 
      string[] lines = System.IO.File.ReadAllLines(@"D:\Projects\GFKMVC\WpfApplication1\WpfApplication1\Opgave001.txt"); 
      Shift shift = new Shift(); 
      SQLiteConnection.CreateFile("TimePlan.sqlite"); 
      SQLiteConnection dbcon; 
      dbcon = new SQLiteConnection("Data Source=TimePlan.sqlite;Version=3;"); 
      dbcon.Open(); 
      //string DropTable = "DROP DATABASE TimePlane.sqlite"; 
      //SQLiteCommand dropCom = new SQLiteCommand(DropTable, dbcon); 
      //dropCom.ExecuteNonQuery(); 
      string CreateTable = "CREATE TABLE Shifts (EmployeeID INT, Date DATETIME, StartTime DATETIME, EndTime DATETIME, Break INT)"; 
      SQLiteCommand createCom = new SQLiteCommand(CreateTable, dbcon); 
      createCom.ExecuteNonQuery(); 

      foreach (string line in lines) 
      { 
       string[] fields = line.Split(';'); 
       shift.EmployeeID = Int32.Parse(fields[0]); 
       DateTime dt = DateTime.ParseExact(fields[1], "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture); 
       shift.Date = dt; 
       DateTime st = DateTime.ParseExact(fields[2], "HHmm", System.Globalization.CultureInfo.InvariantCulture); 
       shift.StartTime = st; 
       DateTime et = DateTime.ParseExact(fields[3], "HHmm", System.Globalization.CultureInfo.InvariantCulture); 
       shift.EndTime = et; 
       shift.Break = Int32.Parse(fields[4]); 
       string InsertSql = "INSERT INTO Shifts (EmployeeID, Date, StartTime, EndTime, Break) values ("+shift.EmployeeID+","+shift.StartTime+","+shift.EndTime+","+shift.Break+")"; 
       SQLiteCommand InsertCom = new SQLiteCommand(InsertSql, dbcon); 
       InsertCom.ExecuteNonQuery(); 
      } 
      dbcon.Close(); 
     } 
    } 
} 
+0

你得到任何錯誤信息中插入的另一種方式?例外?你如何檢查表是否爲空? – Steve

+0

檢查,看看是否有任何參數的值要傳遞到SQL字符串,如shift.EmployeeID,shift.StartTime等都是NULL – Harsh

+0

@Steve我得到這樣的:SQL邏輯錯誤或丟失的數據庫 近「06」:語法錯誤。 我通過查找我的系統中的db文件並通過sqlite瀏覽器檢查它來檢查。 –

回答

2

參數化查詢可以有很大的幫助,避免問題解析輸入值(和玩票瞭解Sql Injection here

foreach (string line in lines) 
{ 
    string[] fields = line.Split(';'); 
    shift.EmployeeID = Int32.Parse(fields[0]); 
    DateTime dt = DateTime.ParseExact(fields[1], "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture); 
    shift.Date = dt; 
    DateTime st = DateTime.ParseExact(fields[2], "HHmm", System.Globalization.CultureInfo.InvariantCulture); 
    shift.StartTime = st; 
    DateTime et = DateTime.ParseExact(fields[3], "HHmm", System.Globalization.CultureInfo.InvariantCulture); 
    shift.EndTime = et; 
    shift.Break = Int32.Parse(fields[4]); 
    string InsertSql = @"INSERT INTO Shifts 
         (EmployeeID, Date, StartTime, EndTime, Break) 
         values ($eid, $date, $stime, $etime, $break)"; 

    SQLiteCommand InsertCom = new SQLiteCommand(InsertSql, dbcon); 
    InsertCom.Parameters.Add("$eid", DbType.Int32).Value = shift.EmployeeID; 
    InsertCom.Parameters.Add("$date", DbType.Date).Value = shift.Date ; 
    InsertCom.Parameters.Add("$stime", DbType.Time).Value = shift.StartTime ; 
    InsertCom.Parameters.Add("$etime", DbType.Time).Value = shift.EndTime; 
    InsertCom.Parameters.Add("$break", DbType.Int32).Value = shift.Break; 
    InsertCom.ExecuteNonQuery(); 
} 

這樣,工作中的正確格式轉換你的價值觀數據庫引擎所期望的不再是字符串連接格式化規則的手中,而是數據庫引擎本身接收日期和時間值並按照它們的喜好使用它們而沒有轉換錯誤。

作爲一種副產品,您的查詢更加清晰並且不易出現錯誤,例如您錯過的錯誤來傳遞預期爲第二個值的日期。

+0

感謝這個偉大的例子!我知道SQL注入,但這個應用程序不會採取任何用戶輸入,所以我沒有那麼擔心 –

+0

@MattBaech請測試類型時間的兩個參數。我沒有辦法在這裏測試,在錯誤嘗試使用直接DbType.Date也爲他們 – Steve

+0

好,首先的DbType是不是內SQLite的認可的情況下,所以不得不進口「System.Data;」 當我運行它,我得到以下錯誤:提供給命令 –

0

使用SQLite的

foreach (string line in lines) 
{ 
    string[] fields = line.Split(';'); 
    shift.EmployeeID = Int32.Parse(fields[0]); 
    DateTime dt = DateTime.ParseExact(fields[1], "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture); 
    shift.Date = dt; 
    DateTime st = DateTime.ParseExact(fields[2], "HHmm", System.Globalization.CultureInfo.InvariantCulture); 
    shift.StartTime = st; 
    DateTime et = DateTime.ParseExact(fields[3], "HHmm", System.Globalization.CultureInfo.InvariantCulture); 
    shift.EndTime = et; 
    shift.Break = Int32.Parse(fields[4]); 

    using (var con = new SQLiteConnection { ConnectionString = "ConnectionString" }) 
    { 
     using (var command = new SQLiteCommand { Connection = con }) 
     { 
      con.Open(); 

      try 
      { 
       command.CommandText = @"INSERT INTO Shifts 
             (EmployeeID, Date, StartTime, EndTime, Break) 
             VALUES (@eid, @date, @stime, @etime, @break)"; 
      command.Parameters.AddWithValue("@eid", shift.EmployeeID); 
      command.Parameters.AddWithValue("@date", shift.Date); 
      command.Parameters.AddWithValue("@stime", shift.StartTime); 
      command.Parameters.AddWithValue("@etime", shift.EndTime); 
      command.Parameters.AddWithValue("@break", shift.Break); 
      command.ExecuteNonQuery(); 
      command.Parameters.Clear(); 
      } 
      catch(SQLiteException ex) 
      { 
       throw ex; 
      } 

     } 
    } 

}