首先,让我告诉你我检查了一堆“如何检查......中是否存在表”。不过,我需要有关查询的更多信息
\n\nSELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'table_name\';\nRun Code Online (Sandbox Code Playgroud)\n\n我想我必须更改名称“sqlite_master”和“table_name”,这是我的代码
\n\n// a static function of the public class "SqliteBase"\npublic static void CreerBase(string dataSource)\n{\n SQLiteConnection connection = new SQLiteConnection();\n\n connection.ConnectionString = "Data Source=" + dataSource;\n connection.Open();\n SQLiteCommand command = new SQLiteCommand(connection);\n\n // Create table if it does not exist\n command.CommandText = "CREATE TABLE IF NOT EXISTS beispiel ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(100) NOT NULL);";\n Console.WriteLine("La Table a bien \xc3\xa9t\xc3\xa9 cr\xc3\xa9\xc3\xa9e");\n command.ExecuteNonQuery();\n command.Dispose();\n connection.Close();\n connection.Dispose();\n}\nRun Code Online (Sandbox Code Playgroud)\n\n和单元测试功能:
\n\n[TestMethod] \npublic void LaCreationBaseMarche()\n{\n string dataSource = "beispiel.db";\n SqliteBase.CreerBase(dataSource);\n SQLiteConnection connection = new SQLiteConnection();\n\n connection.ConnectionString = "Data Source=" + dataSource;\n connection.Open();\n SQLiteCommand command = new SQLiteCommand(connection);\n command.CommandText = "SELECT name FROM sqlite_master WHERE type = \'table\' AND name = \'beispiel\';";\n SQLiteDataReader reader = command.ExecuteReader();\n Assert.Equals("beispiel", reader[0].ToString());\n reader.Close();\n reader.Dispose();\n\n command.Dispose();\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我的问题是:command.executeReader()测试方法的返回给我一个“null”阅读器,当然,当我尝试执行 reader[0] 时,我收到了错误。我是否误用了查询?
编辑:好的,我虽然必须使用文件名^^。现在我改变了它,但它仍然不起作用(同样的错误)。我还更改了“beispiel.db”中的名称“exemple.db”。我更新了我的代码:)
\n\n预先感谢您的回答:)
\n不,您不必更改sqlite_master. 这是 SQLite 的元数据表,其中包含有关 SQLite 已知的所有对象的信息。
所以你的查询将变成:
SELECT name FROM sqlite_master WHERE type='table' AND name='beispiel';
Run Code Online (Sandbox Code Playgroud)
我执行了以下操作来检查数据库中是否已存在表
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");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16167 次 |
| 最近记录: |