gpt4 book ai didi

android - android中反复出现不明确的列名_id sqlite错误

转载 作者:太空狗 更新时间:2023-10-29 15:38:26 26 4
gpt4 key购买 nike

我已经尝试根据我在这个网站上找到的建议编辑我的代码,但似乎没有任何效果..我仍然不断收到以下 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

最佳答案

使用 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

关于android - android中反复出现不明确的列名_id sqlite错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22604721/

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