BG info: Beginer 创建一个桌面应用程序,它从文本文件的列中读取值,然后解析它们。我设法将文本文件中的每个值存储到 3 个列表中(3 个列表,因为每个列表代表文件的一部分)。我现在计划将这些列表值加载到一个 sqlite 数据库中。这就是问题所在。
错误消息: 当我尝试传输列表值时,代码的 sqlite 部分发生“near "[i]" 语法错误” 。具体来说,这部分(位于下面代码段中的第三部分)
sqlite_cmd.CommandText = "INSERT INTO table1 (Seq, Field, Description) VALUES (list[i], list[i+1], list[i+2]);";
通往重要部分的代码:(这并不重要,只是任何人都需要它的 bg 信息,我在下面突出显示了重要的代码段)
namespace WindowsFormsApplication1
public partial class Form1 : Form
{
string[] val = new string[10];
string[] val2 = new string[6];
string[] val3 = new string[7];
string[] values = new string[1000];
List<string> list = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Reading/Inputing column values
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// Data Reading from section 1.
/*string[] lines = File.ReadAllLines(ofd.FileName).Skip(8).ToArray();
textBox1.Lines = lines;
*/
string[] fileLines = File.ReadAllLines(ofd.FileName);
// Import table values from Section 1: from Seq# to YYYY-MM
string[] section1 = fileLines.SkipWhile(line => !line.Contains("Seq#")).TakeWhile(line => !line.Contains("Total Records")).Where(line => Regex.IsMatch(line, @"[\s|\d]{4}\d")).Select(line => line.PadRight(198)).ToArray();
int[] pos = new int[10] { 0, 6, 18, 47, 53,57, 61, 68, 82, 97 }; //setlen&pos to read specific colmn vals
int[] len = new int[10] { 6, 12, 29, 6 , 4, 4, 7, 14, 15, 6 }; // only doing 3 columns right now
只需使用列值填充列表:(这部分工作正常)
foreach (string line in section1)
{
//if (line.StartsWith("0")) break;
for (int j = 0; j < 10; j++) // 3 columns
{
val[j] = line.Substring(pos[j], len[j]).Trim(); // each column value in row add to array
list.Add(val[j]); // column values stored in list
}
}
重要部分:SQLite 连接(此处发生错误)**
private void button4_Click(object sender, EventArgs e)
{
// [snip] - As C# is purely object-oriented the following lines must be put into a class:
// We use these three SQLite objects:
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
// create a new database connection:
sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
// open the connection:
sqlite_conn.Open();
// create a new SQL command:
sqlite_cmd = sqlite_conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE table1 (Seq integer primary key, Field integer , Description integer );";
// Now lets execute the SQL ;D
sqlite_cmd.ExecuteNonQuery();
for (int i = 0; i < 500; i+=3)
{
// Lets insert something into our new table:
sqlite_cmd.CommandText = "INSERT INTO table1 (Seq, Field, Description) VALUES (list[i], list[i+1], list[i+2]);"; THIS IS WHERE PROBLEM OCCURS!
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
}
// We are ready, now lets cleanup and close our connection:
sqlite_conn.Close();
}