2013-10-11 39 views
0

是如何SQLite中創建我的數據庫:rawQuery是不被接受低於

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進行安裝,但是如何?

回答

0

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

您可以在您的DbAdapter中添加getDB()方法來獲取SQLiteDatabase實例。或在您將對DBAdapter類等的方法:

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

然後在您的活動類,你可以這樣做:

Cursor c=db.getSpeficItem(); 
+0

在邊注。發佈logcat,並說明如何不接受將更容易爲別人解決問題,而不是發佈所有的代碼。 – wtsang02

+0

抱歉。這不是問題..我會編輯它!我面臨的問題是它不會採用rawQuery。我編譯它只顯示數據庫的項目,我可以通過直接使用遊標來完成。但是,當我使用rawQuery來提取特定項目時出現問題! – user2761022

+0

在rawQuery中有一個錯誤,它表示:方法rawQuery(String,String [])未定義類型DBAdapter – user2761022