我想将主键从一个表复制到同一 SQLite 数据库中不同表中的新字段。
在我的排行榜中,我有; 联赛编号 联盟名称
在我的系列表中,我有; 系列号 系列联赛ID 系列名
我希望能够将 LeagueID 移动到 Series 表中并用它填充 SeriesLeagueID。
我已经对此进行了搜索,但我似乎只能找到如何将完整的行或列从一个表复制到另一个表。
如有任何帮助,我们将不胜感激。
主要事件
//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);
}
}
最佳答案
您可以使用以下内容来更新系列表:-
UPDATE Series
SET SeriesLeagueID = (
SELECT LeagueId
FROM League
WHERE Leaguename = 'Premier'
)
WHERE SeriesName = 'Test2';
注意到
- League 表中的行是(从中获取 LeagueId),在本示例中,通过将 LeagueName 列的行标识为具有值 Premier(假设您事先不知道 LeaugeId)
- 要更新的行被标识为 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';
结果是:-
关于javascript - 如何将一个数据字段的值复制到同一 SQLite 数据库中的另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50077454/