【问题标题】:How to copy the value of one data field to another table in the same SQLite database如何将一个数据字段的值复制到同一个 SQLite 数据库中的另一个表中
【发布时间】:2018-10-09 04:30:23
【问题描述】:

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

在我的排行榜中,我有; 联赛编号 联赛名称

在我的系列表中,我有; 系列ID 系列联赛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);


    }

}

【问题讨论】:

    标签: javascript sqlite android-studio


    【解决方案1】:

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

    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';
    

    结果是:-

    【讨论】:

    • 我理解你上面解释的概念。这很简单。当我开始一个新系列时,我将如何获得联赛 ID。在这一点上,我还没有像你上面的例子那样给这个系列起个名字。
    • LeagueID 将由其名称或 id 确定(通常使用微调器/下拉菜单用于此目的)(id 实际上更好,因为它可以唯一标识行(如果定义为 INTEGER PRIMARY键或整数主键自动增量))。您实际如何执行此操作取决于您的代码和要求,因此基本上不可能具体说明。
    • @RobertVogl 如果这回答了你的问题,那么请勾选这个作为答案。
    • 虽然我理解您关于预先填充联赛的概念,但这是不可行的。用户正在创建联赛,需要将长 ID 传递给下一个活动,以写入 COLUMN_LEAGUE_ID 中的保龄球表。我一直在尝试将联盟主键传递给下一个活动,但它没有显示。
    • 我在上面附上了我的代码的 sn-ps。不确定我是否让这变得比需要的更困难。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多