我想插入一些数据到一个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();
}
}
}
你得到任何错误信息中插入的另一种方式?例外?你如何检查表是否为空? – Steve
检查,看看是否有任何参数的值要传递到SQL字符串,如shift.EmployeeID,shift.StartTime等都是NULL – Harsh
@Steve我得到这样的:SQL逻辑错误或丢失的数据库 近“06”:语法错误。 我通过查找我的系统中的db文件并通过sqlite浏览器检查它来检查。 –