【问题标题】:android foreign key. program crashes安卓外键。程序崩溃
【发布时间】:2013-02-21 00:18:51
【问题描述】:
public static final String TABLE_TASKS = "tasks";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TASK = "task";
public static final String COLUMN_TIME = "time";
public static final String COLUMN_INITDATE = "initdate";
public static final String COLUMN_PRIORITY="priority";

public static final String TABLE_REPEAT = "alarms";
public static final String COLUMN_AID = "_aid";
public static final String COLUMN_TID = "tid";

public static final String COLUMN_DAYS="days";

private static final String DATABASE1_CREATE = "create table " + TABLE_TASKS + "("
+ COLUMN_ID + " integer primary key autoincrement, "+ COLUMN_TASK
+ " text not null, "+ COLUMN_TIME+" integer not null, "+ COLUMN_INITDATE+" integer not null, "+ COLUMN_PRIORITY +" integer );";

private static final String DATABASE2_CREATE = "create table " + TABLE_REPEAT + "("
        + COLUMN_AID + " integer primary key autoincrement, "+  " FOREIGN KEY (" + COLUMN_TID + ") REFERENCES " + TABLE_TASKS + " ( " + COLUMN_ID + " ), "+ COLUMN_DAYS + " text not null );";

我的 logcat 是: 03-06 09:17:07.051: E/AndroidRuntime(1029): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.habitator/com.example.habitator.Habitator}: android.database.sqlite.SQLiteException :外键定义中的未知列“tid”(代码1):,编译时:创建表警报(_aid整数主键自动增量,外键(tid)参考任务(_id),天文本不为空);

请帮我找出错误!为什么我的程序崩溃?

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个查询。

    create table alarms(_aid integer primary key autoincrement, tid integer FOREIGNKEY REFERENCES tasks ( _id ), days text not null );
    

    https://www.sqlite.org/foreignkeys.html

    【讨论】:

    • 我也做错了。我使用了 FOREIGN KEY (payment_method) REFERENCES payment_method(id) 但我得到一个错误“外键定义中的未知列 payment_method (code 1):” .... 我最终使用了 payment_method INTEGER FOREIGNKEY REFERENCES payment_method(id) 和效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    相关资源
    最近更新 更多