SQLite资产数据库JAVA ANDROID

SQLite资产数据库JAVA ANDROID,android,database,sqlite,assets,sqliteopenhelper,Android,Database,Sqlite,Assets,Sqliteopenhelper,我正试图从assets文件夹中获取一个sqlite数据库,以便在系统中创建一个新的数据库,因此当您在设备上安装应用程序时,它会预加载一些数据-插入项-,以及表表单-创建表-。 我创建了一个名为“SQLDataBase”的类,该类从SQLiteOpenHelper扩展而来,其主体如下: public class SQLDataBase extends SQLiteOpenHelper { public static final String KEY_TITULO = "titulo"; priva

我正试图从assets文件夹中获取一个sqlite数据库,以便在系统中创建一个新的数据库,因此当您在设备上安装应用程序时,它会预加载一些数据-插入项-,以及表表单-
创建表-
。
我创建了一个名为“
SQLDataBase
”的类,该类从
SQLiteOpenHelper
扩展而来,其主体如下:

public class SQLDataBase extends SQLiteOpenHelper {
public static final String KEY_TITULO = "titulo";
private static final String KEY_COD="id";
private static final String KEY_GENERO="genero";
private static final String KEY_DIRECTOR="director";
private static final String KEY_DURACION="duracion";
private static final String TABLA_BASEDATOS = "peliculas";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static final String DATABASE_NAME = "peliculass"; 
public final static String DATABASE_PATH ="/data/data/com.bdsqlite/databases/";
public static final int DATABASE_VERSION = 1;

String sqlCreate ="create table peliculas (id integer primary key autoincrement, titulo text not null, director text not null,genero text not null, duracion int not null)";

public SQLDataBase(Context contexto) {
    super(contexto, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = contexto;
}


public void createDatabase() throws IOException
{

      boolean dbExist = checkDataBase();

      if(dbExist)
      {
            Log.v("DB Exists", "db exists");
      }

      boolean dbExist1 = checkDataBase();
      if(!dbExist1)
      {
            this.getReadableDatabase();
            try
            {
                  this.close();    
                  copyDataBase();
            }
            catch (IOException e)
            {
                  throw new Error("Error copiando base de datos");
            }
      }

}

//Comprobamos si la base de datos existe ya
private boolean checkDataBase()
{
      boolean checkDB = false;
      try
      {
            String myPath = DATABASE_PATH + DATABASE_NAME;
            File dbfile = new File(myPath);
            checkDB = dbfile.exists();
      }
      catch(SQLiteException e)
      {
      }
      return checkDB;
}
//Copia la base de datos de la carpeta assets a la recien creada y vacia base de datos del sistema
private void copyDataBase() throws IOException
{
      String outFileName = DATABASE_PATH + DATABASE_NAME;

      OutputStream myOutput = new FileOutputStream(outFileName);
      InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

      byte[] buffer = new byte[1024];
      int length;
      while ((length = myInput.read(buffer)) > 0)
      {
            myOutput.write(buffer, 0, length);
      }
      myInput.close();
      myOutput.flush();
      myOutput.close();
}

//elimina la base de datos
public void db_delete()
{
      File file = new File(DATABASE_PATH + DATABASE_NAME);
      if(file.exists())
      {
            file.delete();
            System.out.println("Base de datos eliminada.");
      }
}

//Abrir base de datos
public void openDatabase() throws SQLException
{
      String myPath = DATABASE_PATH + DATABASE_NAME;
      myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public synchronized void closeDataBase()throws SQLException
{
      if(myDataBase != null)
            myDataBase.close();
      super.close();
}

public void onCreate(SQLiteDatabase db)
{
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{    
      if (newVersion > oldVersion)
      {
            Log.v("Database Upgrade", "Database version higher than old.");
            db_delete();
      }
}


public void insertarContacto(String titulo, String director, String genero, int duracion) {
    ContentValues valoresIniciales = new ContentValues();
    valoresIniciales.put(KEY_TITULO, titulo);
    valoresIniciales.put(KEY_DIRECTOR, director);
    valoresIniciales.put(KEY_GENERO, genero);
    valoresIniciales.put(KEY_DURACION, duracion);

    //return db.insert(TABLA_BASEDATOS, null, valoresIniciales);
    myDataBase.execSQL("INSERT INTO peliculas(titulo,director,genero,duracion) VALUES (titulo,director,genero,duracion)");
}

 public Cursor obtenerTodasLasPeliculas() {
    return myDataBase.query(TABLA_BASEDATOS, new String[] { KEY_COD, KEY_TITULO,KEY_DIRECTOR,KEY_GENERO,KEY_DURACION}, null, null, null, null, null);
}
 public Cursor obtenerInfo(String nombre) {
     return myDataBase.query(TABLA_BASEDATOS,new String[] { KEY_DIRECTOR,KEY_GENERO,KEY_DURACION} , "titulo=?", new String[] { nombre }, null, null, null);

}

 public void cerrar() {
    myDataBase.close();
}

public Cursor obtenerPeliculasGenero(String genre) {

    return myDataBase.query(TABLA_BASEDATOS, new String[] { KEY_COD, KEY_TITULO,KEY_DIRECTOR,KEY_DURACION}, "genero=?", new String[]{ genre }, null, null, null);

}

public void EliminarPelicula(String titulo){
    myDataBase.execSQL("DELETE FROM peliculas WHERE titulo='"+titulo+"'");

}

public void ModificarPelicula(String titulo, String director, String genero, int duracion, String titulo2){
    myDataBase.execSQL("UPDATE peliculas SET titulo='"+titulo+"', director='"+director+"',genero='"+genero+"',duracion="+duracion+" WHERE titulo='"+titulo2+"'");

}
}
现在在我的主课上:

SQLDataBase basededatos;
在方法onCreate中:

basededatos=new SQLDataBase(this);
mostrarpelicula();

它实际上不起作用,它说:

 java.lang.NullPointerException at "return myDataBase.query(TABLA_BASEDATOS,
 new String[] { KEY_COD, KEY_TITULO,KEY_DIRECTOR,KEY_GENERO,KEY_DURACION}, null, null, null, null, null);"

你能帮我吗?

我的一个应用程序中有一个类似的设置,问题是数据库的名称

试着改变

私有静态最终字符串数据库\u NAME=“peliculass”


私有静态最终字符串数据库\u NAME=“peliculass.sqllite”//或者.sqllite3或任何扩展名

如果您手上有一个.db数据库,那么事情就会简单得多。可以将.sqlite数据库转换为.db数据库。安装后,当应用程序首次启动时,初始化SuperDatabase对象

SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.COPY_TO_SYSTEM);
对于后续的应用程序启动,请使用

SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.READ_FROM_DEVICE);
现在只需发出你不返回的查询

database.sqlInject("INSERT INTO food VALUES('Banana','Vitamin A');");
如果您有一些返回类型查询,那么可以以CSV、JSON或XML格式获取结果

作为CSV

ArrayList<String> rows=new ArrayList<String>();
rows=database.sqlEjectCSV("SELECT * FROM food;");
for (int i=0;i<rows.size();i++)
{
//Each row iterated with attribute values seperated by comma
}
或者作为XML

String xml=database.sqlEjectXML("SELECT * FROM food;");
要使用超级数据库,您需要将此库添加到Gradle中。以下是完整的文档:

我强烈建议您使用
SQLiteAssetHelper
,而不是自己滚动此代码:
String json=database.sqlEjectJSON("SELECT * FROM food;");
String xml=database.sqlEjectXML("SELECT * FROM food;");