我正在为公交时刻表开发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,从我的设备中卸载的应用程序,做了一个干净的构建,现在它工作。
答案 0 :(得分:1)
我认为这个问题与查询sql本身有关:
String selectQuery =&#34; SELECT t1.run,t1.parada,t2.parada,t1.hora,t2.hora FROM directesDirBarna t1, directesDirBarna t2
\ n&#34; +
&#34; WHERE(t1.run = t2.run AND t1.parada =&#34; + paradaSortida +&#34; AND t2.parada =&#34; + paradaArribada +&#34;)\ n&#34 ; +
&#34; AND t1.Hora&gt; &#34; + horaSortida +&#34;和t2.Hora&lt;&gt; &#39;&#39 ;;&#34 ;;
你两次使用同一张桌子......