首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SqliteOpenHelper类的getReadbleDataBase()中出现异常

SqliteOpenHelper类的getReadbleDataBase()中出现异常
EN

Stack Overflow用户
提问于 2012-10-08 14:26:15
回答 1查看 273关注 0票数 0

当我使用android 4.0.3时,我在getReadbleDataBase()方法中得到了一个异常。

代码语言:javascript
运行
复制
   10-08 11:34:24.163: E/SQLiteOpenHelper(18710): Couldn't open quytech.db for writing (will try read-only):
10-08 11:34:24.163: E/SQLiteOpenHelper(18710): android.database.sqlite.SQLiteException: Can't downgrade database from version 6 to 1
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:325)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:186)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:249)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at com.quytech.androidclient.data.MessageCountProvider.query(MessageCountProvider.java:156)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at android.content.ContentProvider$Transport.query(ContentProvider.java:189)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at android.content.ContentResolver.query(ContentResolver.java:315)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at com.quytech.androidclient.service.SmackableImp$6.processPacket(SmackableImp.java:1781)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at org.jivesoftware.smack.Connection$ListenerWrapper.notifyListener(Connection.java:829)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at org.jivesoftware.smack.PacketReader$ListenerNotification.run(PacketReader.java:463)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-08 11:34:24.163: E/SQLiteOpenHelper(18710):  at java.lang.Thread.run(Thread.java:864)

但这在android 2.0.3中运行得很好。

首先,我尝试从dataBase中获取查询。

代码语言:javascript
运行
复制
Cursor cursor = mContentResolver.query(MessageCountProvider.CONTENT_URI,
                        null,  MessageCountConstant.JID + " = ? AND "
                        + MessageCountConstant.To_USER + " = ? AND " 
                        + MessageCountConstant.DATE + " = ? ", new String [] {mConfig.userName,toJID,date},null);


                if(cursor.getCount() > 0)
                {
                    cursor.moveToFirst();
                    int recieveMessageCountColmnIndex = cursor.getColumnIndexOrThrow(MessageCountConstant.RECIEVE_MESSAGE_COUNT);
                    int sendMessageCountColmnIndex = cursor.getColumnIndexOrThrow(MessageCountConstant.SEND_MESSAGE_COUNT);
                    int dateColumnIndex = cursor.getColumnIndexOrThrow(MessageCountConstant.DATE);
                    String dateDB = cursor.getString(dateColumnIndex);
                    int sendMessageCount = cursor.getInt(sendMessageCountColmnIndex);
                    int reciveMessageCount = cursor.getInt(recieveMessageCountColmnIndex);

                    updateMessageCountInDB(mConfig.userName, toJID, sendMessageCount+1, reciveMessageCount, date);
                }
                else
                {
                    addMessageCountInDB(mConfig.userName, toJID, 1, 0, date);

                }

在它之后,它调用我用ContentProvider扩展的。

并调用query()方法。

代码语言:javascript
运行
复制
@Override
public Cursor query(Uri url, String[] projectionIn, String selection,
        String[] selectionArgs, String sortOrder) {
    // TODO Auto-generated method stub
    SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
    int match = URI_MATCHER.match(url);

    switch (match) {
    case MESSAGES:
        qBuilder.setTables(TABLE_NAME);
        break;
    case MESSAGE_ID:
        qBuilder.setTables(TABLE_NAME);
        qBuilder.appendWhere("_id=");
        qBuilder.appendWhere(url.getPathSegments().get(1));
        break;
    default:
        throw new IllegalArgumentException("Unknown URL " + url);
    }

    String orderBy;
    if (TextUtils.isEmpty(sortOrder)) {
        orderBy = ChatConstants.DEFAULT_SORT_ORDER;
    } else {
        orderBy = sortOrder;
    }

    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor ret = qBuilder.query(db, projectionIn, selection, selectionArgs,
            null, null, orderBy);

    if (ret == null) {
        infoLog("MessageCountProvider.query: failed");
    } else {
        ret.setNotificationUri(getContext().getContentResolver(), url);
    }

    return ret;
}







private static class MessageCountDataBaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "quytech.db";
private static final int DATABASE_VERSION = 1;

public MessageCountDataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) 
{
    if (LogConstants.LOG_DEBUG) {
        infoLog("creating new chat table");
    }

    db.execSQL("CREATE TABLE " + TABLE_NAME + " (" 

            + MessageCountConstant._ID+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + MessageCountConstant.JID + " TEXT," 
            + MessageCountConstant.To_USER + " TEXT,"
            + MessageCountConstant.SEND_MESSAGE_COUNT + " INTEGER,"
            + MessageCountConstant.RECIEVE_MESSAGE_COUNT + " INTEGER,"
            + MessageCountConstant.DATE + " DATE"

            +");"

            );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    infoLog("onUpgrade: from " + oldVersion + " to " + newVersion);

    infoLog("onUpgrade: from " + oldVersion + " to " + newVersion);
    switch (oldVersion) {
    default:
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}
}

请帮帮我..

EN

回答 1

Stack Overflow用户

发布于 2012-10-08 15:12:51

错误消息显示:

无法将数据库从版本6降级到版本1

该设备上的当前数据库版本为6。

如果你的应用程序无法处理降级(这是在API版本11中引入的),你必须通过清除应用程序的数据或卸载应用程序来删除该数据库。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12776339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档