sqlite insert into table select * from

后端 未结 3 487
予麋鹿
予麋鹿 2020-12-02 18:38

I need to move data from one table to another in my Android app

I would like to use the following sql:

insert into MYTABLE2 select id, STATUS rispos         


        
相关标签:
3条回答
  • 2020-12-02 19:13

    explicitly specify the column name in the INSERT clause,

    INSERT INTO destinationTable (risposta, data_ins)
    SELECT STATUS risposta, DATETIME('now') data_ins 
    FROM   sourceTable
    
    0 讨论(0)
  • 2020-12-02 19:13

    You can specify which columns you're inserting into. Assuming that the _id column is autoincrement and you're inserting the other two columns, you can have something like this:

    insert into MYTABLE2 (riposta, data_ins)
    select STATUS risposta, DATETIME('now') data_ins from  MYTABLE 2
    
    0 讨论(0)
  • 2020-12-02 19:21

    This might help, a query from one table to another and it will also check if the selected column (id) already exist in another table.

    SQLite QUERY:

    INSERT INTO MYTABLE2(id,data_ins ) 
    SELECT id, data_ins FROM MYTABLE2
    WHERE id NOT IN ( SELECT id FROM MYTABLE1)
    

    Android:

     String select_insert_query = "INSERT INTO " + TABLE_MYTABLE2
                + "( " + ID + "," + DATA_INS + ") SELECT "
                + ID + ","
                + DATA_INS + " FROM "
                + TABLE_MYTABLE2
                + " WHERE " + ID + " NOT IN (SELECT " + ID
                + " FROM " + TABLE_MYTABLE1 + ")";
    
        SQLiteDatabase db = this.getWritableDatabase();
    
        Cursor cursor = db.rawQuery(select_insert_query, null);
        cursor.close();
    
    0 讨论(0)
提交回复
热议问题