<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Android ContentProvider on SQLite tables without the _id column</title>
	<atom:link href="http://www.casarini.org/blog/2009/android-contentprovider-on-sqlite-tables-without-the-_id-column/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.casarini.org/blog/2009/android-contentprovider-on-sqlite-tables-without-the-_id-column/</link>
	<description>happy programming</description>
	<pubDate>Thu, 09 Sep 2010 11:27:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
		<item>
		<title>By: paolo</title>
		<link>http://www.casarini.org/blog/2009/android-contentprovider-on-sqlite-tables-without-the-_id-column/#comment-21591</link>
		<dc:creator>paolo</dc:creator>
		<pubDate>Thu, 05 Aug 2010 13:51:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.casarini.org/blog/?p=216#comment-21591</guid>
		<description>In the query method example I assume a mOpenHelper object as a member variable that is able to provide a SQLiteDatabase object through the getReadableDatabase method.

If you need a class for it, an example could be the following:

[java]
public class SQLiteDatabaseHelper {
    private static final String TAG = SQLiteDatabaseHelper.class.getSimpleName();

    private final CursorFactory factory;
    private final int version;
    private final File dbFile;
    
    private SQLiteDatabase database;
    private boolean isInitializing;
    
    /**
     * Create a helper object to create, open, and/or manage a database
     * already built and provide in specific path.
     * The database is not actually created or opened until one of
     * {@link #getWritableDatabase} or {@link #getReadableDatabase} is called.
     *
     * @param dbpath the path of the database file
     * @param factory to use for creating cursor objects, or null for the default
     * @param version number of the database
     */    
    public SQLiteDatabaseHelper(String dbpath, CursorFactory factory, int version) {
        this.factory = factory;
        this.version = version;
        dbFile = new File(dbpath);
    }
    
    public synchronized SQLiteDatabase getWritableDatabase() {
        if (database != null &#038;&#038; database.isOpen() &#038;&#038; !database.isReadOnly()) {
            return database;  // The database is already open for business
        }

        if (isInitializing) {
            throw new IllegalStateException("getWritableDatabase called recursively");
        }

        boolean success = false;
        SQLiteDatabase db = null;
        try {
            isInitializing = true;
            if (dbFile.exists()) {
                db = SQLiteDatabase.openDatabase(dbFile.getPath(), factory, SQLiteDatabase.OPEN_READWRITE);
                if (db.getVersion() != version) {
                    throw new SQLiteException("The database in [" + dbFile.getPath() +
                            "] has version [" + db.getVersion() + "] while the version needed is [" + version +"]");
                }
            } else {
                throw new SQLiteException("Error while opening database: the file [" + dbFile.getPath() + "] does not exist");
            }
            
            success = true;
            return db;
        } finally {
            isInitializing = false;
            if (success) {
                if (database != null) {
                    try { database.close(); } catch (Exception e) {}
                }
                database = db;
            } else {
                if (db != null) db.close();
            }
        }
    }
    
    public synchronized SQLiteDatabase getReadableDatabase() {
        if (database != null &#038;&#038; database.isOpen()) {
            return database;  // The database is already open for business
        }
        
        if (isInitializing) {
            throw new IllegalStateException("getReadableDatabase called recursively");
        }
        
        try {
            return getWritableDatabase();
        } catch (SQLiteException e) {
            Log.w(TAG, "Couldn't open " + dbFile.getPath() + " for writing (will try read-only):", e);
        }
        
        SQLiteDatabase db = null;
        try {
            isInitializing = true;
            db = SQLiteDatabase.openDatabase(dbFile.getPath(), factory, SQLiteDatabase.OPEN_READONLY);
            if (db.getVersion() != version) {
                throw new SQLiteException("The database in [" + dbFile.getPath() +
                        "] has version [" + db.getVersion() + "] while the version needed is [" + version +"]");
            }
            
            Log.i(TAG, "Opened " + dbFile.getPath() + " in read-only mode");
            database = db;
            return database;
        } finally {
            isInitializing = false;
            if (db != null &#038;&#038; db != database) db.close();
        }
    }
    
    public synchronized void close() {
        if (database != null) {
            try { database.close(); } catch (Exception e) {}
        }
    }
}
[/java]</description>
		<content:encoded><![CDATA[<p>In the query method example I assume a mOpenHelper object as a member variable that is able to provide a SQLiteDatabase object through the getReadableDatabase method.</p>
<p>If you need a class for it, an example could be the following:</p>
<p>[java]<br />
public class SQLiteDatabaseHelper {<br />
    private static final String TAG = SQLiteDatabaseHelper.class.getSimpleName();</p>
<p>    private final CursorFactory factory;<br />
    private final int version;<br />
    private final File dbFile;</p>
<p>    private SQLiteDatabase database;<br />
    private boolean isInitializing;</p>
<p>    /**<br />
     * Create a helper object to create, open, and/or manage a database<br />
     * already built and provide in specific path.<br />
     * The database is not actually created or opened until one of<br />
     * {@link #getWritableDatabase} or {@link #getReadableDatabase} is called.<br />
     *<br />
     * @param dbpath the path of the database file<br />
     * @param factory to use for creating cursor objects, or null for the default<br />
     * @param version number of the database<br />
     */<br />
    public SQLiteDatabaseHelper(String dbpath, CursorFactory factory, int version) {<br />
        this.factory = factory;<br />
        this.version = version;<br />
        dbFile = new File(dbpath);<br />
    }</p>
<p>    public synchronized SQLiteDatabase getWritableDatabase() {<br />
        if (database != null &#038;&#038; database.isOpen() &#038;&#038; !database.isReadOnly()) {<br />
            return database;  // The database is already open for business<br />
        }</p>
<p>        if (isInitializing) {<br />
            throw new IllegalStateException(&#8221;getWritableDatabase called recursively&#8221;);<br />
        }</p>
<p>        boolean success = false;<br />
        SQLiteDatabase db = null;<br />
        try {<br />
            isInitializing = true;<br />
            if (dbFile.exists()) {<br />
                db = SQLiteDatabase.openDatabase(dbFile.getPath(), factory, SQLiteDatabase.OPEN_READWRITE);<br />
                if (db.getVersion() != version) {<br />
                    throw new SQLiteException(&#8221;The database in [" + dbFile.getPath() +<br />
                            "] has version [" + db.getVersion() + "] while the version needed is [" + version +"]&#8220;);<br />
                }<br />
            } else {<br />
                throw new SQLiteException(&#8221;Error while opening database: the file [" + dbFile.getPath() + "] does not exist&#8221;);<br />
            }</p>
<p>            success = true;<br />
            return db;<br />
        } finally {<br />
            isInitializing = false;<br />
            if (success) {<br />
                if (database != null) {<br />
                    try { database.close(); } catch (Exception e) {}<br />
                }<br />
                database = db;<br />
            } else {<br />
                if (db != null) db.close();<br />
            }<br />
        }<br />
    }</p>
<p>    public synchronized SQLiteDatabase getReadableDatabase() {<br />
        if (database != null &#038;&#038; database.isOpen()) {<br />
            return database;  // The database is already open for business<br />
        }</p>
<p>        if (isInitializing) {<br />
            throw new IllegalStateException(&#8221;getReadableDatabase called recursively&#8221;);<br />
        }</p>
<p>        try {<br />
            return getWritableDatabase();<br />
        } catch (SQLiteException e) {<br />
            Log.w(TAG, &#8220;Couldn&#8217;t open &#8221; + dbFile.getPath() + &#8221; for writing (will try read-only):&#8221;, e);<br />
        }</p>
<p>        SQLiteDatabase db = null;<br />
        try {<br />
            isInitializing = true;<br />
            db = SQLiteDatabase.openDatabase(dbFile.getPath(), factory, SQLiteDatabase.OPEN_READONLY);<br />
            if (db.getVersion() != version) {<br />
                throw new SQLiteException(&#8221;The database in [" + dbFile.getPath() +<br />
                        "] has version [" + db.getVersion() + "] while the version needed is [" + version +"]&#8220;);<br />
            }</p>
<p>            Log.i(TAG, &#8220;Opened &#8221; + dbFile.getPath() + &#8221; in read-only mode&#8221;);<br />
            database = db;<br />
            return database;<br />
        } finally {<br />
            isInitializing = false;<br />
            if (db != null &#038;&#038; db != database) db.close();<br />
        }<br />
    }</p>
<p>    public synchronized void close() {<br />
        if (database != null) {<br />
            try { database.close(); } catch (Exception e) {}<br />
        }<br />
    }<br />
}<br />
[/java]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mikey</title>
		<link>http://www.casarini.org/blog/2009/android-contentprovider-on-sqlite-tables-without-the-_id-column/#comment-21590</link>
		<dc:creator>Mikey</dc:creator>
		<pubDate>Thu, 05 Aug 2010 13:24:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.casarini.org/blog/?p=216#comment-21590</guid>
		<description>Can you please state solution for "mOpenHelper cannot be resolved" ?
Do we need to use own DBHelper?</description>
		<content:encoded><![CDATA[<p>Can you please state solution for &#8220;mOpenHelper cannot be resolved&#8221; ?<br />
Do we need to use own DBHelper?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lorenz</title>
		<link>http://www.casarini.org/blog/2009/android-contentprovider-on-sqlite-tables-without-the-_id-column/#comment-21573</link>
		<dc:creator>Lorenz</dc:creator>
		<pubDate>Mon, 07 Jun 2010 23:44:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.casarini.org/blog/?p=216#comment-21573</guid>
		<description>Nice article for android sqlite, thanx for write it up..:-)</description>
		<content:encoded><![CDATA[<p>Nice article for android sqlite, thanx for write it up..:-)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
