SQLiteProvider / SQLiteLog错误:没有这样的表:产品

时间:2014-01-30 19:15:04

标签: android android-sqlite android-contentprovider create-table sqliteopenhelper

我开始在新的Android / Gradle项目中使用novoda/SQLiteProvider跟随他们的“简单”演示项目。似乎我缺少一些导致表创建的步骤/配置。这就是为什么我最终会遇到这个错误:

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

这是我放在一起的......

// 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);

1 个答案:

答案 0 :(得分:2)

我不相信版本1.0.0中支持多行SQL。

两个选项,更改您的迁移文件:

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

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

或使用最新的快照版本:

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

最新版本现已推出,不再是快照

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