プログラム出力を CSV から SQLite に変換するという話を続けていると、別の問題に遭遇しました。テキスト ファイルを行とコンマで分割し、その結果をリストに入れています。以前の方法ではテーブルが正しく作成されなかったため、ADO.NET DataSet および .NET データ プロバイダーを使用してテーブルにデータを挿入する方法について、このチュートリアルに従っています。
ファイルからのライン リーダーは、134 行を読み取ったことを返します。これは、ファイル内の正しい行数です (ちなみに、ここで見つけることができます)。... テストに使用することでメリットが得られます)。さらに、データをデータ アダプターにロードするために作成された DataTable オブジェクトも、合計 134 行を読み取ります。ただし、ファイルを開いてその内容を確認するときが来たら (これは wxSQLite+ を使用して行っています)、すべての行がそこにあるわけではありません。書き込まれる行数はさまざまですが、必然的に 134 ではありません。さらに、行全体に適用されるデータ型は均一ではありません。奇数行のデータ フィールドは、テーブルの作成時に入力したスキーマに忠実なままですが、偶数行ではすべての整数型の行が double に変換されます。何が原因なのかよくわからず、チュートリアルのコードから少し逸脱していますが、原則は同じはずです。それは単純でばかげたものかもしれません(私は素晴らしいですしかし、私の博士課程の学生の指導教官は、実装に明らかな問題は見当たりませんでした。
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){
//open input file
string dateTime = DateTime.Today.ToString("dd_mm_yyyy");
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();
string inputFilePath = dlg.FileName;
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
//create output 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 saveFileName = sdg.FileName;
string cs = string.Format("URI=file:{0}", saveFileName);
string tableName = txtTableName.Text;
int i = 0;
#region SQLite logic
#region create table
using (SQLiteConnection con = new SQLiteConnection(cs)){
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(con)){
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();
}
#endregion
using (SQLiteConnection con = new SQLiteConnection(cs)){
DataTable table = new DataTable(tableName);
#region create columns
table.Columns.Add("Timestamp", System.Type.GetType("System.String"));
//------------------------------------------------------------------
table.Columns.Add("LeftHipState", System.Type.GetType("System.Int32"));
table.Columns.Add("LeftHipX", System.Type.GetType("System.Double"));
table.Columns.Add("LeftHipY", System.Type.GetType("System.Double"));
table.Columns.Add("LeftHipZ", System.Type.GetType("System.Double"));
table.Columns.Add("LeftKneeState", System.Type.GetType("System.Int32"));
table.Columns.Add("LeftKneeX", System.Type.GetType("System.Double"));
table.Columns.Add("LeftKneeY", System.Type.GetType("System.Double"));
table.Columns.Add("LeftKneeZ", System.Type.GetType("System.Double"));
table.Columns.Add("LeftAnkleState", System.Type.GetType("System.Int32"));
table.Columns.Add("LeftAnkleX", System.Type.GetType("System.Double"));
table.Columns.Add("LeftAnkleY", System.Type.GetType("System.Double"));
table.Columns.Add("LeftAnkleZ", System.Type.GetType("System.Double"));
table.Columns.Add("LeftFootState", System.Type.GetType("System.Int32"));
table.Columns.Add("LeftFootX", System.Type.GetType("System.Double"));
table.Columns.Add("LeftFootY", System.Type.GetType("System.Double"));
table.Columns.Add("LeftFootZ", System.Type.GetType("System.Double"));
//-------------------------------------------------------------------
table.Columns.Add("RightHipState", System.Type.GetType("System.Int32"));
table.Columns.Add("RightHipX", System.Type.GetType("System.Double"));
table.Columns.Add("RightHipY", System.Type.GetType("System.Double"));
table.Columns.Add("RightHipZ", System.Type.GetType("System.Double"));
table.Columns.Add("RightKneeState", System.Type.GetType("System.Int32"));
table.Columns.Add("RightKneeX", System.Type.GetType("System.Double"));
table.Columns.Add("RightKneeY", System.Type.GetType("System.Double"));
table.Columns.Add("RightKneeZ", System.Type.GetType("System.Double"));
table.Columns.Add("RightAnkleState", System.Type.GetType("System.Int32"));
table.Columns.Add("RightAnkleX", System.Type.GetType("System.Double"));
table.Columns.Add("RightAnkleY", System.Type.GetType("System.Double"));
table.Columns.Add("RightAnkleZ", System.Type.GetType("System.Double"));
table.Columns.Add("RightFootState", System.Type.GetType("System.Int32"));
table.Columns.Add("RightFootX", System.Type.GetType("System.Double"));
table.Columns.Add("RightFootY", System.Type.GetType("System.Double"));
table.Columns.Add("RightFootZ", System.Type.GetType("System.Double"));
//-------------------------------------------------------------------
table.Columns.Add("LeftKneeFlexion", System.Type.GetType("System.Double"));
table.Columns.Add("LeftKneeValgus", System.Type.GetType("System.Double"));
table.Columns.Add("RightKneeFlexion", System.Type.GetType("System.Double"));
table.Columns.Add("RightKneeValgus", System.Type.GetType("System.Double"));
#endregion
var output = new List<MyData>();
foreach (var line in File.ReadAllLines(inputFilePath)){
output.Add(new MyData(line.Split(',')));
}
foreach (MyData outputLine in output){
DataRow row = table.NewRow();
row["Timestamp"] = outputLine.Time;
row["LeftHipState"] = outputLine.HlState;
row["LeftHipX"] = outputLine.HLX;
row["LeftHipY"] = outputLine.HLY;
row["LeftHipZ"] = outputLine.HLZ;
row["LeftKneeState"] = outputLine.KlState;
row["LeftKneeX"] = outputLine.KLX;
row["LeftKneeY"] = outputLine.KLY;
row["LeftKneeZ"] = outputLine.KLZ;
row["LeftAnkleState"] = outputLine.AlState;
row["LeftAnkleX"] = outputLine.ALX;
row["LeftAnkleY"] = outputLine.ALY;
row["LeftAnkleZ"] = outputLine.ALZ;
row["LeftFootState"] = outputLine.FlState;
row["LeftFootX"] = outputLine.FLX;
row["LeftFootY"] = outputLine.FLY;
row["LeftFootZ"] = outputLine.FLZ;
row["RightHipState"] = outputLine.HrState;
row["RightHipX"] = outputLine.HRX;
row["RightHipY"] = outputLine.HRY;
row["RightHipZ"] = outputLine.HRZ;
row["RightKneeState"] = outputLine.KrState;
row["RightKneeX"] = outputLine.KRX;
row["RightKneeY"] = outputLine.KRY;
row["RightKneeZ"] = outputLine.KRZ;
row["RightAnkleState"] = outputLine.ArState;
row["RightAnkleX"] = outputLine.ARX;
row["RightAnkleY"] = outputLine.ARY;
row["RightAnkleZ"] = outputLine.ARZ;
row["RightFootState"] = outputLine.FrState;
row["RightFootX"] = outputLine.FRX;
row["RightFootY"] = outputLine.FRY;
row["RightFootZ"] = outputLine.FRZ;
row["LeftKneeFlexion"] = outputLine.LKFX;
row["LeftKneeValgus"] = outputLine.LKVG;
row["RightKneeFlexion"] = outputLine.RKFX;
row["RightKneeValgus"] = outputLine.RKVG;
table.Rows.Add(row);
i++;
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);
}
Console.WriteLine("{0}, {1}", i, table.Rows.Count);
string sql = string.Format("SELECT * FROM {0}", tableName);
using (SQLiteDataAdapter da = new SQLiteDataAdapter(sql, con)){
using (new SQLiteCommandBuilder(da)){
da.Update(table);
}
}
con.Close();
#endregion
}
}
#region
//for input file
//fileName = dlg.FileName;
//OutputConsole.Text = " ";
//OutputConsole.Text = fileName;
//for output file
//dataName = sdg.FileName;
#endregion
}
}
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; }
}
}
それは微妙なものですか、それとも実際に間違っているものですか?行数の問題とシフトするデータ型の両方を修正する必要があります。
の XAML コードは次のMainWindow
とおりです。再現する必要はありません。
<Window x:Class="C_Sharp_SQLite_Testbed.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Background="LightGray">
<Button x:Name="btnLoad" Content="Load CSV File to Database" HorizontalAlignment="Left" Margin="10,258,0,0" VerticalAlignment="Top" Width="153" Click="btnLoad_Click"/>
<Button x:Name="btnExit" Content="Exit" HorizontalAlignment="Left" Margin="312,258,0,0" VerticalAlignment="Top" Width="75" Click="btnExit_Click"/>
<TextBlock x:Name="OutputConsole" HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="243" Width="497" Background="Black" Foreground="#FF00E800" FontFamily="Lucida Console"/>
<Button x:Name="btnInfo" Content="Directory Information" HorizontalAlignment="Left" Margin="168,258,0,0" VerticalAlignment="Top" Width="139" Click="btnInfo_Click"/>
<TextBox x:Name="txtTableName" HorizontalAlignment="Left" Height="23" Margin="200,285,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
<Label Content="Enter name for table in database:" HorizontalAlignment="Left" Margin="10,282,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
プロジェクトの種類はコンソール プロジェクトなので、新しい WPF プロジェクトを開いた後に設定する必要があります。
これに付随するばかげた質問かもしれません: これらのファイルの 1 つを書き込んでから開くのを待つ時間は、まったく問題になるでしょうか?