2015-11-29 94 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; 
      } 

     } 
    } 

}