2012-06-01 22 views
8

Puoi aiutarmi sulla relazione tra due tabelle in sqlite.tabelle di relazione in sqlite su android

Inserisco, elimina e aggiorno i passaggi, ma ora devo supportare la relazione tra due tabelle. Immagino che
tutti i passaggi di codice che vengono eseguiti prima vengano modificati. Ho ragione? Hai qualche link o esempio che spiega le relazioni tra le tabelle e le attività dopo la relazione?

risposta

-1

Immagino che dovresti leggere un libro o un sito web sulla programmazione base del database SQL. Si crea una relazione tra due tabelle aggiungendo la chiave (o un campo) di una tabella nell'altra tabella. Ma in realtà dovresti familiarizzare con sql prima. Dopo averlo fatto, puoi creare relazioni, o troverai conveniente usare Ormlite.

15

Per stabilire una relazione tra due tabelle, è possibile utilizzare Foreign keys. Una chiave esterna è un campo in una tabella relazionale che corrisponde a Candidate Key di un'altra tabella.

Ad esempio, supponiamo di avere due tabelle, una tabella CUSTOMER che include tutti i dati dei clienti e una tabella ORDINE che include tutti gli ordini dei clienti. L'intenzione qui è che tutti gli ordini devono essere associati a un cliente che è già nella tabella CUSTOMER. Per fare ciò, inseriremo una chiave esterna nella tabella ORDER e lo faremo in relazione alla chiave primaria della tabella CUSTOMER.

In SQLite Foreign Key Constraints possono essere aggiunti in modo seguente ::

modifica :: è possibile progettare item_order tavolo come ::

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) 
); 
+0

Dopo questo passaggio, come è possibile inserire una tabella di ordini articoli. Perché molti clienti hanno molti ordini. Per inserire l'ordine, come posso raggiungere la tabella degli ordini. – user1417278

+0

in normalmente ho usato il codice qui sotto senza alcuna relazione. vuoto insertkisi privato (nome String, String e-mail, String pword) { \t \t \t \t \t SQLiteDatabase db = vnesne.getWritableDatabase(); \t ContentValues ​​cv = new ContentValues ​​(); \t \t \t \t cv.put (Veritabani.colName, nome); \t \t \t \t cv.put (Veritabani.colAge, email); \t \t \t \t cv.put (Veritabani.colDept, pword); \t \t \t \t db.insert ("kisi", null, cv); \t \t \t \t db.vicino(); \t \t \t} – user1417278

+0

@ user1417278 vedere la modifica, il codice che è stato utilizzato per inserire valori senza alcuna relazione può essere utilizzato anche ora fino a quando non si sta violando i vincoli di chiave esterna. – Eight

1

Buon campione 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, tag tg, todo_tags tt WHERE tg.tag_name = 'Watchlist' AND tg.id = tt.tag_id AND td.id = tt.todo_i d;

/* 
* 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; 
}