gpt4 book ai didi

Android SQLite - 没有这样的表错误(代码 1)

转载 作者:搜寻专家 更新时间:2023-10-30 23:43:47 27 4
gpt4 key购买 nike

我正在为公交时刻表开发一个 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 Activity 中,我使用以下代码:

 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: no such table: directesDirBarna (code 1)

尽管这个表存在于我的数据库中。

我怀疑发生这种情况是因为我的代码找不到数据库,因此它创建了一个空白数据库,因此在其中找不到任何内容,但我找不到发生这种情况的位置。

谁能照亮我的路?非常感谢。

编辑:这已解决。我用了这个https://github.com/jgilfelt/android-sqlite-asset-helper ,从我的设备上卸载了应用程序,进行了干净的构建,现在可以使用了。

最佳答案

我认为这个问题与查询sql本身有关:

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 <> '';";

你正在使用同一张表两次......

关于Android SQLite - 没有这样的表错误(代码 1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31711109/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com