如何将一个数据字段的值复制到同一SQLite数据库中的另一个表

时间:2018-04-28 14:13:25

标签: javascript sqlite android-studio

我想将主键从一个表复制到同一SQLite数据库中不同表中的新字段。

在我的联赛表中我有; LeagueId LeagueName

在我的系列表中我有; SeriesID SeriesLeagueID SERIESNAME

我希望能够将LeagueID移动到Series表中,并用它填充SeriesLeagueID。

我已经对此进行了搜索,但我似乎只能找到如何将完整的行或列从表格复制到另一个。

非常感谢任何协助。

MainActivity

//On Long Click On The RecyclerView Item An Alert Dialog Is Opened With The Option To Choose Edit/Delete
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
                recyclerView, new RecyclerTouchListener.ClickListener() {
            @Override
            public void onClick(View view, final int position) {



                int leagueId = leaguesList.get( position ).getId();
                Intent myIntent = new Intent(getBaseContext(), Main2Activity.class);
                myIntent.putExtra("value1", leagueId);
                startActivity(myIntent);
                //Intent myIntent = new Intent(getApplicationContext(), BowlerActivity.class);
                //startActivity(myIntent);

            }

            @Override
            public void onLongClick(View view, int position) {
                showActionsDialog(position);
            }
        }));
    }

Main2Activity

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.league_id );
        Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
        setSupportActionBar( toolbar );

        String savedExtra = getIntent().getStringExtra( "value1" );
    TextView myText = (TextView) findViewById(R.id.tvLeagueId);
        final String s = myText.toString();
        myText.setText(savedExtra);


    }

}

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容来更新系列表: -

UPDATE Series 
    SET SeriesLeagueID = (
        SELECT LeagueId 
        FROM League 
        WHERE Leaguename = 'Premier'
    ) 
    WHERE SeriesName = 'Test2';

注意到

  • 联盟表中的行是(从中获取LeagueId),在此示例中,通过将具有LeagueName列的行标识为具有值Premier(假设您事先不知道LeaugeId)< / LI>
  • 要更新的行标识为SeriesName列中具有值Test2的行。

用于测试的代码: -

DROP TABLE IF EXISTS League;
DROP TABLE IF EXISTS Series;
CREATE TABLE IF NOT EXISTS League (LeagueId INTEGER PRIMARY KEY, LeagueName TEXT);
CREATE TABLE IF NOT EXISTS Series (SeriesID INTEGER PRIMARY KEY, SeriesLeagueID INTEGER DEFAULT NULL, SeriesName TEXT);
-- Add some leagues
INSERT INTO League (LeagueName) VALUES('Premier'),('A'),('B');
-- Add some Series (note that SeriesLeagueId is null when inserting)
INSERT INTO Series (Seriesname) VALUES('Test1'),('Test2'),('Test3');

UPDATE Series 
    SET SeriesLeagueID = (
        SELECT LeagueId 
        FROM League 
        WHERE Leaguename = 'Premier'
    ) 
    WHERE SeriesName = 'Test2';

结果是: -

enter image description here