简体   繁体   中英

Insert multiple values SQLite android with the same query

Frequently in my app I need to insert a lot of values in a table of the DB.

So I'd need run an insert like:

insert into clients 
(id, name, lastip, visites) values  
('20','John','1.1.1.1','0'),
('21','Kate','1.1.1.2','3'), 
('35','Phill','1.1.1.3','90') ;

I have two questions about that (I'm starter with SQL)

  1. How can I avoid crashes with primary keys (replacing the other columns if trying to inserting an existing row)?
  2. Is there a way for inserting all the values in the same query using the method insert () in SQLiteDatabase or a similar method? or I must build the query string and use execSQL() ?

thanks in advance

1.) A possibility to avoid primary key crashes is to use auto increment fields.

If a column has the type INTEGER PRIMARY KEY AUTOINCREMENT then a slightly different ROWID selection algorithm is used. The ROWID chosen for the new row is at least one larger than the largest ROWID that has ever before existed in that same table.

See sqlite documentation here .

If in your example id would be an auto increment field you do not need to specify it in the insert.

2.) Yes it is possible(for one row) like the documentation you refer to says:

public long insert (String table, String nullColumnHack, ContentValues values)

values this map contains the initial column values for the row. The keys should be the column names and the values the column values

However you should use prepared statements(reference here: How do I use prepared statements in SQlite in Android? )

If you want to insert all 3 rows with one shot you need to build the string or execute the inserts 3 times.

Edit to your comment:

3.) This is pretty well answered here: INSERT IF NOT EXISTS ELSE UPDATE?

See also here: http://sqlite.org/lang_conflict.html

REPLACE

When a UNIQUE constraint violation occurs, the REPLACE algorithm deletes pre-existing rows that are causing the constraint violation prior to inserting or updating the current row and the command continues executing normally. If a NOT NULL constraint violation occurs, the REPLACE conflict resolution replaces the NULL value with he default value for that column, or if the column has no default value, then the ABORT algorithm is used. If a CHECK constraint violation occurs, the REPLACE conflict resolution algorithm always works like ABORT.

If you're using a ContentProvider to access your database, you can do this with applyBatch() :

// List of operations
ArrayList<ContentProviderOperation) ops
    = new ArrayList<ContentProviderOperation>();

// Create the builder
ContentProviderOperation.Builder builder
    = ContentProviderOperation.newInsert(...);

// Create the first item
builder.withValue("Name", "John");
builder.withValue("IP", '1.1.1.1');
ops.add(builder.build());

// Create the second item
builder.withValue("Name", "Kate");
builder.withValue("IP", '1.1.1.2');
ops.add(builder.build());

// Insert the data as one transaction
getContentProvider().applyBatch("AUTHORITY", ops);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM