Question

I'm having trouble creating a class that create and manipulate the database! But the method create () has a problem saying

"The method openOrCreateDatabase(String, int, null) is undefined for the type BancoDeDados".

Anyone have any suggestions to overcome this problem? My code is as follows:

package com.example;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class DataBase{
     private SQLiteDatabase db;
     private String DATABASE_NAME;
     private String TABLE_NAME;
     private String SQL_SELECT_ALL;
     private String SQL_SELECT_ID;
     private String SQL_CREATE;

     public DataBase(SQLiteDatabase db,String DATABASE_NAME,String TABLE_NAME,String SQL_SELECT_ALL,String SQL_SELECT_ID,String SQL_CREATE){
         this.db = db;
         this.DATABASE_NAME = DATABASE_NAME;
         this.TABLE_NAME = TABLE_NAME;
         this.SQL_SELECT_ALL = SQL_SELECT_ALL;
         this.SQL_SELECT_ID = SQL_SELECT_ID;
         this.SQL_CREATE = SQL_CREATE;
     }

     public void create(){
         this.db = openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
         this.db.execSQL(this.SQL_CREATE);
         this.db.close(); 
     }
}

No correct solution

OTHER TIPS

SQLiteOpenHelper is used to create a database in Android. You need to override the onCreate method after extending this class. db.execSQL(TABLE_CREATE); in onCreate creates the database. TABLE_CREATE is your DDL String.

public class DbHelper extends SQLiteOpenHelper {

     //The database name and version can be parameterized in the constructor.
     public DbHelper(Context context, String databaseName, int databaseVersion) {
         super(context, databaseName, null, databaseVersion);
     }



    /*
     * (non-Javadoc)
     * 
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
     * .SQLiteDatabase)
     */
    @Override
    public void onCreate(SQLiteDatabase db) {

        try {
            // Create Database
            db.execSQL(TABLE_CREATE);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
     * .SQLiteDatabase, int, int)
     */
    @Override
    public void onUpgrade(SQLiteDatabase arg0, int oldVersion, int newVersion) {

    }



}

The main reason to create this class to create and manipulate database is repackage it and besides this class EXTENDS NO need no other!

Under My class after you solve the problem '. DataBase.java`

 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;

 public class DataBase{
 private String DATABASE_NAME;
 private int DATABASE_VERSION;
 private String TABLE_NAME;
 private String SQL_SELECT_ALL;
 private String SQL_SELECT_ID;
 private String SQL_CREATE;

 public DataBase(String DATABASE_NAME,int DATABASE_VERSION,String TABLE_NAME,String SQL_SELECT_ALL,String SQL_SELECT_ID,String SQL_CREATE){
     this.DATABASE_NAME = DATABASE_NAME;
     this.DATABASE_VERSION = DATABASE_VERSION;
     this.TABLE_NAME = TABLE_NAME;
     this.SQL_SELECT_ALL = SQL_SELECT_ALL;
     this.SQL_SELECT_ID = SQL_SELECT_ID;
     this.SQL_CREATE = SQL_CREATE;
 }
 public DataBase(String DATABASE_NAME,int DATABASE_VERSION,String TABLE_NAME,String SQL_SELECT_ALL,String SQL_CREATE){
     this.DATABASE_NAME = DATABASE_NAME;
     this.DATABASE_VERSION = DATABASE_VERSION;
     this.TABLE_NAME = TABLE_NAME;
     this.SQL_SELECT_ALL = SQL_SELECT_ALL;
     this.SQL_CREATE = SQL_CREATE;
 }

public void onCreate(Context ctx,SQLiteDatabase db){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE, null);
     db.execSQL(this.SQL_CREATE); //Criando tabela caso não exista!!
     db.close();
 }

public void onUpgrade(Context ctx,SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + this.TABLE_NAME);

    // Create tables again
    onCreate(ctx,db);
 }

 public long onWrite(Context ctx,SQLiteDatabase db,String row,ContentValues ctv){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     long lg = db.insert(this.TABLE_NAME,row,ctv);
     db.close();
     return lg;
 }

 public int onUpdate(Context ctx,SQLiteDatabase db,ContentValues ctv,String row,int id){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
      int x = db.update(this.TABLE_NAME, ctv, row, new String[]{String.valueOf(id)});
     db.close();
     return x;
 }

 public Cursor onSelecAll(Context ctx,SQLiteDatabase db){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     Cursor cursor = db.rawQuery(this.SQL_SELECT_ALL, null);
     return cursor;
 }

 public Cursor onSelecId(Context ctx,SQLiteDatabase db,int id){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     Cursor cursor = db.rawQuery(SQL_SELECT_ID, new String[]{String.valueOf(id)});
      return cursor;
  }

 public int onDelete(Context ctx,SQLiteDatabase db,String row,int id){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     int x = db.delete(this.TABLE_NAME, row, new String[]{String.valueOf(id)});
     db.close();
     return x;
 }

 public void onClose(Context ctx,SQLiteDatabase db){
     db = ctx.openOrCreateDatabase(this.DATABASE_NAME,Context.MODE_PRIVATE,null);
     db.close();
 }
  }

And in Main.java:

     import android...
     public class Main extends Activity {

 private SQLiteDatabase db;
 private Context ctx;
 private static final int DATABASE_VERSION = 1;
 private static final String DATABASE_NAME = "database.db";
 private static final String TABLE_NAME = "table";
 private static final String SQL_SELECT_ALL = "SELECT * FROM "+TABLE_NAME;
 private static final String SQL_SELECT_ID = "SELECT * FROM table WHERE _id = ?";
 /* SQL de criação do banco de dados. */
 private static final String SQL_CREATE = "CREATE TABLE IF NOT EXISTS table(" +
                    "_id INTEGER PRIMARY KEY, " +
                    "person VARCHAR(30), " 
                    "animal VARCHAR(30))";
 BancoDeDados BD = new BancoDeDados(DATABASE_NAME,DATABASE_VERSION,TABLE_NAME,SQL_SELECT_ALL,SQL_SELECT_ID,SQL_CREATE);
  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  
    ctx = getBaseContext(); //Context to use in class BancoDeDados
    BD.onCreate(ctx, db); // Create BD and table if needed!! 
   /* .... */
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top