Doe Jowns
Doe Jowns

Reputation: 302

how to check if a table exists in C#

First, let me tell you I checked a bunch of the "How to check if a table exists in ...". I nevertheless need some more information about the query

SELECT name FROM sqlite_master WHERE type='table' AND name='table_name';

I supposed I had to change the names "sqlite_master" and "table_name", and here is my code

// 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();
}

and the unit test function :

[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();

}

My problem is : the command.executeReader() of the test method returns me a "null" reader, and of course i got an error when I try to do reader[0].. Did I misused the query ?

EDIT : Ok, I though I had to use the name of the file ^^. Now I changed it, but it still doesn't work (same error). I also changed the name "exemple.db" in "beispiel.db". I updated my code :)

Thank you in advance for your answers :)

Upvotes: 5

Views: 17814

Answers (4)

surfmuggle
surfmuggle

Reputation: 5942

I did the following to check if a table already exists in the database

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");
    }
}

Upvotes: 5

Romano Zumbé
Romano Zumbé

Reputation: 8079

As far as I see it, you don't read from the reader:

[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();
}

EDIT

A possible problem is the dataSource. You have to make sure, that both methods access the same location.

Upvotes: 2

vasek
vasek

Reputation: 2839

You have to query sqlite_master table:

SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'beispiel';

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151588

No, you didn't have to change the sqlite_master. That's SQLite's metadata table which contains information about all objects known to SQLite.

So your query will become:

SELECT name FROM sqlite_master WHERE type='table' AND name='beispiel';

Upvotes: 5

Related Questions