Alright, so I am new to this whole Android thing, and I'm having a
little trouble with Databases. I've been able to figure out most of
Notepad examples, and when I copy and paste that database into my
application it works reasonably well. The problem is, I believe that
I need another table in my application and when I try to access the
functions referencing that table, Android crashes. This makes me
think that I am incorrectly creating the second table, but I can't
really see where I'm going wrong. Is there anything that I am
missing?
Thanks in advance!
***Here is the function that seems to be crashing***
private void fillPlayers() {
Cursor mPlayerCursor = temp_Db.fetchAllPlayers(); <--
This line works if you call fetchAllRosters()
startManagingCursor(mPlayerCursor);
String[] from = new String[]{DbTables.KEY_ROSTER};
int[] to = new int[]{R.id.text1};
ListAdapter rosters =
new SimpleCursorAdapter(this, R.layout.roster_row,
mPlayerCursor, from, to);
setListAdapter(rosters);
}
***Here is the Database Definition Code***
package com.android.USP;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DbTables {
public static final String KEY_ROSTER = "roster";
public static final String KEY_PLAYER = "player";
public static final String KEY_ROWID = "_id";
private static final String TAG = "DbRoster";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
// Database creation sql statement
private static final String DATABASE_CREATE = "" +
"create table roster (_id integer primary key
autoincrement, roster
text not null);" +
"create table player (_id integer primary key
autoincrement, player
text not null);";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE_ROSTER = "roster";
private static final String DATABASE_TABLE_PLAYER = "player";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
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 notes");
onCreate(db);
}
}
public DbTables(Context ctx) {
this.mCtx = ctx;
}
public DbTables open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createRoster(String roster) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ROSTER, roster);
return mDb.insert(DATABASE_TABLE_ROSTER, null, initialValues);
}
public Cursor fetchAllRosters() {
return mDb.query(DATABASE_TABLE_ROSTER, new String[] {
KEY_ROWID,
KEY_ROSTER }, null, null, null, null, null);
}
public long createPlayer(String roster) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_PLAYER, roster);
return mDb.insert(DATABASE_TABLE_PLAYER, null, initialValues);
}
public Cursor fetchAllPlayers() {
return mDb.query(DATABASE_TABLE_PLAYER, new String[] {
KEY_ROWID,
KEY_PLAYER }, null, null, null, null, null);
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---