SQL DB只持有一行?

时间:2011-10-26 17:45:34

标签: java android sql

使用数据库的新手,所以不确定我的错误在哪里诚实。这是我的代码:

创建数据库

    private static final String DATABASE_CREATE =
    "create table job_details (_id integer primary key autoincrement, "
    + "company text not null, advertised text, status text not null, position text not null, " +
    "wage text, hours text, address text, " +
    "email text, number text, notes text);";

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(DATABASE_CREATE);
    }

创建一行

     public long createJob(String company, String advertised, String status, String position, String wage, String hours,
        String address, String email, String number, String notes) {

    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_COMPANY, company);
    initialValues.put(KEY_ADVERTISED, advertised);
    initialValues.put(KEY_STATUS, status);
    initialValues.put(KEY_POSITION, position);
    initialValues.put(KEY_WAGE, wage);
    initialValues.put(KEY_HOURS, hours);
    initialValues.put(KEY_ADDRESS, address);
    initialValues.put(KEY_EMAIL, email);
    initialValues.put(KEY_NUMBER, number);
    initialValues.put(KEY_NOTES, notes);

    return mDb.insert(DATABASE_TABLE, null, initialValues);
}

将光标移动到表格中的所有行

     public Cursor fetchAllJobs() {

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_COMPANY,
            KEY_ADVERTISED, KEY_STATUS, KEY_POSITION, KEY_WAGE, KEY_HOURS,
            KEY_ADDRESS, KEY_EMAIL, KEY_NUMBER, KEY_NOTES}, null,
            null, null, null, null);
}

使用游标记录所有行的一些属性

    private void getAllData(){
    Cursor c = mDbHelper.fetchAllJobs();
    startManagingCursor(c);
    do{
        if(c.moveToFirst()){
            String company;
            String position;
            String number;

            int companyColumn = c.getColumnIndex(JobsDbAdapter.KEY_COMPANY);
            int positionColumn = c.getColumnIndex(JobsDbAdapter.KEY_POSITION);
            int numberColumn = c.getColumnIndex(JobsDbAdapter.KEY_NUMBER);

            do{
                company = c.getString(companyColumn);
                position = c.getString(positionColumn);
                number = c.getString(numberColumn); 
            }while(c.moveToNext());

            Log.d(TAG, "Company is = "+company);
            Log.d(TAG, "Position is = "+position);
            Log.d(TAG, "Number is = "+number);
        }
    }while(c.moveToNext());
}

当我使用此代码时,输​​出/记录的值只是我尝试在数据库中输入的最后一行中包含的值。我不确定我输入的数据是错误的,还是查找错误的数据库。

3 个答案:

答案 0 :(得分:4)

看看你的循环。

do{
    if(c.moveToFirst()){
        String company;
        String position;
        String number;

        int companyColumn = c.getColumnIndex(JobsDbAdapter.KEY_COMPANY);
        int positionColumn = c.getColumnIndex(JobsDbAdapter.KEY_POSITION);
        int numberColumn = c.getColumnIndex(JobsDbAdapter.KEY_NUMBER);

        do{
            company = c.getString(companyColumn);
            position = c.getString(positionColumn);
            number = c.getString(numberColumn); 
        }while(c.moveToNext());

        Log.d(TAG, "Company is = "+company);
        Log.d(TAG, "Position is = "+position);
        Log.d(TAG, "Number is = "+number);
    }
}while(c.moveToNext());

您可以将光标视为翻书中的页面。在这个块中:

do{
            company = c.getString(companyColumn);
            position = c.getString(positionColumn);
            number = c.getString(numberColumn); 
        }while(c.moveToNext());

您将页面翻到最后一页。所以当你登录时,你就是最后一个。

答案 1 :(得分:2)

这是不正确的:

 do{
            company = c.getString(companyColumn);
            position = c.getString(positionColumn);
            number = c.getString(numberColumn); 
        }while(c.moveToNext());

当您想要读取光标实际指向的内容时,您无需移动它。光标指向完整记录。

此外,您的if语句始终将光标移回第一个结果。这可能就是你只看到第一排的原因。试试这个。

private void getAllData(){
  Cursor c = mDbHelper.fetchAllJobs();
  startManagingCursor(c);

  while(c.moveToNext()){

        String company;
        String position;
        String number;

        int companyColumn = c.getColumnIndex(JobsDbAdapter.KEY_COMPANY);
        int positionColumn = c.getColumnIndex(JobsDbAdapter.KEY_POSITION);
        int numberColumn = c.getColumnIndex(JobsDbAdapter.KEY_NUMBER);


        company = c.getString(companyColumn);
        position = c.getString(positionColumn);
        number = c.getString(numberColumn); 


        Log.d(TAG, "Company is = "+company);
        Log.d(TAG, "Position is = "+position);
        Log.d(TAG, "Number is = "+number);
    }
}

答案 2 :(得分:0)

您确定问题出在数据库中而不是您的循环吗?我会尝试手动连接到数据库并在该表上执行SELECT。我认为有一些时髦的东西可能会继续你的光标和你的循环,使它不打印所有的行。