Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Enhance Insert on API 15 (ICS) to still allow inserting multiple sets of values at once #50

Open
monzee opened this issue Jul 24, 2015 · 5 comments

Comments

@monzee
Copy link

monzee commented Jul 24, 2015

I had a statement like this in an onTablesCreated

tryExecStatement(Insert.into(Group.TABLE)
  .columns(Group.NAME, Group.DESCRIPTION, Group.ROLE)
  .values("admin", "Admin", 1)
  .values("staff", "Staff", 2)
  .values("users", "Default group", 3));

My onError handler captured this exception message in an API 15 AVD:

Failed to execute statement: INSERT INTO groups (name,description,role) VALUES (?,?,?),(?,?,?),(?,?,?)
    android.database.sqlite.SQLiteException: near ",": syntax error: , while compiling: INSERT INTO groups (name,description,role) VALUES (?,?,?),(?,?,?),(?,?,?)

Multi-row inserts are supported in SQLite 3.7.11 and above. ICS uses 3.7.4.

A workaround is to explode the statement into individual statements.

@sbosley
Copy link
Contributor

sbosley commented Jul 27, 2015

Thanks for reporting this! We'll work on a fix.

@sbosley
Copy link
Contributor

sbosley commented Jul 29, 2015

For now, we're going to throw an exception if you try to add multiple values clauses to an Insert statement on API < 16. This is really just status quo with writing raw SQL, so it's a temporary measure to make sure things don't fail silently. We've also added documentation to the values() method to reflect the change. In the future, we'd like to do some things behind the scenes to split the insert up into multiple statements so that we can remove this restriction, but that's more of a feature request that we'll need a little more time to think about. I'm going to update the issue title to reflect that future direction.

@sbosley sbosley changed the title SQL generated by Insert with multiple values incompatible with API 15 (ICS) Enhance Insert on API 15 (ICS) to still allow inserting multiple sets of values at once Jul 29, 2015
@monzee
Copy link
Author

monzee commented Jul 30, 2015

Nice. I can live with it; squidb has already bought me a lot. Insert queries like these only appear in migration and setup methods only anyway. It's a minor annoyance at worst.

@sbosley
Copy link
Contributor

sbosley commented Jul 30, 2015

Cool, good to hear. For what its worth, some planned future enhancements to the API will allow you to do dao-style inserts using model objects even from within migration and setup, which should further reduce the need for the Insert objects.

@jdkoren
Copy link
Collaborator

jdkoren commented Jul 30, 2015

Fun fact: you can do this inside of setup/migration methods:

DatabaseDao dao = new DatabaseDao(this);

Although, as @sbosley noted, future changes should obviate the need for this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants