删除并重新安装应用程序时,从资产文件夹中执行sqlite中的查询时光标出错

时间:2017-04-20 04:23:53

标签: android sqlite android-studio assets android-assets

我只有在卸载应用程序并重新安装时才从资源文件夹中查询sqlite数据库时遇到问题。如果是第一次安装,应用程序正常运行没有任何问题。在错误列表中显示错误在光标中,但我不明白原因,因为当我第一次安装应用程序时不会发生这种情况。当我从数据文件夹中删除数据时,它将恢复正常操作。

附加代码:

    public class MainActivity extends AppCompatActivity {

ButtonRectangle button,button2;
EditText view;
private MyDatabase db;
ListView listView;
FloatingActionButton nuevaB;



@Override
protected void onCreate(Bundle savedInstanceState) {
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView= (ListView) findViewById(R.id.listView);
    view = (EditText) findViewById(R.id.view);
    db = new MyDatabase(MainActivity.this);
    button2= (ButtonRectangle) findViewById(R.id.button2);
    button= (ButtonRectangle) findViewById(R.id.btnAceptar);

    nuevaB= (FloatingActionButton) findViewById(R.id.floatB);

    File database = getApplicationContext().getDatabasePath(MyDatabase.DBNAME);
    //Si la base de datos no existe

    if(false == database.exists()) {
        db.getReadableDatabase();
        //Copiar db
        if (copyDatabase(this)) {
            Toast.makeText(this, "Base de datos creada", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Error al crear la base de datos", Toast.LENGTH_SHORT).show();
            return;
        }
    }

    nuevaB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent a=new Intent(getApplicationContext(),Registro.class);
            startActivity(a);
        }
    });

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Buscar();
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.setText("");
            view.setEnabled(true);
            button.setEnabled(true);
            cargaralimento();
        }
    });


    cargaralimento();
}
private boolean copyDatabase(Context context) { try {

    InputStream inputStream = context.getAssets().open(MyDatabase.DBNAME);
    String outFileName = MyDatabase.DBLOCATION + MyDatabase.DBNAME;
    OutputStream outputStream = new FileOutputStream(outFileName);
    byte[] buff = new byte[1024];
    int length = 0;
    while ((length = inputStream.read(buff)) > 0) {
        outputStream.write(buff, 0, length);
    }
    outputStream.flush();
    outputStream.close();
    return true;
} catch (Exception e) {
    e.printStackTrace();
    return false;
}
}

public void cargaralimento(){ ArrayList item=new ArrayList(); item=db.listarAlimentos();

ArrayAdapter<Alimento> adapter=new ArrayAdapter<Alimento>(this,android.R.layout.simple_list_item_1,item);
listView.setAdapter(adapter);
} }

附加代码helperSQL:

public class MyDatabase extends SQLiteOpenHelper {

public static final String DBNAME = "dbTest.db";
public static final String DBLOCATION = "/data/data/com.example.jinex.dbTest/databases/";
private Context mContext;
private SQLiteDatabase mDatabase;

public MyDatabase(Context context) {
    super(context, DBNAME, null, 1);
    this.mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}

public void openDatabase() {
    String dbPath = mContext.getDatabasePath(DBNAME).getPath();
    if(mDatabase != null && mDatabase.isOpen()) {
        return;
    }
    mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase() {
    if(mDatabase!=null) {
        mDatabase.close();
    }
}



public ArrayList<Alimento> listarAlimentos() throws SQLException{
    Alimento a=null;
    ArrayList<Alimento> item= new ArrayList<Alimento>();
    openDatabase();
    Cursor cursor=null;
    cursor = mDatabase.rawQuery("SELECT * FROM ALIMENTOS", null);
    cursor.moveToFirst();
    try{
    while (!cursor.isAfterLast()) {
       a=new Alimento(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getDouble(4),cursor.getDouble(5),cursor.getDouble(6),cursor.getDouble(7),cursor.getDouble(8),cursor.getDouble(9),cursor.getDouble(10),cursor.getDouble(11),cursor.getDouble(12),cursor.getDouble(13),cursor.getDouble(14),cursor.getDouble(15),cursor.getDouble(16),cursor.getDouble(17),cursor.getDouble(18),cursor.getDouble(19),cursor.getDouble(20),cursor.getDouble(21));
        item.add(a);
        cursor.moveToNext();
    }}catch (Exception e){
        Log.d("error",e.toString());
    }
    cursor.close();
    closeDatabase();
    return item;
}

附加错误:

04-19 18:25:43.365 18263-18263/com.example.jinex.dbTest E/CursorWindow: Failed to read row 0, column 9 from a CursorWindow which has 635 rows, 9 columns.

04-19 18:25:43.367 18263-18263/com.example.jinex.dbTest D/error: java.lang.IllegalStateException: Couldn't read row 0, col 9 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

数据库位于assets文件夹中,当安装运行时,将验证它是否在手机中。如果它发现它什么都不做,否则复制它。

0 个答案:

没有答案