我正在开发一个android应用程序,用于公交车时刻表。我有一个带有停止和时间的sqlite数据库,但是我目前正在做一个噩梦试图访问它。我在谷歌上搜索了这个问题,但找不到解决办法。我已经构建了一个DataBaseHelper类,该类扩展了SQLiteOpenHelper,代码如下:public class DataBaseHelper extends SQLiteOpenHelper { private static String DB_PATH; private static String DB_NAME = "BagesExpress_def"; private SQLiteDatabase myDataBase; private final Context myContext; public DataBaseHelper(Context context){ super(context, DB_NAME, null,1); this.myContext = context; this.DB_PATH = this.myContext.getDatabasePath(DB_NAME).getAbsolutePath(); } public void createDatabase() throws IOException{ boolean dbExist = checkDataBase(); if(dbExist){ Log.v("EXISTS", "dbExists"); }else{ this.getReadableDatabase(); try{ copyDataBase(); } catch (IOException e){ throw new Error ("Error copying data"); } } } private boolean checkDataBase(){ SQLiteDatabase checkDB = null; try{ String myPath = DB_PATH; checkDB = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ } if(checkDB!=null){ checkDB.close(); } return checkDB != null; } private void copyDataBase() throws IOException{ InputStream myInput = myContext.getAssets().open(DB_NAME); String outFilename = DB_PATH; OutputStream myOutput = new FileOutputStream(outFilename); byte[] buffer = new byte [1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer,0,length); } myOutput.flush(); myOutput.close(); myInput.close(); } public void openDataBase() throws SQLException{ String myPath = DB_PATH; myDataBase = SQLiteDatabase.openDatabase(myPath,null, SQLiteDatabase.OPEN_READONLY); } public synchronized void close() { if(myDataBase!=null) myDataBase.close(); super.close(); } @Override public void onCreate (SQLiteDatabase db){ } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ if(newVersion>oldVersion) try{ copyDataBase(); } catch(IOException ioe){ } } public List<Viatge> getViatges(int horaSortida, int paradaSortida, int paradaArribada){ Log.v("DBPATH",DB_PATH); List<Viatge> viatges = new ArrayList<Viatge>(); String selectQuery = "SELECT t1.run, t1.parada, t2.parada,t1.hora,t2.hora FROM directesDirBarna t1, directesDirBarna t2 \n" + "WHERE (t1.run = t2.run AND t1.parada = "+ paradaSortida +" AND t2.parada = " + paradaArribada +") \n" + "AND t1.Hora > "+ horaSortida +" AND t2.Hora <> '';"; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()){ do{ Viatge viatge = new Viatge(); viatge.setParadaSortida(Integer.parseInt(cursor.getString(1))); viatge.setParadaArribada(Integer.parseInt(cursor.getString(2))); viatge.setHoraSortida(Integer.parseInt(cursor.getString(3))); viatge.setHoraArribada(Integer.parseInt(cursor.getString(4))); viatges.add(viatge); } while(cursor.moveToNext()); } return viatges; }}在testSQL活动中,我使用以下代码: public class SqlTest extends Activity { private DataBaseHelper myDBHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sql_test); Button sqlButton = (Button)findViewById(R.id.SQLBtn); sqlButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myDBHelper = new DataBaseHelper(getApplicationContext()); try { myDBHelper.openDataBase(); } catch(SQLException sqle){ } myDBHelper.getViatges(1000,1,10); } }); } @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_sql_test, 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); }当我按下按钮获取数据库时,会出现以下错误:android.database.sqlite.sqliteexception:没有这样的表:DirectesDirbarna(代码1)尽管这个表存在于我的数据库中。我怀疑发生这种情况是因为我的代码找不到数据库,所以它创建了一个空白数据库,因此在其中找不到任何东西,但我找不到发生这种情况的地方。有人能照亮我的路吗?谢谢。编辑:问题解决了。我使用了这个https://github.com/jgilfelt/android-sqlite-asset-helper,从我的设备中卸载了应用程序,创建了一个干净的版本,现在它可以工作了。 最佳答案 我认为这个问题与查询sql本身有关:string selectquery=“从directesDirBarna t1, directesDirBarna t2中选择T1.run、T1.parada、T2.parada、T1.hora、T2.hora”\n”+“其中(t1.run=t2.run和t1.parada=”+paradasortida+“和t2.parada=”+paradaarribada+“)\n”+“和T1.hora>”+horasortida+“和T2.hora”;“;你用同一张桌子两次…
08-26 10:14