2013-11-01 35 views
0

所以我的错误是:表chang9已经存在。如何删除sqlite表后,它已经存在于c#

我用Google搜索周围,人们说你运行后删除表,因此,我使用(没有运气):

sqlite_cmd.CommandText = " DROP Table 'chang9'"; 

也许有一个语法问题呢?但这里是我的全部代码:(注:DROP TABLE部分是在代码末尾)

private void button4_Click(object sender, EventArgs e) 
    { 
     // We use these three SQLite objects: 
     SQLiteConnection sqlite_conn; 
     SQLiteCommand sqlite_cmd; 

     // create a new database connection: // Maybe error here - video was different 
     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 chang9 (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_cmd.CommandText = "INSERT INTO chang9 (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 < 500 ; 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(); 

     } 

     sqlite_cmd.CommandText = " DROP Table 'chang9'"; 
     sqlite_conn.Close(); 

    } 

回答

1

你的文字设置为您的DROP TABLE语句后,您不执行你的命令

+0

嘿,请参阅我发布到bunyip答案的注释。我如何简单地查看这个表后创建它没有得到表已经存在的错误? 我只是想多次运行该程序,并能够一遍又一遍地查看同一张表。 – user2788405

1

你的问题是在这里:

sqlite_cmd.CommandText = "CREATE TABLE chang9 (Seq text, Field text, Desc text, Len text, Dec text, Typ text, Percnt text, Pop text, Alzero text, MaxLen text);"; 

创造它已经存在的表时,这样你不能再创建它。

你假定当你启动这段代码时表格不存在,但它已经存在。下面的命令

drop table if exists Table_Name 
1

用途:

sqlite_cmd.CommandText = " DROP Table 'chang9'"; 
sqlite_conn.Close(); 

这样的表永远不会被丢弃。当您尝试再次运行该方法时,表格已经存在,并且您看到了错误。设置命令文本以删除表格后,您需要添加一行如sqlite_cmd.ExecuteNonQuery();

sqlite_cmd.CommandText = " DROP Table 'chang9'"; 
sqlite_conn.Close(); 
sqlite_cmd.ExecuteNonQuery(); 
+0

表存在(因此错误)。 – Mike

+0

是的,如果我们使用上面的命令,它会删除已经存在的表。我认为这是问题的意图。 – SKJ

+0

如果我们假设这个表存在,那么添加'if exists table_Name'就等于'if true',它会一直通过并且可以被关闭。如果表格不存在,错误将会不同。 – Mike

相关问题