i'd appreciate any help with the following: SOLVED I AM AN IDIOT! - i had 2 log tags, one in interface and another in activity. i monitored the interface tag in debug, thinking it included the activity tag. hence i only saw the result in the interface and not the results for the activity. i therefore assumed that the activity was not "getting the return from the interface" - sorry. thanks to those who tried to help.
i call a search ( dbHelper.getNumberOfRecords(); - to get the number of records - and - dbHelper.findManyRecords(aNameDateRecord, "name", "Sarah"); - to get a number of records out the database) on a sqlite database from an activity via a databaseHelper and another "interface" class. the search works (i can see the results in the debug "TAG". - the problem is that the results should be "returned" to the calling activity but they are'nt. any help on why this is happening will be much appreciated. thank you. please see code below:
part of "interface" class...
public class DatabaseInterface{
private static final String TAG = "HelloDatePicker";
private static final String TableName = "t_namesAndDates";
private static final String RowId = "_id";
private static final String Col_name = "name";
private static final String Col_date = "date";
private Context context;
private DatabaseHelper dbHelper;
private SQLiteDatabase database;
public DatabaseInterface(Context context){
this.context = context;
}
//open the database
public DatabaseInterface open(){
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
//close the database
public DatabaseInterface close(){
dbHelper.close();
return this;
}
//get number of records in database
public int getNumberOfRecords(){
open();
String sql = "SELECT * FROM " + TableName;
Cursor cursor = database.rawQuery(sql, null);
int numberOfRecords = cursor.getCount();
cursor.close();
close();
Log.i(TAG, "HelloDatePicker-getNumberOfRecords **** " + numberOfRecords);
return numberOfRecords;
}
//search for many records
public Cursor findManyRecords(NameDateRecord aNameDateRecord, String columnName, String whereClause){
open();
Log.i(TAG, "HelloDatePicker-column aNameDateRecord = " + aNameDateRecord + " columnName = " + columnName + " whereClause = " + whereClause);
/*
String selection = columnName;
Log.i(TAG, "HelloDatePicker-column name = " + columnName + " whereClause = " + whereClause);
String[] selectionArgs = new String[] {"Sarah"};//searches on a name
String[] columns = new String[] {"_id","name","date"};
for(int i=0;i<selectionArgs.length;i++)
Log.i(TAG, "HelloDatePicker-selectionArgs = " + selectionArgs[i]);
//Cursor cursor = database.query(TableName, columns, " name = ", selectionArgs, null, null, null);
*/
String sql = "SELECT * FROM " + TableName + " WHERE name = 'Sarah'";
Cursor c = database.rawQuery(sql, null);
Log.i(TAG, "HelloDatePicker-getNumberOfRecords @@@@@@ " + c.getCount());
c.moveToFirst();
while (c.moveToNext()) {
// Your code
Log.i(TAG, "HelloDatePicker- id = " + c.getInt(c.getColumnIndex(RowId)) + " name " + c.getString(c.getColumnIndex(Col_name)) + " date = " +
c.getInt(c.getColumnIndex(Col_date)));
}
Log.i(TAG, "HelloDatePicker-findManyRecords");
close();
return c;
}
part of helper class...
public class DatabaseHelper extends SQLiteOpenHelper{
private static final String TAG = "HelloDatePicker";
private static final String DBname = "db_date";
private static final String TableName = "t_namesAndDates";
private static int versionNumber = 1;
//the constructor
public DatabaseHelper(Context context) {
super(context, DBname, null, versionNumber);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase database) {
//create table once on installation of app
String columns = "(_id integer primary key autoincrement,name text not null,date integer)";
String sql = "CREATE TABLE " + TableName + columns;
Log.i(TAG, "HelloDatePicker-create database-sql = " + sql);
database.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Logs that the database is being upgraded
Log.i(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
// Kills 开发者_如何学Gothe table and existing data
database.execSQL("DROP TABLE IF EXISTS notes");
// Recreates the database with a new version
onCreate(database);
}
}
and finally the activity where the searches were called from...
public class HelloDatePicker extends Activity {
private static final String TAG = "datepicker";
private static final String RowId = "_id";
private static final String Col_name = "name";
private static final String Col_date = "date";
private TextView mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
private DatabaseInterface dbHelper;
NameDateRecord aNameDateRecord;
static final int DATE_DIALOG_ID = 0;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "in onCreate HelloDatePicker");
dbHelper = new DatabaseInterface(this);
//to get number of records in database
int numberRecords = dbHelper.getNumberOfRecords();
Log.i(TAG, "number of records in database &&&&&& " + numberRecords);
//to search for records
dbHelper = new DatabaseInterface(this);
Cursor c = dbHelper.findManyRecords(aNameDateRecord, "name", "Sarah");
Log.i(TAG, "HelloDatePicker-getNumberOfRecords +++++ " + c.getCount());
c.moveToFirst();
while (c.moveToNext()) {
// Your code
Log.i(TAG,"HelloDatePicker- id = " + c.getInt(c.getColumnIndex(RowId)) + " name "
+ c.getString(c.getColumnIndex(Col_name))
+ " date = " + c.getInt(c.getColumnIndex(Col_date)));
thanks in advance.
regards
It looks like you are returning the cursor after closing the database, maybe this invalidates the cursor.
This is bad SQL-Style:
String sql = "SELECT * FROM " + TableName;
Cursor cursor = database.rawQuery(sql, null);
int numberOfRecords = cursor.getCount();
Use the "count"-function in stead! Also, why are you creating "dbHelper" twice? And this:
String sql = "CREATE TABLE " + TableName + columns;
database.execSQL(sql);
is garbage. Use "rawQuery()", "query()" or "PreparedStatement"-Object to create your Query's.
Last but not least, in your "findManyRecords"-method, you go through the whole Cursor and then return it. So the Cursor is at the end of the returned field. Don't do this or use the "moveToFirst"-method.
精彩评论