How to manage the same database from different classes? (android/sqlite)?

Move all access to the data base into a ContentProvider . Each class can then interact with the ContentProvider, which then interacts with the database.

Up vote 0 down vote favorite share g+ share fb share tw.

In my android app I had one class doing the following functions: storing data from an XML source into a database; loading the data from the database; rewriting to the database from the xml source if the data was more than 24 hours old; that all worked fine. I am now trying to write a new table to that same database from another class. So I followed the same procedures/set-up as in the first class and now the program crashes (force close error) and the android log cat gives the following errors: SQL error: msg= table "table-name" does not exist error inserting (query) into table "table-name" (this error is repeated for each insert attempted) The problem is, the "table-name" it gives me (saying it doesn't exist) is the table from the first class which worked fine before.

Here is some relevant code: public class CurrencyConverter { public static String XML_SOURCE = "rss.timegenie.com/forex.gz"; public static long LAST_UPDATE=0; private static SQLiteHelper. DatabaseAdapter dbAdapter; private final static String TBL_NAME = "tbl_currency"; private final static String KEY_CODE = "code"; private final static String KEY_DESC = "description"; private final static String KEY_RATE = "rate"; private final static void createOrOpenDB(Context context) { SQLiteHelper dbHelper = new SQLiteHelper(); TableObjectMaker tableObjs = dbHelper. New TableObjectMaker(); tableObjs.

AddTable(TBL_NAME, new String { "_id integer primary key autoincrement", KEY_CODE + " text not null", KEY_DESC + " text not null", KEY_RATE + " real not null" }); dbAdapter = dbHelper. New DatabaseAdapter(context); dbAdapter. Open(tableObjs, "local_data", 1); } private final static void writeToDB(NodeList nodes) { if (nodes==null) return; List content = SQLiteHelper.

GetContentValues(nodes, new String {KEY_CODE,KEY_DESC,KEY_RATE}); for (ContentValues c:content) { dbAdapter. InsertRowInTable(TBL_NAME, c); } } private final static void closeDB() { dbAdapter.close(); } public static void updateDB(Context context) { Document xmlDocument = GZip. GetResourceAsXML(XML_SOURCE,true); NodeList nodes = xmlDocument.

GetElementsByTagName("data"); createOrOpenDB(context); writeToDB(nodes); closeDB(); LAST_UPDATE = Calendar.getInstance().getTimeInMillis(); } ... } The other class has identical database access/write methods as above, except with its own table name and field names, and it uses the same "local_data" name for the database name. How can I get both classes to read and write to their own table in the same database? Java android database sqlite link|improve this question asked Dec 15 '11 at 18:30user10313124628 90% accept rate.

Move all access to the data base into a ContentProvider. Each class can then interact with the ContentProvider, which then interacts with the database.

– user1031312 Dec 15 '11 at 18:52 @user1031312 - No, it's not necessary. I suggested it because it might simplify your code to separate the database interactions into a separate component of the app. – Ted Hopp Dec 15 '11 at 19:24 I've already done that with my SQLiteHelper class.

I also read a tutorial on the ContentProvider but it seems as so a contentprovider only creates one table, and I would need to create a separate contentprovider class for each table. So I'd rather create a different database name (which i'm doing for now) than use contentproviders. – user1031312 Dec 15 '11 at 22:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions