当我使用时android 4.0.3
,我在方法中遇到异常getReadbleDataBase()
。`
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
.
首先我尝试从数据库中获取查询。
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() 方法。
@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);
}
}
}
请帮我..