首先,让我告诉您,我查了很多“如何检查表是否存在于...”的内容。然而,我仍需要更多有关查询的信息。
SELECT name FROM sqlite_master WHERE type='table' AND name='table_name';
我想我需要更改“sqlite_master”和“table_name”的名称,这是我的代码
// a static function of the public class "SqliteBase"
public static void CreerBase(string dataSource)
{
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + dataSource;
connection.Open();
SQLiteCommand command = new SQLiteCommand(connection);
// Create table if it does not exist
command.CommandText = "CREATE TABLE IF NOT EXISTS beispiel ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(100) NOT NULL);";
Console.WriteLine("La Table a bien été créée");
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
connection.Dispose();
}
以及单元测试函数:
[TestMethod]
public void LaCreationBaseMarche()
{
string dataSource = "beispiel.db";
SqliteBase.CreerBase(dataSource);
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + dataSource;
connection.Open();
SQLiteCommand command = new SQLiteCommand(connection);
command.CommandText = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'beispiel';";
SQLiteDataReader reader = command.ExecuteReader();
Assert.Equals("beispiel", reader[0].ToString());
reader.Close();
reader.Dispose();
command.Dispose();
}
我的问题是:测试方法中的
command.executeReader()
返回了一个"null"读取器,当我尝试做reader[0]时,我当然会遇到错误。我是否误用了查询?编辑:好的,我以为我必须使用文件的名称^^。现在我已经更改了它,但它仍然不起作用(相同的错误)。我还将名称“exemple.db”更改为“beispiel.db”。我已更新我的代码 :)
提前感谢您的答复 :)