2013-10-11 2 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가 사용되지 않습니까? 그 이유를 설명해주십시오. 나는 안드로이드에서 초보자이며 막 시작했습니다. 나는 내가 SQLiteDatabase를 사용하여 instatiate해야한다고 알았지 만 어떻게?

답변

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