Android如何查找数据库及其表?

时间:2014-04-02 11:09:13

标签: android sqlite

您好我对Android开发很新,我想问一下如果能够找到并查看数据库,如果它是如下创建的?通过添加,查看记录,代码工作正常,但问题是我无法找到数据库。

 public class db extends Activity
 {
 SQLiteDatabase mydb;
 TextView name,review;
 Button save,myview;
 ListView lv;
  ArrayList data;
  @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.db);

 name=(TextView)findViewById(R.id.entername);
 save=(Button) findViewById(R.id.bsave);
 review = (TextView)findViewById(R.id.enterreview);



 lv=(ListView) findViewById(R.id.list);
 lv.setOnItemClickListener(new OnItemClickListener()
  {

   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
 Toast.makeText(getApplicationContext(), data.get(arg2).toString(),3000).show();
}

 });

  mydb = db.this.openOrCreateDatabase("mydatabase", MODE_PRIVATE, null);
  mydb.execSQL("CREATE TABLE IF NOT EXISTS test2 (id INTEGER PRIMARY KEY AUTOINCREMENT,name    
  varchar,review varchar);");

//=== SAVE BUTTON FOR INSERTING DATA TO DATABASE =======

  save.setOnClickListener(new View.OnClickListener()
  {

 public void onClick(View v)
  {
 mydb.execSQL("insert into test2 (name,review) values(?,?);", new String[]             
    {name.getText().toString(), review.getText().toString(), });

  Toast.makeText(getApplicationContext(), "DATA INSERTED", 3000).show();

 }
});

   myview=(Button)findViewById(R.id.bview);
   data=new ArrayList();

  //=VIEW BUTTON TO SHOW DATA INSERTED IN THE LISTVIEW=======

  myview.setOnClickListener(new View.OnClickListener()
 {

 public void onClick(View v)
 {
 Toast.makeText(getApplicationContext(), "Review Added", 5000).show();
 Cursor cursor2=mydb.rawQuery("SELECT * FROM test2;", null);

if  (cursor2.moveToFirst())
  {
Toast.makeText(getApplicationContext(), "Review's are:", 5000).show();
 data.clear();
 do
{
 data.add(cursor2.getString(cursor2.getColumnIndex("name")));
 data.add(cursor2.getString(cursor2.getColumnIndex("review")));

  }
 while (cursor2.moveToNext());


  //====CODE FOR SHOWING DATA AS A SIMPLE LIST ITEM=========================================

  ArrayAdapter <String> adapter=new ArrayAdapter<String>   
  (db.this,android.R.layout.simple_list_item_1,data);
  lv.setAdapter(adapter);
 }
else
  {
  Toast.makeText(getApplicationContext(), "There are no reviews available", 3000).show();
  }
  cursor2.close();
 }
 });

  }

  }

1 个答案:

答案 0 :(得分:0)

您可以使用此方法:

public boolean isDatabaseExist()
{
    SQLiteDatabase checkDB = null;
    try {
        checkDB = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
        checkDB.close();
        return true;
    }
    catch(SQLiteException e) {
        // Log an info message stating database doesn't exist.
    }

   return false;
}