2016-10-14 16 views
1

私はアプリケーションを作成しています。既に最初のテーブルに保存されている既存のユーザの電子メールが必要で、2番目のテーブルに保存します。これを達成するために必要なクエリまたは関数助言がありますか?最初のテーブルから2番目のテーブルにメールを保存

+0

ユーザーを2人フェッチしてから、別の表に挿入するために挿入関数を作成するだけで済みます。 – 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列です。

関連する問題