在C#中获取异常错误(接近“INDEX”:语法错误)

时间:2014-02-09 11:09:44

标签: c# sqlite

我是数据库中的新手。 我正在尝试创建一些表,但它失败并显示错误“接近'INDEX':语法错误”。 这是我生成表格的代码:

        try {
        using( SqliteCommand cmd = _db.CreateCommand() ) {

            // create words table
            cmd.CommandText = "DROP TABLE words";
            cmd.ExecuteNonQuery();
            cmd.CommandText = 
                "CREATE TABLE IF NOT EXISTS words(" +
                    "word_id INTEGER AUTO_INCREMENT," +
                    "word_eng VARCHAR(128)," +
                    "PRIMARY KEY(word_id)" +
                    ")";
            cmd.ExecuteNonQuery();

            // create word_classes table
            cmd.CommandText = "DROP TABLE word_classes";
            cmd.ExecuteNonQuery();
            cmd.CommandText = 
                "CREATE TABLE IF NOT EXISTS word_classes(" +
                    "word_class_id INTEGER," +
                    "word_class_name VARCHAR(50)," +
                    "PRIMARY KEY(word_class_id)" +
                    ")";
            cmd.ExecuteNonQuery();

            // create meanings table
            cmd.CommandText = "DROP TABLE meanings";
            cmd.ExecuteNonQuery();
            cmd.CommandText = 
                "CREATE TABLE IF NOT EXISTS meanings(" +
                    "meaning_id INTEGER AUTO_INCREMENT," +
                    "meaning VARCHAR(100)," +
                    "word_class_id INTEGER," +
                    "PRIMARY KEY(meaning_id)," +
                    "FOREIGN KEY(word_class_id) REFERENCES word_classes(word_class_id)" + 
                    ")";
            cmd.ExecuteNonQuery();

            // create word_meaning_realationship table
            cmd.CommandText = "DROP TABLE word_meaning_realationship";
            cmd.ExecuteNonQuery();
            cmd.CommandText =
                "CREATE TABLE IF NOT EXISTS word_meaning_realationship(" +
                    "word_id INTEGER," +
                    "meaning_id INTEGER," +
                    "FOREIGN KEY(word_id) REFERENCES words(word_id)" +
                    "FOREIGN KEY(meaning_id) REFERENCES meanings(meaning_id)" +
                    ")";
            cmd.ExecuteNonQuery();

            // create decks table
            cmd.CommandText = "DROP TABLE decks";
            cmd.ExecuteNonQuery();
            cmd.CommandText =
                "CREATE TABLE IF NOT EXISTS decks(" +
                    "deck_id INTEGER AUTO_INCREMENT," +
                    "deck_name VARCHAR(128)," +
                    "deck_version INTEGER," +
                    "PRIMARY KEY(deck_id)" +
                    ")";
            cmd.ExecuteNonQuery();

            // create deck_word_relationship table
            cmd.CommandText = "DROP TABLE deck_word_relationship";
            cmd.ExecuteNonQuery();
            cmd.CommandText =
                "CREATE TABLE IF NOT EXISTS deck_word_relationship(" +
                    "deck_id INTEGER," +
                    "word_id INTEGER," +
                    "FOREIGN KEY(deck_id) REFERENCES decks(deck_id)," +
                    "FOREIGN KEY(word_id) REFERENCES words(word_id)" +
                    ")";
            cmd.ExecuteNonQuery();
        }
    } catch( SqliteException ex ) {
        Debug.LogError( "DBError : " + ex.ToString() );
    }

如果你检查我的其他代码来创建表格,我将非常感激,因为我是数据库和mysql的新手,并且对我的代码不太自信:))

**更新** 我决定暂不考虑索引。我更新了我的代码,这些代码的错误很小,导致代码无效。但是如果你告诉我如何为我应用索引,我会在申请后将其作为正确答案。谢谢你们的帮助。

2 个答案:

答案 0 :(得分:1)

您的陈述中有不同的错误:

  • 为外键拼写单词REFERENCES。你总是用双“F”写这个,这是错的。不知道这是不是一个错字。
  • 在创建表meaning时,您错过了NOT来检查表是否存在:这应该是CREATE TABLE IF NOT EXISTS
  • 在创建表deck时,您希望在字段text上创建索引,但此字段不存在。因此,您应该在字段name或其他不同的
  • 上创建索引

修复所有这些错误后,我可以在普通的MySQL数据库上执行所有语句(没有测试你的代码,只是直接在数据库上测试语句)。

答案 1 :(得分:0)

首先尝试创建表,然后添加如下所示的索引

cmd.CommandText = @"CREATE TABLE IF NOT EXISTS word(" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "word VARCHAR(128))";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE INDEX IF NOT EXISTS idx_word_word ON word(id)";
cmd.ExecuteNonQuery();