0

我的 Sqlite 表应该有 50 行。当我第一次运行该程序并通过 firefox 查看器检查表格时,我看到了 50 行(应该是这样)。

当我第二次运行该程序并检查表格时,它的行数是双倍的——也就是说,它现在有 100 行。(这就是问题)

我有使用 DELETE 的建议,但我不想删除表,因为我想在运行程序进行调试后在 firefox 查看器中看到它。

 // 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;");

          // 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 IF NOT EXISTS 'tab1' (Seq text, Field text, Desc text, Len text, Dec text, Typ text, Percnt text, Pop text, Alzero text, MaxLen text );";

          // Now lets execute the SQL                                                                                 
          sqlite_cmd.ExecuteNonQuery();

          // **** SQLITE TRANSFER SECTION 1 - transfer values from list to table *****

          sqlite_cmd.CommandText = "INSERT INTO tab1 (Seq, Field, Desc, Len, Dec, Typ, Percnt, Pop, Alzero, MaxLen) VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)";
          sqlite_cmd.Parameters.AddWithValue("@p1", 6);  // dummy initial values 
          sqlite_cmd.Parameters.AddWithValue("@p2", 878); 
          sqlite_cmd.Parameters.AddWithValue("@p3", 56);
          sqlite_cmd.Parameters.AddWithValue("@p4", 6);
          sqlite_cmd.Parameters.AddWithValue("@p5", 546);
          sqlite_cmd.Parameters.AddWithValue("@p6", 565);
          sqlite_cmd.Parameters.AddWithValue("@p7", 568);
          sqlite_cmd.Parameters.AddWithValue("@p8", 526);
          sqlite_cmd.Parameters.AddWithValue("@p9", 586);
          sqlite_cmd.Parameters.AddWithValue("@p10", 526);


          for (int i = 0; i < NumListValues; i += 10) // Filling SQlite table rows and columns with values from our list 
          {

              sqlite_cmd.Parameters.AddWithValue("@p1", list[i]);
              sqlite_cmd.Parameters.AddWithValue("@p2", list[i+1]);
              sqlite_cmd.Parameters.AddWithValue("@p3", list[i+2]);
              sqlite_cmd.Parameters.AddWithValue("@p4", list[i+3]);
              sqlite_cmd.Parameters.AddWithValue("@p5", list[i+4]);
              if (i > 490)
                  break; 
              sqlite_cmd.Parameters.AddWithValue("@p6", list[i+5]);
              sqlite_cmd.Parameters.AddWithValue("@p7", list[i+6]);
              sqlite_cmd.Parameters.AddWithValue("@p8", list[i+7]);
              sqlite_cmd.Parameters.AddWithValue("@p9", list[i+8]);
              sqlite_cmd.Parameters.AddWithValue("@p10", list[i+9]);
              sqlite_cmd.ExecuteNonQuery();

          }
4

2 回答 2

1

您的脚本在您第一次调用它时创建表,并插入记录。

第二次调用它时,您并没有重新创建表,因为您使用了该CREATE TABLE IF NOT EXISTS子句。但是,您仍在向表中插入记录!

如果您第三次调用您的脚本,您将有 150 行。

于 2013-11-05T16:19:25.483 回答
1

当我第二次运行该程序并检查表格时,它的行数是双倍的——也就是说,它现在有 100 行。

嗯,是的,它会的。您说过如果表不存在则创建表,然后插入一堆数据,而不删除已经存在的数据。

它实际上不会每次都加倍 - 它只会在NumListValues / 10您每次运行时添加行,因为这就是您所说的想要做的。

如果您想在开始插入之前从表中删除任何现有行(即在运行开始时而不是在结束时),那么您需要明确地这样做。您可以使用TRUNCATE TABLEDELETE FROM ...

于 2013-11-05T16:19:55.107 回答