Table with _ id INTEGER PRIMARY KEY AUTOINCREMENT
_id|NAME|ADDRESS And import file
???|Саша|Верхнепортовая, 51 This is a general procedure for copying data containing an auto-increment field. How to apply it with android? - probably there too it is possible to write requests)
If there are no other tables with fields referencing this auto-increment field (or other fields in this table that reference it), then just copy: insert insert database.schema.table (fieds) select from ... while in the field list of the target table And in you do not specify this autoincrement field. All finished.
Only if ...
If there are tables with fields that refer to this field, then you will first have to create a temporary table with two fields - the old and the new ID. Fill it (for now only the field is the 'old ID') of all rows that you plan to transfer (this is 1 insert-select query), then basic copying with the cursor, line by line + getting the ID of the new record and saving it to a temporary table (getting the last ID is @ @identity, which must be read directly after each insert operation inside the cursor, for example: select @newid = @@ identity)
After the main table is transferred, we copy the references to it (one query), while changing the value of the reference field from the old to the new (insert-select construction and in the additional join to a temporary table with a link - reference field = 'old id')
if in the main portable table there is a field referring to the auto-increment ID, then in the temporary table you need another field - 'old reference to ID' (also pre-stored in the same query as the 'old ID'). At the main copying, the value of this field in the target table is replaced with some existing ID value in the new table. After the main copy with one query (with join to the temporary table - replace the value of this link with the current new one from the temporary table)
If ID conflicts are not supposed (for example, the target table is empty) and you also need to copy linked tables, then you can copy without recalculating the keys. Just temporarily disable the auto-increment rule, copy as is, indicating all the fields. Then turn this rule back on:
SET IDENTITY_INSERT [database_name. [schema_name]. ] table OFF
SET IDENTITY_INSERT [database_name. [schema_name]. ] table ON
Source: https://ru.stackoverflow.com/questions/508128/
All Articles