rawQuery未被接受

时间:2013-10-11 19:17:49

标签: android database sqlite

下面是我在sqlite中创建数据库的方式:

public class DBAdapter {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_FIRST = "first_name";
    public static final String KEY_SECOND = "second_name";
    public static final String KEY_SALARY = "salary";
    public static final String KEY_OVERTIME = "overtime";
    public static final String KEY_TOTAL = "total";

    private static final String TAG = "DBAdapter";
    private static final String DATABASE_NAME = "myDatabase";
    private static final String DATABASE_TABLE = "Info";

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_CREATE = "create table Info (_id integer primary key autoincrement, "
    + "first_name text not null, last_name text not null, salary real not null, overtime real null, total real not null);";

    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

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

    //---opens the database---
    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();

        return this;
    }

    //---closes the database---
    public void close()
    {
        DBHelper.close();
    }

    //---insert a History into the database---
    public long insertHistory(String source, String destination, String distance, String charge_day, String charge_night)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_FIRST, first_name);
        initialValues.put(KEY_SECOND, last_name);
        initialValues.put(KEY_SALARY, salary);
        initialValues.put(KEY_OVERTIME, overtime);
        initialValues.put(KEY_TOTAL, total);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular History---
    public boolean deleteHistory(long rowId)
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    //---retrieves all the history---
    public Cursor getAllHistory()
    {
        return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_FIRST,KEY_SECOND, KEY_SALARY, KEY_OVERTIME, KEY_TOTAL}, null, null, null, null, null);
    }

    //---retrieves a particular History---
    public Cursor getHistory(String first_name, String last_name) throws SQLException
    {
        //SQLiteDatabase db = this.getReadableDatabase();
        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_FIRST, KEY_SECOND, KEY_SALARY, KEY_OVERTIME, KEY_TOTAL}, KEY_ROWID + "=" + destination, null,null, null, null, null);
        if (mCursor != null) 
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
}

现在我从mainActivity插入输入。代码如下:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText et1 = (EditText) findViewById(R.id.editText1);
        final EditText et2 = (EditText) findViewById(R.id.editText2);
        Button b = (Button) findViewById(R.id.button1);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Second.class);
                intent.putExtra("first_name", et1.getText().toString());
                intent.putExtra("second_name", et2.getText().toString());
                startActivity(intent);
            }
        });

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

现在,我必须在下一个活动的列表视图中显示基于first_name和last_name作为输入的项目。我使用的代码如下:

public class Second extends Activity{

    String first_name, last_name;
    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        DBAdapter db = new DBAdapter(this);

        ListView listView = (ListView) findViewById(R.id.listView1);

        db.open();
        first_name = getIntent().getExtras().getString("first_name");
        last_name = getIntent().getExtras().getString("last_name");

        Cursor c;

        String select = "SELECT * FROM Info WHERE first_name = ? AND last_name = ?";
        String[] whereArgs = {first_name, last_name};
        c = db.rawQuery(select, whereArgs); //this statement is not accepted. Why?
        final ArrayList<String> temparr = new ArrayList<String>();

        if (c.moveToFirst())
        {
            do{
                temparr.add("Your details are as follows:" + '\n' + '\n' + "First_name: " +c.getString(1)+ '\n' + "Second_name: " + c.getString(2) + '\n' + "Salary: "+ c.getString(3)+ "\n" + "Overtime: "+ c.getString(4) +'\n'+ "Total: " +c.getString(5));
            }while(c.moveToNext());
             ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, temparr);
             listView.setAdapter(adapter);

        }else{
            temparr.add("NO DETAILS!!!");
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, temparr);
            listView.setAdapter(adapter);
        }
        db.close();
    }
}

为什么没有使用rawQuery?请解释原因。我是android的初学者,刚开始。我想我需要使用SQLiteDatabase进行实例化但是如何实现?

1 个答案:

答案 0 :(得分:0)

DBAdapter不是具有rawQuery()方法的SQLiteDatabase实例。

您可以在DbAdapter中添加getDB()方法以获取SQLiteDatabase实例。或者DbAdapter类中的方法,如:

    public  Cursor getSepficItem(){
       return  db.rawQuery("...");
    }

然后在您的活动课程中,您可以:

Cursor c=db.getSpeficItem();