我用java创建了一个mySQL数据库,我想要做的就是让类只有'getData()'方法中的'Cursor c'变量的一个实例。这样,无论何时调用getData()
方法,游标都可以从中断处继续读取数据库,而不是从头开始。
我尝试从方法中取出'Cursor c'并使其静止,但这会导致其他问题。
你能告诉我怎么做得好吗?
public class SQLClass {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "IncomeName";
public static final String KEY_AMOUNT = "IncomeAmount";
private static String DATABASE_NAME = "MoneyTracker";
private static String DATABASE_TABLE = "IncomeTable";
private static int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}//end DbHelper constructor
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT NOT NULL, " + KEY_AMOUNT + " TEXT NOT NULL);");
}//end onCreate method
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP IF EXISTS " + DATABASE_NAME);
onCreate(db);
}//end onUpgrade method
}//end class DbHelper
public SQLClass (Context c){
ourContext = c;
}//end constructor SQLClass
public SQLClass open()throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}//end method open
public void close(){
ourHelper.close();
}//end method close
public long createEntry(String name, String amount) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_AMOUNT, amount);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String[] getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_AMOUNT};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String[] result = new String[]{"","",""};
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iAmount = c.getColumnIndex(KEY_AMOUNT);
c.moveToNext();
result[0] = c.getString(iRow);
result[1] = c.getString(iName);
result[2] = c.getString(iAmount);
return result;
}//end method getData
}//end class SQLClass
答案 0 :(得分:0)
这是一个经典的java示例,如何在非静态类方法中访问静态字段:
public class MyClass {
private static int staticCursor;
{ // Initialize
staticCursor = 1;
}
public int getData(){
int retval;
retval = MyClass.staticCursor;
MyClass.staticCursor++;
return retval;
}
public static void main(String[] args) {
MyClass myClass1 = new MyClass();
MyClass myClass2 = new MyClass();
MyClass myClass3 = new MyClass();
System.out.println( myClass1.getData() );
System.out.println( myClass2.getData() );
System.out.println( myClass3.getData() );
System.out.println( myClass1.getData() );
}
}
run results:
====================
1
2
3
4
但是,如果您计划在多线程环境中运行的许多类实例之间共享游标变量,则需要仔细考虑同步和初始化内容,在线程之间共享游标可能导致难以诊断错误。
答案 1 :(得分:0)
感谢您帮助我了解如何在非静态方法中访问静态字段,但这不是我想要的。也许我没有问正确的问题。无论如何,我设法通过使getData()接受一个参数,int位置,用作光标的当前位置并将c.moveToNext()更改为c.moveToPosition(position)来解决问题
public String[] getData(int position) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_AMOUNT};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String[] result = new String[]{"","",""};
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iAmount = c.getColumnIndex(KEY_AMOUNT);
c.moveToPosition(position);
if (!c.isAfterLast()){
result[0] = c.getString(iRow);
result[1] = c.getString(iName);
result[2] = c.getString(iAmount);
}else if(c.isAfterLast()){
result[0] = "false";
}
return result;
}//end method getData