2

我正在尝试在使用 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”表。在上面的示例中,您还将看到我尝试过的其他查询字符串也不起作用。

请帮忙,

谢谢

4

2 回答 2

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 命令。

于 2010-03-11T21:22:58.700 回答
0

如果您为了性能而这样做,请考虑以下答案

REINDEX 没有帮助。仅当您更改整理顺序时才需要 REINDEX。

在进行了很多 INSERT 和 DELETE 之后,您有时可以通过 VACUUM 获得稍好的性能。VACUUM 略微提高了参考的局部性。

于 2010-04-14T21:07:49.763 回答