c#/。NET SQLite - REINDEX无法正常工作?

时间:2010-03-11 10:29:37

标签: c# .net sql sqlite system.data.sqlite

我正在尝试在使用SQLite.NET和VS2008创建的简单数据库中重新索引表。我需要在每个DELETE命令之后重新索引表,这里是我编写的代码片段(它不起作用):

SQLiteCommand currentCommand;
String tempString = "REINDEX tf_questions";
//String tempString = "REINDEX [main].tf_questions";
//String tempString = "REINDEX main.tf_questions";

currentCommand = new SQLiteCommand(myConnection);
currentCommand.CommandText = tempString;
currentCommand.ExecuteNonQuery()

在我的程序中运行时,代码不会产生错误,但它也不会重新索引“tf_questions”表。在上面的示例中,您还将看到我尝试过的其他查询字符串也无效。

请帮忙,

由于

2 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法。请考虑以下代码:

SQLiteCommand currentCommand;
String tempString;
String currentTableName = "tf_questions";
DataTable currentDataTable;
SQLiteDataAdapter myDataAdapter;

currentDataTable = new DataTable();
myDataAdapter = new SQLiteDataAdapter("SELECT * FROM " + currentTableName, myConnection);
myDataAdapter.Fill(currentDataTable);


//"tf_questions" is the name of the table
//"question_id" is the name of the primary key column in "tf_questions" (set to auto inc.)
//"myConnection" is and already open SQLiteConnection pointing to the db file

for (int i = currentDataTable.Rows.Count-1; i >=0 ; i--)
{
    currentCommand = new SQLiteCommand(myConnection);
    tempString = "UPDATE "+ currentTableName +"\nSET question_id=\'"+(i+1)+"\'\nWHERE (question_id=\'" +
        currentDataTable.Rows[i][currentDataTable.Columns.IndexOf("question_id")]+"\')";
    currentCommand.CommandText = tempString;

    if( currentCommand.ExecuteNonQuery() < 1 )
    {
        throw new Exception("There was an error executing the REINDEX protion of the code...");
    }
}

此代码有效,但我本来希望使用内置的SQL REINDEX命令。

答案 1 :(得分:0)

如果您出于性能考虑,请考虑answer

  

REINDEX无济于事。 REINDEX只是   如果您更改整理,则需要   序列

     

经过大量的INSERT和DELETE后,   你有时会变得更好   通过做VACUUM来表现。真空   改善参考的局部性   略。