Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2
This is the forum to discuss the Wrox book Professional Android Application Development by Reto Meier; ISBN: 9780470344712
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 15th, 2013, 11:47 AM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

}
}
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Earthquake Content Provider Chapter 7 crustymonkey BOOK: Professional Android 2 Application Development 0 October 21st, 2010 07:31 PM
Problem: Chapter 7 "Creating and Using an Earthquake Content Provider" (Page 232) DrGaribaldi BOOK: Professional Android 2 Application Development 0 June 17th, 2010 04:07 AM
Does the content provider allow for MySql synchronization? Porjung BOOK: Professional Android 2 Application Development 2 April 7th, 2010 08:59 PM
Remote Connection Error: Named Pipes Provider error hbansal ASP.NET 2.0 Basics 5 April 12th, 2009 09:51 AM
» in content property natmaster CSS Cascading Style Sheets 1 April 17th, 2006 02:12 AM



All times are GMT -4. The time now is 10:35 AM.


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