如何在C#中检查表是否存在

4

首先,让我告诉您,我查了很多“如何检查表是否存在于...”的内容。然而,我仍需要更多有关查询的信息。

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”。我已更新我的代码 :)
提前感谢您的答复 :)
4个回答

5
不,您不必更改sqlite_master。这是SQLite的元数据表,包含有关SQLite已知的所有对象的信息。
因此,您的查询将变为:
SELECT name FROM sqlite_master WHERE type='table' AND name='beispiel';

好的,我以为我必须使用文件名^^。现在我改了它,但它仍然不起作用(相同的错误)。 - Doe Jowns

4

我执行以下操作以检查数据库中是否已存在表格

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数据库的内存版本进行测试时也非常有用。 - Trevor
非常有用的方法,我建议将其编写为扩展方法: public static bool ExistsTable(this SQLiteConnection con, string tableName) { ... } - Paul Efford
一个非常好的解决方案 - j.hull

2
据我所见,您不从读卡器中读取数据。
[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();
}

编辑

可能存在的问题是数据源。您需要确保两种方法访问同一位置。


1
方法CreerBase可用且表已创建? - Romano Zumbé
好的,好问题。实际上,这就是我想要测试的东西。你怎么能找出来呢?如果存在的话,它应该在哪里?(我正在使用Visual Studio 2015) - Doe Jowns
我现在能想到的最简单的解决方案是:将您的查询更改为“SELECT name FROM exemple WHERE type ='table'”,然后调试循环。查看所有结果。 - Romano Zumbé
如果您按照我的建议更改了查询,我认为您的数据库连接存在问题。它应该至少返回一些内容。 - Romano Zumbé
我想表达的是 "SELECT name FROM sqlite_master WHERE type='table'"。 - Romano Zumbé
显示剩余4条评论

0
你需要查询 sqlite_master 表: SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'beispiel';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接