Question

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);
Était-ce utile?

La solution

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'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top