Marco Faion
Marco Faion

Reputation: 607

Android - Sqlite database method undefined fot type

i've a class in this app that should create and use a database, but Eclipse tell me that the sqlite methods are undefined.

Seem to be a context problem but i don't understand how to fix, have i to extend a different class instead of Activity?

package com.android.userdata;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

 public class getdata extends Activity {

  private final String MY_DATABASE_NAME = "DataStore";
        private final String MY_DATABASE_TABLE = "UserData";

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.userdata);

         SQLiteDatabase myDB = null;
            /* Create the Database (no Errors if it already exists) */
            this.createDatabase(MY_DATABASE_NAME, 1, MODE_PRIVATE, null);
            myDB = this.openDatabase(MY_DATABASE_NAME, null);

            myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                    + MY_DATABASE_TABLE
                    + " (LastName VARCHAR, FirstName VARCHAR,"
                    + " Country VARCHAR, Age INT(3));");

         Button btn = (Button) findViewById(R.id.confirm);
         btn.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
           Toast.makeText(getdata.this, "Hai Premuto il Pulsante", Toast.LENGTH_SHORT).show();
          }
         });
     }
}

Thanks for your help!

Upvotes: 2

Views: 5634

Answers (2)

Beasly
Beasly

Reputation: 1537

I suggest you use the SQLiteOpenHelper.

It cares automatically to create a DB if it's not available...

example code:

 public class DatabaseHelper extends SQLiteOpenHelper {
     private static final int DB_VERSION = 1;
     private static final String DB_NAME = "myDB.db";

     public DatabaseHelper(Context ctx) {
         super(ctx, DB_NAME, null, DB_VERSION);
     }

     @Override
     public void onCreate(SQLiteDatabase db) {
         String query = "CREATE TABLE myTable(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL)";
         db.execSQL(query);
         db.close();
     }

     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         ...
     }
}

Use it like this:

...
DatabaseHelper helper = new DatabaseHelper(context);
SQLiteDatabase db = helper.getWritableDatabase();
...

I think it's easier and you don't need to care about if the DB is existing already.

Hope this helps :)

EDIT:

I have an idea... maybe it is not possible to give this string like you are doing... try to build the string before. Like this:

String query = "YOUR SQL STATEMENT";
db.execSQL(query);
db.close();

Is a possibility :)

Upvotes: 2

Perry Hoekstra
Perry Hoekstra

Reputation: 202

If you need to keep your database material within an Activity extended class for whatever reason, you would define a private class like this within your getdata Activity class :


private static class DatabaseHelper extends SQLiteOpenHelper {
  .. see Beasly's code example
}    

Upvotes: 0

Related Questions