我正在使用现有数据库在Android应用中构建数据库。我在调试中继续收到错误,说列stop_name不存在:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bkane56.practice.practiceapp/com.bkane56.practice.practiceapp.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
数据库的架构如下:
CREATE TABLE trips (
block_id char(40),
route_id char(40),
direction_id char(2),
trip_headsign char(20),
shape_id char(20),
service_id char(20),
_id char(20) PRIMARY KEY,
FOREIGN KEY(route_id) references routes(_id),
FOREIGN KEY(service_id) references calendar(_id)
);
CREATE TABLE calendar(
_id char(40),
start_date char(10),
end_date char(10),
monday char(1),
tuesday char(1),
wednesday char(1),
thursday char(1),
friday char(1),
saturday char(1),
sunday char(1)
);
CREATE TABLE routes (
route_long_name char(200),
route_type char(40),
route_text_color char(40),
agency_id char(40),
_id char(40) PRIMARY KEY,
route_color char(40),
route_short_name char(40)
);
CREATE TABLE stops (
stop_lat Decimal(9,6),
zone_id char(30),
stop_lon Decimal(9,6),
_id char(30) PRIMARY KEY,
stop_desc char(30),
stop_name char(30),
location_type char(30),
stop_code char(30)
);
CREATE TABLE stop_times (
trip_id char(40),
arrival_time char(40),
departure_time char(40),
stop_id char(40),
stop_sequence char(40),
stop_headsign char(40),
pickup_type char(40),
drop_off_type char(40),
shape_dist_traveled char(40),
timepoint char(20),
FOREIGN KEY(stop_id) references stops(_id),
FOREIGN KEY(trip_id) references trips(_id)
);
CREATE TABLE android_metadata (
locale char(40)
);
构建数据库的方法(在StackOverflow上很常见)似乎正在工作,不确定是否正确。没有错误或通知数据库不存在。这是方法:
public void copydatabase() throws IOException {
// Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);
// Path to the created empty db
String outfilename = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream(outfilename);
// transfer byte from inputfile to 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();
}
这是方法(在我的Database Helper文件中扩展了SQLiteOpenHelper)我用来获取停止名称的列表。稍后用于填充ViewList。
public Cursor getAllStops() {
final String TABLE = "stops";
final String[] COLUMN = new String[] {"stop_name as _id"};
final String ORDER_BY = "stop_lon";
String mypath = DB_PATH + DB_NAME;
try {
myDataBase = SQLiteDatabase.openDatabase(mypath, null,
SQLiteDatabase.OPEN_READONLY);
Cursor c = myDataBase.query(TABLE, COLUMN, null, null, null, null, ORDER_BY, null);
// Cursor c = myDataBase.rawQuery("select stop_name as _id from stops", null);
if (c != null) {
c.moveToFirst();
return c;
} else {
System.out.println("unable to execute getAllStops, cursor = null!");
}
}
finally {
myDataBase.close();
}
return null;
}
有没有办法检查模拟器使用的数据库。我找不到:
data/data/com.bkane56.practice.practiceapp/databases/metrolink.sqlite
非常感谢任何帮助。
更新:
错误似乎来自populateStopsDB方法:
private void populateStopsDb() {
Cursor cursor = myHelper.getAllStops();
String[] stops = new String[] {"_id"};
int[] tvIds = new int[] {R.layout.listview_stops};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.simplerow,
cursor, stops, tvIds,0);
lvMetroStop = (ListView) findViewById(R.id.metroStopList);
}
抱歉原本没有发布。
由于