我已经尝试根据我在此网站上找到的建议编辑我的代码,但似乎没有任何工作..我仍然不断收到以下logcat错误:
03-24 11:44:27.037: E/AndroidRuntime(1809): Caused by: android.database.sqlite.SQLiteException:
ambiguous column name: _id (code 1): , while compiling:
CREATE VIEW customerView AS SELECT _id AS _id, fName, lName, customerMeterId, customerMeterNumber, customerPlotNumber, _id FROM customers AS customers
INNER JOIN meters AS meters ON customerMeterId =_id
上面在logcat中引用的类:
public class ViewCustomers {
public static final String TABLE_CUSTOMER_VIEW = "customerView";
public static final String CUSTOMER_VIEW_ID = CustomerTableDetails.KEY_CUSTOMER_ID;
public static final String CUSTOMER_VIEW_FIRSTNAME = CustomerTableDetails.KEY_FIRST_NAME;
public static final String CUSTOMER_VIEW_LASTNAME = CustomerTableDetails.KEY_LAST_NAME;
public static final String CUSTOMER_VIEW_METER = CustomerTableDetails.KEY_METER_NUMBER;
public static final String CUSTOMER_VIEW_PLOT = CustomerTableDetails.KEY_PLOT_NUMBER;
public static final String CUSTOMER_VIEW_KEY_METER_ID = CustomerTableDetails.KEY_METER_ID;
private static final String CREATE_CUSTOMER_VIEW = ""
+ "CREATE VIEW " + TABLE_CUSTOMER_VIEW
+ " AS SELECT "+ CustomerTableDetails.KEY_CUSTOMER_ID+" AS _id,"+
" "+CustomerTableDetails.KEY_FIRST_NAME+","+
" "+CustomerTableDetails.KEY_LAST_NAME+","+
" "+CustomerTableDetails.KEY_METER_ID+","+
" "+CustomerTableDetails.KEY_METER_NUMBER+","+
" "+CustomerTableDetails.KEY_PLOT_NUMBER+","+
" "+MeterTableDetails.METER_ID+""+
" FROM "+CustomerTableDetails.TABLE_CUSTOMERS+" AS customers "+" INNER JOIN "+MeterTableDetails.TABLE_METERS+" AS meters"+
" ON "+CustomerTableDetails.KEY_METER_ID+" ="+MeterTableDetails.METER_ID;
public static void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_CUSTOMER_VIEW);
}
public static void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
Log.w(ViewCustomers.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
database.execSQL("DROP VIEW IF EXISTS " + ViewCustomers.TABLE_CUSTOMER_VIEW);
onCreate(database);
}
我甚至使用别名但仍然没有解决方案。现在我不知道如何解决它。
修改
更正后:
private static final String CREATE_CUSTOMER_VIEW = ""
+ "CREATE VIEW " + TABLE_CUSTOMER_VIEW
+ " AS SELECT "+MeterTableDetails.TABLE_METERS+"."+MeterTableDetails.METER_ID+""+" AS "+ MeterTableDetails.TABLE_METERS+"."+MeterTableDetails.METER_ID +","+
" "+CustomerTableDetails.KEY_FIRST_NAME+","+
" "+CustomerTableDetails.KEY_LAST_NAME+","+
" "+CustomerTableDetails.KEY_METER_ID+","+
" "+CustomerTableDetails.KEY_METER_NUMBER+","+
" "+CustomerTableDetails.KEY_PLOT_NUMBER+","+
" "+CustomerTableDetails.TABLE_CUSTOMERS+"."+ CustomerTableDetails.KEY_CUSTOMER_ID+
" FROM "+CustomerTableDetails.TABLE_CUSTOMERS+" AS customers "+" INNER JOIN "+MeterTableDetails.TABLE_METERS+" AS meters"+
" ON "+CustomerTableDetails.KEY_METER_ID+" ="+MeterTableDetails.TABLE_METERS+"."+MeterTableDetails.METER_ID;
编辑2
03-24 13:58:24.687: I/create statement(3098): CREATE VIEW customerView AS SELECT meters._id AS meters._id, fName, lName, customerMeterId, customerMeterNumber,
customerPlotNumber, customers._id FROM customers AS customers
INNER JOIN meters AS meters ON customerMeterId = meters._id
答案 0 :(得分:11)
使用tablename._id以在上面的查询中引用_id
变化:
CREATE VIEW customerView AS SELECT _id AS _id,
fName, lName, customerMeterId, customerMeterNumber,
customerPlotNumber, _id FROM customers AS customers
INNER JOIN meters AS meters ON customerMeterId =_id
到
CREATE VIEW customerView AS SELECT customers._id AS _id,
fName, lName, customerMeterId, customerMeterNumber, customerPlotNumber,
_id FROM customers AS customers
INNER JOIN meters AS meters ON
customerMeterId =customers._id // or meters._id -> whatever you think suits
或
CREATE VIEW customerView AS SELECT meters._id AS _id,
fName, lName, customerMeterId, customerMeterNumber, customerPlotNumber,
_id FROM customers AS customers
INNER JOIN meters AS meters ON customerMeterId =meters._id