1

I start using novoda/SQLiteProvider following their "simple" demo project in a fresh Android/Gradle project. It seems that I am missing some step/configuration which causes the creation of the table. That is why I end up with this error:

SQLiteProvider  W  No constrain against URI: 
                   content://com.example.providers/products
     SQLiteLog  E  (1) no such table: products
SQLiteDatabase  E  Error inserting name=book
                E  android.database.sqlite.SQLiteException: 
                   no such table: products (code 1): , 
                   while compiling: INSERT INTO products(name) VALUES (?)

                E      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                E      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                E      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                E      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                E      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                E      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                E      at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
                E      at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
                E      at novoda.lib.sqliteprovider.provider.action.InsertHelper.insert(InsertHelper.java:41)
                E      at novoda.lib.sqliteprovider.provider.SQLiteContentProviderImpl.insertInTransaction(SQLiteContentProviderImpl.java:62)
                E      at novoda.lib.sqliteprovider.provider.SQLiteContentProvider.bulkInsert(SQLiteContentProvider.java:109)
                E      at android.content.ContentProvider$Transport.bulkInsert(ContentProvider.java:233)
                E      at android.content.ContentResolver.bulkInsert(ContentResolver.java:1251)
                E      at com.example.StorageHelper.store(StorageHelper.java:21)
                E      at com.example.tasks.DataTask.doInBackground(DataTask.java:38)
                E      at android.os.AsyncTask$2.call(AsyncTask.java:288)
                E      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                E      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                E      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                E      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                E      at java.lang.Thread.run(Thread.java:841)
      dalvikvm  W  threadid=11: thread exiting with uncaught exception (group=0x416b7d40)
AndroidRuntime  E  FATAL EXCEPTION: AsyncTask #1
                E  Process: com.example, PID: 23203

                E  java.lang.RuntimeException: 
                   An error occured while executing doInBackground()
                E      at android.os.AsyncTask$3.done(AsyncTask.java:300)
                E      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                E      at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                E      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                E      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                E      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                E      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                E      at java.lang.Thread.run(Thread.java:841)

                E  Caused by: android.database.SQLException: 
                   Failed to insert row into 
                   content://com.example.providers/products
                E      at novoda.lib.sqliteprovider.provider.action.InsertHelper.insert(InsertHelper.java:46)
                E      at novoda.lib.sqliteprovider.provider.SQLiteContentProviderImpl.insertInTransaction(SQLiteContentProviderImpl.java:62)
                E      at novoda.lib.sqliteprovider.provider.SQLiteContentProvider.bulkInsert(SQLiteContentProvider.java:109)
                E      at android.content.ContentProvider$Transport.bulkInsert(ContentProvider.java:233)
                E      at android.content.ContentResolver.bulkInsert(ContentResolver.java:1251)
                E      at com.example.StorageHelper.store(StorageHelper.java:21)
                E      at com.example.tasks.DataTask.doInBackground(DataTask.java:38)
                E      at android.os.AsyncTask$2.call(AsyncTask.java:288)
                E      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                E      ... 4 more
       Process  I  Sending signal. PID: 23203 SIG: 9

Here is what I put together ...

// app/build.gradle
dependencies {
    compile 'com.android.support:support-v4:19.0.1'
    compile 'com.android.support:appcompat-v7:19.0.1'
    compile 'com.novoda:sqliteprovider-core:1.0.0'
}

...

// com.example.providers.ProductProvider.java
public class ProductProvider extends SQLiteContentProviderImpl {

    private static final String AUTHORITY =
            "content://com.example.providers";
    private static final String TABLE_NAME_PRODUCTS = "products";
    public static final String COL_PRODUCTS_NAME = "name";

    public static final Uri PRODUCTS =
            Uri.parse(AUTHORITY).buildUpon()
            .appendPath(TABLE_NAME_PRODUCTS).build();
}

...

<!-- AndroidManifest.xml -->
<application
    <provider
        android:name=".providers.ProductProvider"
        android:authorities="com.example.providers"
        android:exported="false" />
</application>  

...

// app/src/main/assets/migrations/001_setup.sql
CREATE TABLE IF NOT EXISTS 'products' (
    _id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
);

...

Uri table = ProductProvider.PRODUCTS;
ContentValues values = new ContentValues(1);
values.put(ProductProvider.COL_PRODUCTS_NAME, "book");
getActivity().getContentResolver().insert(table, values);
JJD
  • 50,076
  • 60
  • 203
  • 339
  • What version of the SQLiteProvider arre you using? Did you ensure you uninstalled any old APK's (a fresh install will recreate the DB for you correctly). Drop the leading zero's from your migration file name, i.e. `1_` not `001_` – Blundell Jan 31 '14 at 08:45
  • @Blundell I added the version to the question: v.1.0.0. Yes, the APK is installed freshly to the device. I removed `~/.gradle/caches/modules-2/files-2.1/com.novoda/` before running gradle. The `sqliteprovider-core-1.0.0.jar` gets downloaded freshly. No difference without leading zeros in the file name of the SQL file. Still failing. – JJD Jan 31 '14 at 09:45

1 Answers1

2

I don't believe multi-line SQL is supported in Version 1.0.0.

Two options, change your migration file:

// app/src/main/assets/migrations/001_setup.sql

CREATE TABLE IF NOT EXISTS 'products' (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT);

or use the latest snapshot version:

 compile 'com.novoda:sqliteprovider-core:1.0.1-SNAPSHOT'

latest release is now available, no longer snapshot

 compile 'com.novoda:sqliteprovider-core:1.0.1'
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • WOW. You should definitely mention this on the project page. Otherwise this will take very long to figure out. Thanks a lot. – JJD Jan 31 '14 at 10:10
  • 1
    I'll get version 1.0.1 released today – Blundell Jan 31 '14 at 10:16
  • 1
    Nice. Still a `CHANGELOG` or GitHub Release notes would be very welcome. – JJD Jan 31 '14 at 10:19
  • I also tried the 1.0.1-SNAPSHOT. It works. Just for others that run into this issue: I had to let Maven install the POM to `~/.m2/..` so that Gradle finds it. It is not available via [novoda/public-mvn-repo](https://github.com/novoda/public-mvn-repo). Thanks again. – JJD Jan 31 '14 at 10:33
  • 1
    Yes we need to add a ChangeLog good point! @JJD v1.0.1 has now been released and is available on `novoda/public-mvn-repo` – Blundell Jan 31 '14 at 12:03
  • Can you tell what the `SQLiteProvider` warning `No constrain against URI:` is caused from? – JJD Feb 04 '14 at 16:08
  • 1
    It means the table you have created has no unique field and so it is dangerous in that you could attempt to create two rows of data that could be exactly the same. – Blundell Feb 04 '14 at 16:58