我想将主键从一个表复制到同一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);
}
}
答案 0 :(得分:1)
您可以使用以下内容来更新系列表: -
UPDATE Series
SET SeriesLeagueID = (
SELECT LeagueId
FROM League
WHERE Leaguename = 'Premier'
)
WHERE 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';
结果是: -