在遵循这个线程中好人提供的一些步骤之后,我能够将 CSV 文件放入列表中,从而为写入 SQLite 数据库做好准备。
然而,我又遇到了麻烦。我创建一个数据库文件,告诉 SQLite 引擎(或其他任何名称)通过用户在文本框中提供的名称创建一个表。但是,当实际写入表时,它说不存在这样的表。
我已经使用 SQLite 数据库浏览器验证了该表不存在。.db 文件已创建,但不执行进一步操作。这是我所有这些的代码。故障区域被标记。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace C_Sharp_SQLite_Testbed
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static string fileName;
private static string dataName;
public static string[] lines;
public MainWindow()
{
InitializeComponent();
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".txt";
dlg.Filter = "Text Documents (.txt)|*.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
//check for input in the table name textbox
if(txtTableName.GetLineLength(0) == 0)
{
string errorBoxText = "Please enter a name for the table.";
string caption = "Error";
MessageBox.Show(errorBoxText, caption, MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
//create save dialog box for .db file
Microsoft.Win32.SaveFileDialog sdg = new Microsoft.Win32.SaveFileDialog();
sdg.FileName = "databaseName";
sdg.DefaultExt = ".db";
sdg.Filter = "SQLite3 Database File (.db)|*.db";
Nullable<bool> saveResult = sdg.ShowDialog();
if (saveResult == true)
{
string fcs = string.Format("URI=file:{0}", dataName);
using (SQLiteConnection con = new SQLiteConnection(fcs))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(con))
{
//cmd.CommandText = String.Format("DROP TABLE IF EXISTS {0}", txtTableName.Text);
//Console.WriteLine(cmd.CommandText);
//cmd.ExecuteNonQuery();
Console.WriteLine(txtTableName.Text);
//Table is SUPPOSED to be built here
cmd.CommandText = String.Format(@"CREATE TABLE {0} (Timestamp INTEGER PRIMARY KEY,
LeftHipState INTEGER NOT NULL,
LeftHipX real,
LeftHipY REAL,
LeftHipZ REAL,
LeftKneeState INTEGER NOT NULL,
LeftKneeX REAL,
LeftKneeY REAL,
LeftKneeZ REAL,
LeftAnkleState INTEGER NOT NULL,
LeftAnkleX REAL,
LeftAnkleY REAL,
LeftAnkleZ REAL,
LeftFootState INTEGER NOT NULL,
LeftFootX REAL,
LeftFootY REAL,
LeftFootZ REAL,
RightHipState INTEGER NOT NULL,
RightHipX REAL,
RightHipY REAL,
RightHipZ REAL,
RightKneeState INTEGER NOT NULL,
RightKneeX REAL,
RightKneeY REAL,
RightKneeZ REAL,
RightAnkleState INTEGER NOT NULL,
RightAnkleX REAL,
RightAnkleY REAL,
RightAnkleZ REAL,
RightFootState INTEGER NOT NULL,
RightFootX REAL,
RightFootY REAL,
RightFootZ REAL,
LeftKneeFlexion REAL,
LeftKneeValgus REAL,
RightKneeFlexion REAL,
RightKneeValgus REAL)", txtTableName.Text);
Console.WriteLine(cmd.CommandText);
cmd.ExecuteNonQuery();
}
con.Close();
}
}
//for input file
fileName = dlg.FileName;
OutputConsole.Text = " ";
OutputConsole.Text = fileName;
//for output file
dataName = sdg.FileName;
var output = new List<MyData>();
try
{
//First line skipped as we do not need headers
foreach (var line in File.ReadAllLines(fileName).Skip(1))
{
output.Add(new MyData(line.Split(',')));
}
//int j = 0;
foreach (MyData outputLine in output)
{
Console.WriteLine("{0}| {1} {2:N4} {3:N4} {4:N4}| {5} {6:N4} {7:N4} {8:N4}| {9} {10:N4} {11:N4} {12:N4}| {13} {14:N4} {15:N4} {16:N4}| {17} {18:N4} {19:N4} {20:N4}| {21} {22:N4} {23:N4} {24:N4}| {25} {26:N4} {27:N4} {28:N4}| {29} {30:N4} {31:N4} {32:N4}| {33:N2} {34:N2} {35:N2} {36:N2}\n",
outputLine.Time, outputLine.HlState, outputLine.HLX, outputLine.HLY, outputLine.HLZ, outputLine.KlState, outputLine.KLX, outputLine.KLY, outputLine.KLZ, outputLine.AlState, outputLine.ALX, outputLine.ALY, outputLine.ALZ, outputLine.FlState, outputLine.FLX, outputLine.FLY, outputLine.FLZ, outputLine.HrState, outputLine.HRX, outputLine.HRY, outputLine.HRZ, outputLine.KrState, outputLine.KRX, outputLine.KRY, outputLine.KRZ, outputLine.ArState, outputLine.ARX, outputLine.ARY, outputLine.ARZ, outputLine.FrState, outputLine.FRX, outputLine.FRY, outputLine.FRZ, outputLine.LKFX, outputLine.LKVG, outputLine.RKFX, outputLine.RKVG);
//SQLite stuff--- TROUBLE HERE
string cs = string.Format("URI=file:{0}", dataName);
using (SQLiteConnection con = new SQLiteConnection(cs))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(con))
{
cmd.CommandText = String.Format("INSERT INTO {0}(Timestamp,LeftHipState,LeftHipX,LeftHipY,LeftHipZ,LeftKneeState,LeftKneeX,LeftKneeY,LeftKneeZ,LeftAnkleState,LeftAnkleX,LeftAnkleY,LeftAnkleZ,LeftFootState,LeftFootX,LeftFootY,LeftFootZ,RightHipState,RightHipX,RightHipY,RightHipZ,RightKneeState,RightKneeX,RightKneeY,RightKneeZ,RightAnkleState,RightAnkleX,RightAnkleY,RightAnkleZ,RightFootState,RightFootX,RightFootY,RightFootZ,LeftKneeFlexion,LeftKneeValgus,RightKneeFlexion,RightKneeValgus) VALUES({1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31},{32},{33},{34},{35},{36},{37})",txtTableName.Text, outputLine.Time, outputLine.HlState, outputLine.HLX, outputLine.HLY, outputLine.HLZ, outputLine.KlState, outputLine.KLX, outputLine.KLY, outputLine.KLZ, outputLine.AlState, outputLine.ALX, outputLine.ALY, outputLine.ALZ, outputLine.FlState, outputLine.FLX, outputLine.FLY, outputLine.FLZ, outputLine.HrState, outputLine.HRX, outputLine.HRY, outputLine.HRZ, outputLine.KrState, outputLine.KRX, outputLine.KRY, outputLine.KRZ, outputLine.ArState, outputLine.ARX, outputLine.ARY, outputLine.ARZ, outputLine.FrState, outputLine.FRX, outputLine.FRY, outputLine.FRZ, outputLine.LKFX, outputLine.LKVG, outputLine.RKFX, outputLine.RKVG);
cmd.ExecuteNonQuery();
}
con.Close();
}
}
/*
Console.WriteLine("{0}", output.Count);
Console.WriteLine("{0} | {1}", i, j);*/
}
catch (Exception ex)
{
OutputConsole.Text = " ";
OutputConsole.Text = ex.Message;
}
}
}
private void btnExit_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
}
private void btnInfo_Click(object sender, RoutedEventArgs e)
{
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");
DirectoryInfo info = new DirectoryInfo(".");
lock (info)
{
OutputConsole.Text = String.Format("Directory info: " + info.FullName);
}
}
}
public class MyData
{
public MyData(string[] values)
{
Time = int.Parse(values[0]);
HlState = int.Parse(values[1]);
HLX = double.Parse(values[2]);
HLY = double.Parse(values[3]);
HLZ = double.Parse(values[4]);
KlState = int.Parse(values[5]);
KLX = double.Parse(values[6]);
KLY = double.Parse(values[7]);
KLZ = double.Parse(values[8]);
AlState = int.Parse(values[9]);
ALX = double.Parse(values[10]);
ALY = double.Parse(values[11]);
ALZ = double.Parse(values[12]);
FlState = int.Parse(values[13]);
FLX = double.Parse(values[14]);
FLY = double.Parse(values[15]);
FLZ = double.Parse(values[16]);
HrState = int.Parse(values[17]);
HRX = double.Parse(values[18]);
HRY = double.Parse(values[19]);
HRZ = double.Parse(values[20]);
KrState = int.Parse(values[21]);
KRX = double.Parse(values[22]);
KRY = double.Parse(values[23]);
KRZ = double.Parse(values[24]);
ArState = int.Parse(values[25]);
ARX = double.Parse(values[26]);
ARY = double.Parse(values[27]);
ARZ = double.Parse(values[28]);
FrState = int.Parse(values[29]);
FRX = double.Parse(values[30]);
FRY = double.Parse(values[31]);
FRZ = double.Parse(values[32]);
LKFX = double.Parse(values[33]);
LKVG = double.Parse(values[34]);
RKFX = double.Parse(values[35]);
RKVG = double.Parse(values[36]);
}
public int Time { get; set; }
public int HlState { get; set; }
public double HLX { get; set; }
public double HLY { get; set; }
public double HLZ { get; set; }
public int KlState { get; set; }
public double KLX { get; set; }
public double KLY { get; set; }
public double KLZ { get; set; }
public int AlState { get; set; }
public double ALX { get; set; }
public double ALY { get; set; }
public double ALZ { get; set; }
public int FlState { get; set; }
public double FLX { get; set; }
public double FLY { get; set; }
public double FLZ { get; set; }
public int HrState { get; set; }
public double HRX { get; set; }
public double HRY { get; set; }
public double HRZ { get; set; }
public int KrState { get; set; }
public double KRX { get; set; }
public double KRY { get; set; }
public double KRZ { get; set; }
public int ArState { get; set; }
public double ARX { get; set; }
public double ARY { get; set; }
public double ARZ { get; set; }
public int FrState { get; set; }
public double FRX { get; set; }
public double FRY { get; set; }
public double FRZ { get; set; }
public double LKFX { get; set; }
public double LKVG { get; set; }
public double RKFX { get; set; }
public double RKVG { get; set; }
}
}