首先,让我告诉你我检查了一堆“如何检查......中是否存在表”。不过,我需要有关查询的更多信息
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] 时,我收到了错误。我是否误用了查询?
编辑:好吧,我虽然必须使用文件名^^。现在我改变了它,但它仍然不起作用(同样的错误)。我还更改了“beispiel.db”中的名称“exemple.db”。我更新了我的代码:)
提前感谢您的回答:)
不,您不必更改
sqlite_master
。这是 SQLite 的元数据表,其中包含有关 SQLite 已知的所有对象的信息。
所以你的查询将变成:
SELECT name FROM sqlite_master WHERE type='table' AND name='beispiel';
我执行了以下操作来检查数据库中是否已存在表
public static bool tableAlreadyExists(SqliteConnection openConnection, string tableName)
{
var sql =
"SELECT name FROM sqlite_master WHERE type='table' AND name='"+tableName +"';";
if(openConnection.State == System.Data.ConnectionState.Open)
{
SqliteCommand command = new SqliteCommand(sql, openConnection);
SqliteDataReader reader =command.ExecuteReader();
if(reader.HasRows)
{
reader.Close(); // @al000y: thanks mate
return true;
}
reader.Close(); // @al000y: thanks mate
return false;
}else{
throw new System.ArgumentException("Data.ConnectionState must be open");
}
}
据我所知,你没有阅读读者的内容:
[TestMethod]
public void LaCreationBaseMarche()
{
string dataSource = "exemple.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 exemple WHERE type = 'table' AND name = 'beispiel';";
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Assert.Equals("beispiel", reader[0].ToString());
}
reader.Close();
reader.Dispose();
command.Dispose();
}
编辑
一个可能的问题是数据源。您必须确保两种方法访问相同的位置。
你要查询
sqlite_master
表:
SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'beispiel';