[英]how to check if a table exists in C#
首先,让我告诉你我检查了一堆“如何检查表是否存在于......”。 尽管如此,我仍需要有关查询的更多信息
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()
返回一个“空”读取器,当然,当我尝试执行 reader[0] 时出现错误。我是否误用了查询?
编辑:好的,虽然我必须使用文件名 ^^。 现在我改变了它,但它仍然不起作用(同样的错误)。 我还在“beispiel.db”中更改了名称“exemple.db”。 我更新了我的代码:)
预先感谢您的回答:)
不,您不必更改sqlite_master
。 那是 SQLite 的元数据表,其中包含有关 SQLite 已知的所有对象的信息。
所以你的查询将变成:
SELECT name FROM sqlite_master WHERE type='table' AND name='beispiel';
在我看来,你不会从读者那里读到:
[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();
}
编辑
一个可能的问题是数据源。 您必须确保这两种方法都访问相同的位置。
我做了以下检查数据库中是否已经存在一个表
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)
{
return true;
}
return false;
}else{
throw new System.ArgumentException("Data.ConnectionState must be open");
}
}
您必须查询sqlite_master
表:
SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'beispiel';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.