8

你能帮我了解一下sqlite中两个表之间的关系吗?

我做了插入、删除和更新步骤,但我现在必须支持两个表之间的关系。我猜
之前完成的所有代码步骤都会被更改对吗?您是否有任何链接或示例来解释表关系和关系后的任何活动?

4

3 回答 3

20

要在两个表之间建立关系,可以使用外键。外键是关系表中与另一个表的候选键匹配的字段。

例如,假设我们有两个表,一个包含所有客户数据的 CUSTOMER 表和一个包含所有客户订单的 ORDER 表。此处的目的是所有订单都必须与已在 CUSTOMER 表中的客户相关联。为此,我们将在 ORDER 表中放置一个外键,并使其与 CUSTOMER 表的主键相关。

在SQLite中,可以通过以下方式添加 外键约束::

编辑:: 你可以设计 item_order 表像 ::

CREATE TABLE customer(
         id INTEGER,
         firstName TEXT,
         middleName TEXT,
         lastName   TEXT,
         address TEXT,
         contactNum TEXT
);

 CREATE TABLE item(
        id INTEGER,
        name TEXT,
        description TEXT
 );

 CREATE TABLE order(
        id INTEGER,
        customerID INTEGER,
        date TEXT,
        FOREIGN KEY(customerId) REFERENCES customer(id)
 );

 CREATE TABLE item_order(
        id INTEGER,
        orderID INTEGER,
        itemId  INTEGER,
        quantity INTEGER,
        FOREIGN KEY(orderId) REFERENCES order(Id),
        FOREIGN KEY(itemId) REFERENCES item(Id)
 );
于 2012-06-01T04:02:54.593 回答
2

好的示例 http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/

// Table Create Statements
// Todo table create statement
private static final String CREATE_TABLE_TODO = "CREATE TABLE "
        + TABLE_TODO + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TODO
        + " TEXT," + KEY_STATUS + " INTEGER," + KEY_CREATED_AT
        + " DATETIME" + ")";

// Tag table create statement
private static final String CREATE_TABLE_TAG = "CREATE TABLE " + TABLE_TAG
        + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TAG_NAME + " TEXT,"
        + KEY_CREATED_AT + " DATETIME" + ")";

// todo_tag table create statement
private static final String CREATE_TABLE_TODO_TAG = "CREATE TABLE "
        + TABLE_TODO_TAG + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
        + KEY_TODO_ID + " INTEGER," + KEY_TAG_ID + " INTEGER,"
        + KEY_CREATED_AT + " DATETIME" + ")";

SELECT * FROM todos td, tags tg, todo_tags tt WHERE tg.tag_name = 'Watchlist' AND tg.id = tt.tag_id AND td.id = tt.todo_id;

/*
 * getting all todos under single tag
 * */
public List<Todo> getAllToDosByTag(String tag_name) {
    List<Todo> todos = new ArrayList<Todo>();

    String selectQuery = "SELECT  * FROM " + TABLE_TODO + " td, "
            + TABLE_TAG + " tg, " + TABLE_TODO_TAG + " tt WHERE tg."
            + KEY_TAG_NAME + " = '" + tag_name + "'" + " AND tg." + KEY_ID
            + " = " + "tt." + KEY_TAG_ID + " AND td." + KEY_ID + " = "
            + "tt." + KEY_TODO_ID;

    Log.e(LOG, selectQuery);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {
            Todo td = new Todo();
            td.setId(c.getInt((c.getColumnIndex(KEY_ID))));
            td.setNote((c.getString(c.getColumnIndex(KEY_TODO))));
            td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));

            // adding to todo list
            todos.add(td);
        } while (c.moveToNext());
    }

    return todos;
}
于 2015-01-07T23:00:26.943 回答
-1

我想您应该阅读有关基本 sql 数据库编程的书或网站。您可以通过将一个表的键(或字段)添加到另一个表中来创建两个表之间的关系。但实际上,您应该先熟悉 sql。完成后,您可以创建关系,或者您会发现使用 Ormlite 很方便。

于 2012-06-01T03:29:43.910 回答