我正在尝试通过assets文件夹中的托管文件连接到外部数据库。我想要介绍所有的陈述,但是在启动程序时告诉我“tareas'表丢失了。我留下代码......
代码SQL:
CREATE TABLE 'tareas' (
'_id' INTEGER PRIMARY KEY AUTOINCREMENT,
'nombre' TEXT,
'observaciones' TEXT,
'fecha' INTEGER
);
INSERT INTO 'tareas' VALUES ('1','Programming',NULL,NULL);
INSERT INTO 'tareas' VALUES ('2','Examn database',NULL,'18/12/2014');
Code Java class bbdd:
public class BBDD extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.ros.juanmanuel.appnotas2/databases/";
private static String DB_NAME = "bbdd";
private SQLiteDatabase myDataBase;
private final Context myContext;
public BBDD(Context context){
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
Log.i("BBDD","Base de datos creada correctamente.");
/*try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}*/
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
public void openDataBase(int flag) throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, flag);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
}
public Tareas pruebaConsulta(int id){
Tareas t;
Cursor cursor = myDataBase.rawQuery("SELECT * FROM tareas WHERE _id="+id, null);
cursor.moveToFirst();
t = new Tareas(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3));
return t;
}
}
Code Java(打开数据库的类)(方法select()):
公共类ListaTareas扩展了ActionBarActivity {
private TextView textoSeleccionado;
private Principal princ;
private BBDD myDbHelper = null;
private ListView lista;
private Adapter adapter;
private Tareas t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_tareas);
darValorLabel();
myDbHelper = new BBDD(this);
try {
myDbHelper.createDataBase();
} catch (IOException e) {
e.printStackTrace();
Log.d("BBDD", "Error al crear la base de datos (Clase.Principal)");
}
myDbHelper.openDataBase(SQLiteDatabase.OPEN_READWRITE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_lista_tareas, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public String recogerParametro(){
return getIntent().getExtras().getString("NombreSeleccion");
}
private void darValorLabel(){
textoSeleccionado = (TextView) findViewById(R.id.lbListaTareas);
textoSeleccionado.setText("Lista Tareas: "+recogerParametro());
}
public void cambiarVentanaAdd(View view){
Intent intent = new Intent(this, PantallaAdd.class);
startActivity(intent);
}
public TextView getTextoSeleccionado() {
return textoSeleccionado;
}
public void setTextoSeleccionado(TextView textoSeleccionado) {
this.textoSeleccionado = textoSeleccionado;
}
/*public void consultar(int id){
lista = (ListView) findViewById(R.id.listaTareas);
myDbHelper.pruebaConsulta(id);
List<Tareas> listaT = new ArrayList<Tareas>();
listaT.add(myDbHelper.pruebaConsulta(id));
ArrayAdapter<Tareas> arrayAdapter = new ArrayAdapter<Tareas>(
this,
android.R.layout.simple_list_item_1,
listaT
);
lista.setAdapter(arrayAdapter);
}*/
public void select(View view){
TextView pantalla = (TextView) findViewById(R.id.pantallaPrueba);
t = new Tareas();
t= myDbHelper.pruebaConsulta(1);
pantalla.setText(t.getNombreTarea());
}
}
错误:
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17477)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: tareas (code 1): , while compiling: SELECT * FROM tareas
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at com.ros.juanmanuel.appnotas2.BBDD.pruebaConsulta(BBDD.java:147)
at com.ros.juanmanuel.appnotas2.ListaTareas.select(ListaTareas.java:128)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View$1.onClick(View.java:3602)
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17477)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
删除你的应用并重新安装,你有一个旧版本的数据库,
Yout table&#39; tareas&#39;尚未创建,或有其他名称。