开发者

Can't create a database in list activity

I am trying to create a database in listactivity on a button click.. but I am getting an error

The method openOrCreateDatabase(String, int, null) is undefined for the type new View.OnClickListener(){}" and "DefaultDBHelper cannot be resolved.

Is there any way doing this in a listactivity? Please help !!!

my code :

reset.setOnClickListener(new OnClickListener() 
{
   private int count;
   @Override public void onClick(View v) 
   {
      SQLiteDatabase myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, SQLiteDatabase.OPEN_READWRITE, null);
      try 
      { 
          /* Create the Database (no Errors if it already exists) */
          // myDB = dbHelper.getWritableDatabase();
          // dbHelper.onCreate(myDB);
          myDB.execSQL("CREATE TABLE IF NOT EXISTS " 
                       + DefaultDBHelper.MY_DATABASE_TAB开发者_StackOverflowLE 
                       + " (LastName VARCHAR, FirstName VARCHAR," 
                       + " Country VARCHAR, Age INT(3));");

          myDB.execSQL("INSERT INTO " 
                       + DefaultDBHelper.MY_DATABASE_TABLE 
                       + " (LastName, FirstName, Country, Age)" 
                       + " VALUES ('name', 'title', 'country', 20);"); 
      }
      catch (SQLiteException e) { 
      } 
      finally { 
         if (myDB != null) 
            myDB.close(); 
      }
   }
});


u just change the code like this

SQLiteDatabase myDB = getBaseContext().openOrCreateDatabase(MY_DATABASE_NAME, SQLiteDatabase.OPEN_READWRITE, null);

i thought it will help u


 reset.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 SQLiteDatabase myDB =openOrCreateDatabase("DB_NAME", MODE_WORLD_WRITEABLE ,null);



            }
        });

The following code will work.I have tested it.The 'this' was refering to the OnClickListener object that you had created there.That was causing the compilation error.Taking out 'this' will solve the issue

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜