sayantan
sayantan

Reputation: 199

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_TABLE 
                       + " (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(); 
      }
   }
});

Upvotes: 2

Views: 1272

Answers (2)

rogerstone
rogerstone

Reputation: 7671

 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

Upvotes: 0

kannappan
kannappan

Reputation: 2250

u just change the code like this

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

i thought it will help u

Upvotes: 4

Related Questions