p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 (http://p2p.wrox.com/forumdisplay.php?f=432)
-   -   Error in Content Provider & SQLITE (http://p2p.wrox.com/showthread.php?t=89157)

puneetverma05 January 15th, 2013 12:47 PM

Error in Content Provider & SQLITE
 
Error

01-15 21:27:01.236: E/AndroidRuntime(944): FATAL EXCEPTION: main
01-15 21:27:01.236: E/AndroidRuntime(944): java.lang.RuntimeException: Unable to get provider com.uwe.subodh2.MessageProvider: android.database.sqlite.SQLiteException: near "tablemessages": syntax error (code 1): , while compiling: create tablemessages (_id integer primary key autoincrement, date INTEGER, title TEXT, details TEXT);
01-15 21:27:01.236: E/AndroidRuntime(944): at android.app.ActivityThread.installProvider(Activit yThread.java:4563)
01-15 21:27:01.236: E/AndroidRuntime(944): at android.app.ActivityThread.installContentProviders (ActivityThread.java:4190)
01-15 21:27:01.236: E/AndroidRuntime(944): at android.app.ActivityThread.handleBindApplication(A ctivityThread.java:4132)
01-15 21:27:01.236: E/AndroidRuntime(944): at android.app.ActivityThread.access$1300(ActivityThr ead.java:130)
01-15 21:27:01.236: E/AndroidRuntime(944): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1255)
01-15 21:27:01.236: E/AndroidRuntime(944): at android.os.Handler.dispatchMessage(Handler.java:99 )
01-15 21:27:01.236: E/AndroidRuntime(944): at android.os.Looper.loop(Looper.java:137)
01-15 21:27:01.236: E/AndroidRuntime(944): at android.app.ActivityThread.main(ActivityThread.jav a:4745)
01-15 21:27:01.236: E/AndroidRuntime(944): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 21:27:01.236: E/AndroidRuntime(944): at java.lang.reflect.Method.invoke(Method.java:511)
01-15 21:27:01.236: E/AndroidRuntime(944): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:786)
01-15 21:27:01.236: E/AndroidRuntime(944): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:553)
01-15 21:27:01.236: E/AndroidRuntime(944): at dalvik.system.NativeStart.main(Native Method)




MESSAGE PROVIDER.java

package com.uwe.subodh2;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFacto ry;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class MessageProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://com.uwe.provider.subodh2.contentprovider/messages");
private static final int MESS = 1;
private static final int MESS_ID = 2;
private static final UriMatcher uriMatcher;

static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.uwe.provider.subodh2.conten tprovider.Message", "messages",MESS);
uriMatcher.addURI("com.uwe.provider.subodh2.conten tprovider.Message", "messages/#",MESS_ID);
}
@Override
public boolean onCreate() {
Context context = getContext();
messageDatabaseHelper dbHelper;
dbHelper = new messageDatabaseHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
messageDB = dbHelper.getWritableDatabase();
return (messageDB == null) ? false : true;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case MESS:
return "vnd.android.cursor.dir/vnd.com.uwe.subodh2.contentprovider.messages";
case MESS_ID:
return "vnd.android.cursor.item/vnd.com.uwe.subodh2.contentprovider.messages";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public Uri insert(Uri _uri, ContentValues _initialValues) {

// Insert the new row, will return the row number if
// successful.
long rowID = messageDB.insert(MESSAGE_TABLE, "mess",
_initialValues);
// Return a URI to the newly inserted row on success.
if (rowID > 0) {
Uri uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(uri , null);
return uri;
}
throw new SQLException("Failed to insert row into" + _uri);
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sort) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(MESSAGE_TABLE);
// If this is a row query, limit the result set to the passed in row.
switch (uriMatcher.match(uri)) {
case MESS_ID:
qb.appendWhere(KEY_ID + "=" + uri.getPathSegments().get(1));
break;
default: break;
}
// If no sort order is specified sort by date / time
String orderBy;
if (TextUtils.isEmpty(sort)) {
orderBy = KEY_DATE;
} else {
orderBy = sort;
}
// Apply the query to the underlying database.
Cursor c = qb.query(messageDB,
projection,
selection, selectionArgs,
null, null,
orderBy);
// Register the contexts ContentResolver to be notified if
// the cursor result set changes.
c.setNotificationUri(getContext().getContentResolv er(), uri);
// Return a cursor to the query result.
return c;
}


@Override
public int delete (Uri uri, String where, String[] whereArgs) {
int count;
switch (uriMatcher.match(uri)) {
case MESS:
count = messageDB.delete(MESSAGE_TABLE, where, whereArgs);
break;
case MESS_ID:
String segment = uri.getPathSegments().get(1);
count = messageDB.delete(MESSAGE_TABLE, KEY_ID + "="
+ segment
+ (!TextUtils.isEmpty(where) ? " AND ("
+ where + ')' : ""), whereArgs);
break;
default: throw new IllegalArgumentException("Unsupported URI: " +
uri);
}
getContext().getContentResolver().notifyChange(uri , null);
return count;
}
@Override
public int update (Uri uri, ContentValues values, String where,
String[] whereArgs) {
int count;
switch (uriMatcher.match(uri)) {
case MESS:
count = messageDB.update(MESSAGE_TABLE, values,
where, whereArgs);
break;
case MESS_ID:
String segment = uri.getPathSegments().get(1);
count = messageDB.update(MESSAGE_TABLE, values, KEY_ID
+ "=" + segment
+ (!TextUtils.isEmpty(where) ? " AND ("+ where + ')' : ""), whereArgs);
break;
default: throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri , null);
return count;


}


// The underlying database
private SQLiteDatabase messageDB;
private static final String TAG = "MessageProvider";
private static final String DATABASE_NAME = "messages.db";
private static final int DATABASE_VERSION = 1;
private static final String MESSAGE_TABLE = "messages";
// Column Names
public static final String KEY_ID = "_id";
public static final String KEY_DATE ="date";
public static final String KEY_TITLE ="title";
public static final String KEY_DETAILS ="details";

// Column indexes
public static final int DATE_COLUMN = 1;
public static final int TITLE_COLUMN = 2;
public static final int DETAILS_COLUMN =3;

// Helper class for opening, creating, and managing
// database version control
private static class messageDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_CREATE =
"create table" + MESSAGE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_DATE + " INTEGER, "
+ KEY_TITLE + " TEXT, "
+ KEY_DETAILS+" TEXT);";
public messageDatabaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
Log.w(TAG,
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + MESSAGE_TABLE);
onCreate(db);
}
// Create the constants used to differentiate between the different URI
// requests.

}
}


All times are GMT -4. The time now is 08:04 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.