1

所以我的错误是:表 chang9 已经存在。

我四处搜索,人们说在你运行它后放下桌子,因此我使用了(没有运气):

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

    }
4

3 回答 3

4

将文本设置为 drop table 语句后,您没有执行命令:

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

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

sqlite_cmd.CommandText = " DROP Table 'chang9'";
sqlite_conn.Close();
sqlite_cmd.ExecuteNonQuery();
于 2013-11-01T18:13:41.497 回答
2

你的问题在这里:

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

创建表时它已经存在,因此您无法再次创建它。

您假设启动此代码时该表不存在,但它已经存在。

于 2013-11-01T17:51:37.017 回答
1

使用以下命令

drop table if exists Table_Name
于 2013-11-01T17:52:04.870 回答