游标始终从SQL查询返回0

时间:2013-12-13 18:34:58

标签: android sqlite

我一直在尝试从SQL查询返回值,但它始终具有值0,而不是null。我试图在Google和SO上找到答案,但我什么都没找到。

此外,数据库正在运行,因为它可以使用数据库本身的数据填充ListView。

继承我的代码:

BeaverDetailsDB

private SQLiteDatabase db;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
        MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_NIGHTS_AWAY };
private String[] NightsAway = { MySQLiteHelper.COLUMN_NIGHTS_AWAY };

public BeaverDetailsDB(Context context) {
    dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
    db = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}




public DetailsSQLClass createName(String Name) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_NAME, Name);
    long insertId = db.insert(MySQLiteHelper.TABLE_NAME, null, values);
    Cursor cursor = db.query(MySQLiteHelper.TABLE_NAME, allColumns,
            MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null,
            null);
    cursor.moveToFirst();
    DetailsSQLClass newName = cursorToName(cursor);
    cursor.close();
    return newName;
}

public void createNightsAway(Integer TotalNightsAway) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_NIGHTS_AWAY, TotalNightsAway);
    db.insert(MySQLiteHelper.TABLE_NAME, null, values);
    return;
}





public List<DetailsSQLClass> getAllNames() {
    List<DetailsSQLClass> Names = new ArrayList<DetailsSQLClass>();
    Cursor cursor = db.query(MySQLiteHelper.TABLE_NAME, allColumns, null,
            null, null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        DetailsSQLClass Name = cursorToName(cursor);
        Names.add(Name);
        cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return Names;
}



public Integer getNightsAway(String Name) {
    dbHelper.getReadableDatabase();
    int value = 9999;
    Cursor c = db.query(MySQLiteHelper.TABLE_NAME, NightsAway,
    "Name" + " =  ?" ,new String[] {Name} , null, null, null, null);
    int x = c.getColumnIndex(MySQLiteHelper.COLUMN_NIGHTS_AWAY);
    if (c.moveToFirst()) {
        value = c.getInt(x); 
        int b = c.getCount();
    } else if (c.isNull(0)) {
        value = 55675; // Check if Column 0 is empty/null
    }
    c.close();
    dbHelper.close();
    return value;
}

public String DbQuery(String Name) {
    db = dbHelper.getReadableDatabase();
    Cursor c = db.query(MySQLiteHelper.TABLE_NAME, null, "Name" + "='"
            + Name + "'", null, null, null, null);
    String result;
    c.moveToFirst();
    result = c.getString(0);
    c.close();
    dbHelper.close();
    return result;
}

private DetailsSQLClass cursorToName(Cursor cursor) {
    DetailsSQLClass Name = new DetailsSQLClass();
    Name.setId(cursor.getLong(0));
    Name.setName(cursor.getString(1));
    return Name;
}

}

MySQLiteHelper

public class MySQLiteHelper extends SQLiteOpenHelper {
    public static final String TABLE_NAME = "sqlBeaverDetails";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_NAME = "Name";
    public static final String COLUMN_NIGHTS_AWAY = "NightsAway";

    public static final String DATABASE_NAME = "sqlBeaverDetails.db";
    public static final int DATABASE_VERSION = 1;

    public static final String DATABASE_CREATE = ("CREATE TABLE " + TABLE_NAME + 
            " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_NAME + " STRING, " + 
            COLUMN_NIGHTS_AWAY + " INTEGER );");

    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

}

AddBeaverDetailsActivity

public class AddBeaverDetailsActivity extends Activity {

EditText etName;
EditText etNightsAway;

private BeaverDetailsDB datasource;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_beaver_details);


     datasource = new BeaverDetailsDB(this); datasource.open(); 
     etName = (EditText) findViewById(R.id.etBeaverName);
     etNightsAway = (EditText) findViewById(R.id.etNightsAway);}

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void bSQLEnter(View v) { 
    String Name;
    String NightsAway;

    Name = etName.getText().toString();
    NightsAway = etNightsAway.getText().toString();
    int TotalNightsAway = Integer.parseInt(NightsAway);

    datasource.createName(Name);
    datasource.createNightsAway(TotalNightsAway);

    Intent i = new Intent(getApplicationContext(),  ViewBeaverDetails.class);
    startActivity(i);

}

public void showDatePickerDialog(View v) {
    Toast.makeText(getApplicationContext(), "You clicked me!",
            Toast.LENGTH_SHORT).show();

}

}

DetailsS​​QLClass

public class DetailsSQLClass {
private long id;
private String Name;
private String NightsAway;

public long getId(){
    return id;
    }
public void setId(long id){
    this.id = id;
}

public String getName(){
    return Name;
    }
public void setName(String Name){
    this.Name = Name;
}

public String getNightsAway(){
    return NightsAway;
    }
public void setNightsAway(String NightsAway){
    this.NightsAway = NightsAway;
}

// Used for the ArrayAdapter
@Override
public String toString(){
    return Name;
}

}

BeaverDetailsPage

public class BeaverDetailsPage extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_beaver_details);

    String title = "";
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        title = extras.getString("BeaverName");}
    setTitle(title);

    BeaverDetailsDB db = new BeaverDetailsDB(getApplicationContext());

    TextView tv = (TextView) findViewById(R.id.tvBDetailsNightsAway);

    db.open();
    Integer value = db.getNightsAway(title);
    tv.setText(String.valueOf(value));
    db.close();

}

}

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个

if (c.moveToFirst()) {
            do {
                value = c.getInt(0);
            } while (c.moveToNext());
        }

对于使用上述代码后出现类似问题的其他人,请下载SQLite Database Browser 2.0 b1,然后使用DDMS移出应用程序数据库,并确保数据库中的值正确。