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();
}
+1打了我一拳,並提及'SqlParameters'。 – Brian
@Steve你說得對,他們是字符串,我必須改變這個問題,以幫助你。 還有我得到一個錯誤說「Finsar.sqlite不包含定義.addwithvalue 所以,我要聲明一下庫是什麼? – user2788405
沒有AddWithValue您應該返回到構建參數的標準方式。更新答案 – Steve