So I do not have any issues inserting information into my table, but when I try to query my database, I seem to have issues retrieving it. I am new to SQLite and databases in general so maybe my understanding is different. Here are the declarations for my constants:
public static abstract class TableInfo implements BaseColumns
{
//User Table
public static final String USER_NAME = "user_name";
public static final String USER_PASS = "user_pass";
public static final String USER_FNAME = "user_fname";
public static final String USER_LNAME = "user_lname";
public static final String DATABASE_NAME = "current_users";
public static final String USER_TABLE = "user_info";
//Message Table
public static final String MESSAGE_ID = "msg_id";
public static final String RECEIVER_ID = "receiver_id";
public static final String SENDER_ID = "sender_id";
public static final String MESSAGE = "message";
public static final String STATUS = "status";
public static final String MSG_TABLE = "msg_info";
This is my query string to create the table (which works)
public String CREATE_MSG_TABLE = "CREATE TABLE " + TableData.TableInfo.MSG_TABLE +
" ( " + TableData.TableInfo.MESSAGE_ID + " INTEGER PRIMARY KEY, "
+ TableData.TableInfo.RECEIVER_ID + " TEXT, "
+ TableData.TableInfo.SENDER_ID + " TEXT, "
+ TableData.TableInfo.MESSAGE + " TEXT, "
+ TableData.TableInfo.STATUS + " INTEGER);";
public void onCreate(SQLiteDatabase sdb){
sdb.execSQL(CREATE_USER_TABLE);
Log.d("Database operations", "Table1 created");
sdb.execSQL(CREATE_MSG_TABLE);
Log.d("Database operations", "Table2 created");
}
This is the method I use to query the database for my information. It is a messaging application so I want the receiver_id and message:
public Cursor getMessages(DatabaseOperations dop, String receiver)
{
SQLiteDatabase SQ = dop.getWritableDatabase();
Cursor CR = SQ.rawQuery("SELECT "
+ TableData.TableInfo.RECEIVER_ID + ", "
+ TableData.TableInfo.SENDER_ID + ", "
+ TableData.TableInfo.MESSAGE + ", "
+ TableData.TableInfo.STATUS
+ " FROM " + TableData.TableInfo.MSG_TABLE + " WHERE "
+ TableData.TableInfo.RECEIVER_ID + "= '"
+ receiver + "'" , null);
return CR;
}
And finally, this what I do when I return with the cursor:
private void loadMessages()
{
DatabaseOperations DOP = new DatabaseOperations(CTX);
//The cursor will iterate through the DB
Cursor CR = DOP.getInformationMsgTable(DOP);
if(CR.getCount() > 0)
{
CR.moveToFirst();
String[] fromFieldNames = new String[]
{
TableData.TableInfo.RECEIVER_ID,
TableData.TableInfo.MESSAGE
};
int[] toViewIDs = new int[]
{
R.id.userNameFromDB,
R.id.messageInfoDB
};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(
CTX,
R.layout.item_layout,
CR,
fromFieldNames,
toViewIDs);
ListView myList = (ListView) findViewById(R.id.friendsLV);
myList.setAdapter(myCursorAdapter);
}
}
I have inserted multiple rows into the table, I log everytime I do. But the error I get is "java.lang.IllegalArgumentException: column '_id' does not exist", which is referring to the receiver_id column. Any thoughts? I find it strange that it cuts out the part before the '_'. Also, when the table is empty, I can use the load message function and not have any issues, but when I add a row to the table THEN query the table, I get issues and errors. Thanks.
Aucun commentaire:
Enregistrer un commentaire