2013-10-29 16 views
0

BG信息: Beginer創建一個桌面應用程序,該應用程序從文本文件中的列讀取值,然後解析它們。我設法將文本文件中的每個值存儲到3個列表中(3個列表,因爲每個列表都代表文件的一部分)。 我現在計劃將這些列表值加載到一個sqlite數據庫。這就是問題的進來無法將列表值從C轉移到sqlite

Errror消息「近‘[i]’語法錯誤」發生在代碼的sqlite的一部分,當我嘗試傳送列表值。具體而言,這部分(它位於如下面的代碼段第三部分)

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(); 
    } 

回答

4

你不能通過你的變量將它們嵌入的SQL字符串內。
以這種方式,它們變成文字,不能用作整數字段的值。
您需要使用參數化查詢

sqlite_cmd.CommandText = "INSERT INTO table1 (Seq, Field, Description) VALUES (@p1, @p2, @p3)"; 
sqlite_cmd.Parameters.AddWithValue("@p1", 0); // dummy initial values 
sqlite_cmd.Parameters.AddWithValue("@p2", 0); 
sqlite_cmd.Parameters.AddWithValue("@p3", 0); 
for (int i = 0; i < 500; i+=3) 
{ 
    sqlite_cmd.Parameters["@p1"].Value = Convert.ToInt32(list[i]); 
    sqlite_cmd.Parameters["@p2"].Value = Convert.ToInt32(list[i+1]); 
    sqlite_cmd.Parameters["@p3"].Value = Convert.ToInt32(list[i+2]); // Description as Integer ?? 
    sqlite_cmd.ExecuteNonQuery(); 
} 

在這段代碼中,佔位符@ P1,P2 @ @和P3表示將通過循環內的適當的值被替換的參數。在進入循環之前,命令參數集合將使用與三個佔位符相對應的三個參數和一個虛擬初始值進行初始化。在循環內部,每個參數都會收到從列表變量中插入的實際值。

說的是,你在上面的代碼中有其他問題。

for (int j = 0; j < 10; j++) // 3 columns 

此線環10項不爲9(0至9是10種元素)

用於飼料的參數數據庫中的插入值的可變list包含字符串不是整數。但是創建表的代碼定義了三列整數數據類型。現在還不清楚你在這些列中存儲了什麼數據,因爲其中一個數據幾乎不是一個數字。

編輯從您的評論,似乎你有一個相當舊的版本的SQLite提供程序。
在這種情況下,您應該創建一個參數並將其添加到集合中。

sqlite_cmd.Parameters.Add(new SQLiteParameter("@p1", SqlDbType.Integer)); 
+0

+1打了我一拳,並提及'SqlParameters'。 – Brian

+0

@Steve你說得對,他們是字符串,我必須改變這個問題,以幫助你。 還有我得到一個錯誤說「Finsar.sqlite不包含定義.addwithvalue 所以,我要聲明一下庫是什麼? – user2788405

+0

沒有AddWithValue您應該返回到構建參數的標準方式。更新答案 – Steve