2016-10-14 51 views
1

我正在創建應用程序。我需要現有用戶的電子郵件,該電子郵件已保存在第一個表中,並將其保存到第二個表中。我需要哪些查詢或功能來實現此目的?有什麼建議麼?將電子郵件從第一張表保存到第二張表

+0

您只需要兩次獲取用戶,然後創建一個插入函數將其重新插入到另一個表中。 – Razor

回答

1

首先,你需要傳遞的有效行ID來獲取用戶的電子郵件:

public Cursor getRecord(long id) throws SQLException { 
    Cursor cursor = this.database.query(true, databaseTable, new String[] {columnId, columnName, columnEmail}, columnId + "=" + id, null, null, null, null, null); 

    if (cursor != null) { 
     cursor.moveToFirst(); 
    } 
    return cursor; 
} 

請注意,你可能有不同的列,以便改變String[]陣列與你指定的列。現在,我們可以創建另一個函數以保存電子郵件中的另一個表像這樣:

public long insertExistingUser(String name, String email) { 
    ContentValues contentValues = new ContentValues(); 

    contentValues.put(columnName, name); 
    contentValues.put(columnEmail, email); 

    return this.database.insert(otherDatabaseTable, null, contentValues); 
} 

這將插入其他用戶的信息到其他表。爲了使這個在你的應用程序中工作:

DatabaseAdapter db = new DatabaseAdapter(this); 

db.open(); 

Cursor cursor = db.getRecord(current_user_id); 

if (db.insertExistingUser(cursor.getString(1), cursor.getString(2)) > 0) 
    Toast.makeText(this, "Old user's info was inserted!", Toast.LENGTH_SHORT).show(); 

db.close(); 

cursor.getString(1)需要一個數字,表示它是什麼列。通常,0是用於獲取用戶電子郵件的id列。

相關問題