SQLite表之间的一对多和多对多关系

时间:2016-08-07 17:26:02

标签: android sqlite

我正在尝试创建一个表,其中BasicModel Table和ExtendCategory1表具有一对多的关系。 BasicModel(一) - > ExtendCategory1(很多)。 然后ExtendCategory1表和报警表具有一对一的关系。并且报警表和报警模式表具有一对一的关系。

这个ID我的代码如下。但它不起作用。任何想法?

String basicModelCreateQuery = "CREATE TABLE BasicModel" +
                    "(_BasicModel_ID integer primary key autoincrement," +
                    "CATEGORY TEXT);";
            db.execSQL(basicModelCreateQuery);

            db.execSQL("INSERT INTO BasicModel VALUES (0, 'Category 1')");
            db.execSQL("INSERT INTO BasicModel VALUES (1, 'Category 2')");

            String extendCategory1CreateQuery = "CREATE TABLE ExtendCategory1" +
                    "(_ExtendCategory1_ID integer primary key autoincrement," +
                    "_BasicModel_ID_TO_ExtendCategory1_ID INTEGER, _BasicModel_ID_TO_ExtendCategory1_ID REFERENCES BasicModel(_BasicModel_ID)," +
                    "NAME TEXT, DESCRIPTION TEXT);";
            db.execSQL(extendCategory1CreateQuery);

            String alarmCreateQuery = "CREATE TABLE Alarm" +
                    "(_ALARM_ID integer primary key autoincrement, " +
                    "_ExtendCategory1_ID_TO_ALARM_ID INTEGER, _ExtendCategory1_ID_TO_ALARM_ID REFERENCES ExtendCategory1(_ExtendCategory1_ID)"+
                    "FROM_DATE TEXT, TO_DATE TEXT, ALWAYS, TEXT, WHEN_CREATED TEXT, " +
                    "WHEN_COMPLETE TEXT, REPEAT TEXT, SOUND TEXT, VIBRATION TEXT);";
            db.execSQL(alarmCreateQuery);

            String alarmTimingsCreateQuery = "CREATE TABLE AlarmTimings" +
                    "(_ALARM_TIMINGS_ID integer primary key autoincrement, " +
                    "_ALARM_ID_TO_ALARM_TIMINGS INTEGER, _ALARM_ID_TO_ALARM_TIMINGS REFERENCES Alarm(_ALARM_ID)"+
                    "TIME1_HOUR INTEGER, TIME1_MINUTE INTEGER, TIME2_HOUR INTEGER, TIME2_MINUTE INTEGER, TIME3_HOUR INTEGER, TIME3_MINUTE INTEGER" +
                    "SUNDAY INTEGER, MONDAY INTEGER, TUESDAY INTEGER, WEDNESDAY INTEGER, THURSDAY INTEGER, FRIDAY INTEGER, SATURDAY INTEGER);";
            db.execSQL(alarmTimingsCreateQuery);

错误表示重复的列。我对数据库知之甚少。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为这部分作为重复列被选中:

"_ALARM_ID_TO_ALARM_TIMINGS INTEGER, _ALARM_ID_TO_ALARM_TIMINGS REFERENCES Alarm(_ALARM_ID)"+

我认为应删除第二个“_ALARM_ID_TO_ALARM_TIMINGS”。