SQLITE使用FOREIGN键和Composite键创建表

时间:2014-10-23 12:20:33

标签: android database android-sqlite

我是Android sqlite数据库的新手,所以我无法弄清楚用主键和外键编写这个create语句的正确语法是什么。下面我将通过我的列名并创建表语句。我希望有人能指导我在这方面做错了什么。

//table names
public static final String TABLE_configuracao_situacao = "configuracao_situacao";
public static final String TABLE_situacao = "situacao";

//column names for Situation table
public static final String KEY_idSituacao = "id_situacao";
public static final String KEY_descicao = "descricao";
public static final String KEY_padrao = "padrao";
public static final String KEY_button = "button";

//column names for configuracao_situacao table
public static final String KEY_situacao = "situacao";
public static final String KEY_configuracao = "configuracao";
public static final String KEY_minimo = "minimo";
public static final String KEY_maximo = "maximo";
public static final String KEY_acaoMin = "acao_min";
public static final String KEY_acaoMax = "acao_max";
public static final String KEY_dataCriacao = "data_criacao";

//Table created statement for situation
public static final String DATABASE_CREATE_SITUACAO =
        "CREATE TABLE " + TABLE_situacao 
        + "("   +   KEY_idSituacao  + " integer primary key autoincrement, "
                +   KEY_descicao    + " string , "
                +   KEY_padrao      + " string, "
                +   KEY_button      + " integer "
        + ");";

//Table created statement configuracao_situacao
public static final String DATABASE_CREATE_Table_CONFIGSITUACAO =
        "CREATE TABLE " +   TABLE_configuracao_situacao
        + "("   +   KEY_situacao        + " integer primary key, "
                +   KEY_configuracao    + " integer primary key, "
                +   KEY_minimo          + " text, "
                +   KEY_maximo          + " text, "
                +   KEY_acaoMin         + " text, "
                +   KEY_acaoMax         + " text, "
                +   KEY_dataCriacao     + " DATETIME, "
                "FOREIGN KEY (" + KEY_situacao + " ) REFERENCES " +  TABLE_situacao + "(" + KEY_idSituacao + ")"
        + ");";

问题在于configuracao_situacao创建语句,同时制作foregin键。有谁能告诉我如何在这里定义密钥。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

您使用了错误的数据类型 检查下面的代码,您将获得参考

//Table created statement for situation
public static final String DATABASE_CREATE_SITUACAO =
    "CREATE TABLE " + TABLE_situacao 
    + "("   +   KEY_idSituacao  + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            +   KEY_descicao    + " TEXT, "
            +   KEY_padrao      + " TEXT, "
            +   KEY_button      + " INTEGER"
    + ");";
//Table created statement configuracao_situacao
public static final String DATABASE_CREATE_Table_CONFIGSITUACAO =
    "CREATE TABLE " +   TABLE_configuracao_situacao
    + "("   +   KEY_situacao        + " INTEGER PRIMARY KEY, "
            +   KEY_configuracao    + " INTEGER PRIMARY KEY, "
            +   KEY_minimo          + " TEXT,"
            +   KEY_maximo          + " TEXT,"
            +   KEY_acaoMin         + " TEXT,"
            +   KEY_acaoMax         + " TEXT,"
            +   KEY_dataCriacao     + " TEXT,"
            "FOREIGN KEY (" + KEY_situacao + " ) REFERENCES " +  TABLE_situacao + "(" + KEY_idSituacao + ")"
    + ");";

答案 1 :(得分:0)

也许你在DATETIME之后有额外的逗号:

+ KEY_dataCriacao +“DATETIME,”

而SQLite没有类型字段DATETIME和string

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