Android - Sqlite undefined database method type fot

I have a class in this application that needs to create and use a database, but Eclipse tells me that the sqlite methods are undefined.

It seems like this is a context issue, but I don't understand how to fix it, do I have a class extension 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();
          }
         });
     }
}

Thank you for your help!

+2
source share
2 answers

I suggest you use SQLiteOpenHelper.

It automatically takes care of creating the database if it is not available ...

code example:

 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 as follows:

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

It seems to me that it’s easier, and you don’t have to worry about whether the database already exists.

Hope this helps :)

EDIT:

... , , ... . :

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

:)

+2

Activity - , , , getdata:


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

Source: https://habr.com/ru/post/1791924/


All Articles