C#SQLite表存在且不存在

时间:2017-08-28 03:08:06

标签: c# sqlite

    private void StartMenu_Load(object sender, EventArgs e)
    {
        this.CenterToScreen();

        if (!File.Exists("schedules.db"))
        {
            SQLiteConnection.CreateFile("schedules.db");
            MessageBox.Show("Created schedules.db");
        }

        SQLCon = CreateSQLiteConnection();

        SQLCon.Open();

        using (SQLiteCommand cmd = new SQLiteCommand("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='schedule_ids'", SQLCon))
        {
            using (SQLiteDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    using (SQLiteCommand loadSchedules = new SQLiteCommand("SELECT name FROM schedule_ids", SQLCon))
                    {
                        reader.Close();

                        using (SQLiteDataReader reader1 = loadSchedules.ExecuteReader())
                        {
                            while (reader1.Read())
                            {
                                MessageBox.Show("?");
                                ScheduleList.Items.Add((string)reader1[0]);
                            }

                            MessageBox.Show("$");
                        }
                    }
                }
                else
                {
                    reader.Close();

                    using (SQLiteCommand createTable = new SQLiteCommand("CREATE TABLE schedules_ids (name nvarchar(45), schedule_id int)", SQLCon))
                    {
                        createTable.ExecuteNonQuery();
                    }
                }
            }
        }

    }

    private SQLiteConnection CreateSQLiteConnection()
    {
        string path = AppDomain.CurrentDomain.BaseDirectory;
        //path = path.Replace(@"bin\Debug\", "");

        SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
        builder.FailIfMissing = true;
        builder.DataSource = Path.Combine(path, "schedules.db");

        return new SQLiteConnection(builder.ConnectionString);
    }

当我运行此(WinForms应用程序)时,它会创建schedules.db,但后来我收到错误“SQL逻辑错误或缺少数据库没有这样的表:schedule_ids”。奇怪的是,如果我从

改变它
if (reader.Read())

if (!reader.Read())

然后我收到错误“SQL逻辑错误或缺少数据库表schedule_ids已存在”。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

尝试一点重构:

// re-usable variable to avoid typos...
private const string _schedule_Id_TableName = "schedule_ids";

private void StartMenu_Load(object sender, EventArgs e)
{
    this.CenterToScreen();

    if (!File.Exists("schedules.db"))
    {
        SQLiteConnection.CreateFile("schedules.db");
        MessageBox.Show("Created schedules.db");
    }

    SQLCon = CreateSQLiteConnection();

    SQLCon.Open();

    using (SQLiteCommand cmd = new SQLiteCommand(string.Format("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='{0}'", _schedule_Id_TableName), SQLCon))
    {
        using (SQLiteDataReader reader = cmd.ExecuteReader())
        {
            if (reader.Read())
            {
                using (SQLiteCommand loadSchedules = new SQLiteCommand(string.Format("SELECT name FROM {0}", _schedule_Id_TableName), SQLCon))
                {
                    reader.Close();

                    using (SQLiteDataReader reader1 = loadSchedules.ExecuteReader())
                    {
                        while (reader1.Read())
                        {
                            MessageBox.Show("?");
                            ScheduleList.Items.Add((string)reader1[0]);
                        }

                        MessageBox.Show("$");
                    }
                }
            }
            else
            {
                reader.Close();

                // Only create the table if it doesn't already exists...
                using (SQLiteCommand createTable = new SQLiteCommand(string.Format("CREATE TABLE IF NOT EXISTS {0}(name nvarchar(45), schedule_id int)", _schedule_Id_TableName), SQLCon))
                {
                    createTable.ExecuteNonQuery();
                }
            }
        }
    }

}

private SQLiteConnection CreateSQLiteConnection()
{
    string path = AppDomain.CurrentDomain.BaseDirectory;
    //path = path.Replace(@"bin\Debug\", "");

    SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
    builder.FailIfMissing = true;
    builder.DataSource = Path.Combine(path, "schedules.db");

    return new SQLiteConnection(builder.ConnectionString);
}