简体   繁体   English

没有这样的带有外键的列

[英]No such column with foreign key

I want to connect to tables in my database with different ID's ( CustomerID , OrderID ). 我想使用不同的ID( CustomerIDOrderID )连接到数据库中的表。 I use the following code to create my foreign key: 我使用以下代码创建我的外键:

FOREIGN KEY("+ COLUMN_ORDER_ID + ") REFERENCES+TABLE_NAME_CUSTOMER + "(" + COLUMN_CUSTOMER_ID + "));";

I'm not posting the complete source code, because my first table works fine and I think the problem is the foreign key. 我没有发布完整的源代码,因为我的第一个表工作正常,而且我认为问题是外键。

I filter my data with this method: 我用这种方法过滤数据:

public List<Orders> getOrdersByCustomerID() {
    List<Orders> orderList = new ArrayList<Orders>();
    String query = "select " + COLUMN_ORDER_ID
            + "," + COLUMN_ORDER_NAME
            + "," + COLUMN_SETS
            + "," + COLUMN_REPEATS
            + "," + COLUMN_SECTION
            + " from " + TABLE_NAME_ORDERS
            + " where " + COLUMN_CUSTOMER_ID
            + "=" + COLUMN_ORDER_ID;
    db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    if (cursor.moveToFirst()) {
        do {
            Orders orders = new Orders(cursor.getString(0), cursor.getString(1), cursor.getInt(2), cursor.getInt(3), cursor.getString(4));
            orderList.add(orders);
        } while (cursor.moveToNext());
    }
    db.close();
    return orderList;
}

I get this error message: 我收到此错误消息:

06-26 17:11:26.154 10163-10163/com.xxx.xxx.xxx 06-26 17:11:26.154 10163-10163 / com.xxx.xxx.xxx

E/AndroidRuntime: FATAL EXCEPTION: main process: com.xxx.xxx.xxx, PID: 10163 E / AndroidRuntime:致命异常:主进程:com.xxx.xxx.xxx,PID:10163

java.lang.RuntimeException: Unable to start activity android.database.sqlite.SQLiteException: no such column: customerId(Sqlite code 1): , while compiling: select orderId,orderName,sets,repeats,section from orders where customerId=orderId,(OS error - 2:No such file or directory) java.lang.RuntimeException:无法启动活动android.database.sqlite.SQLiteException:否这样的列:customerId(Sqlite代码1):,而在编译时:从order中选择orderId,orderName,sets,repeats,repeats,customerId = orderId, (操作系统错误-2:无此类文件或目录)

I think the connection between this two ids is incorrect, or must I commit the id from my customer table? 我认为这两个ID之间的连接不正确,还是我必须从客户表中提交ID? Any tips? 有小费吗? As per my understanding, a Customer can have multiple Order s. 根据我的理解,一个Customer可以有多个Order That's why I use the foreign key; 这就是为什么我使用外键; I hope this is correct. 我希望这是正确的。

Edit: I was wrong apparently queries are case-insensitive by default in mysql. 编辑:我错了,在mysql默认情况下查询是不区分大小写的。

Edit: In order to create a foreign key both tables must contain the ConsumerId. 编辑:为了创建外键,两个表都必须包含ConsumerId。 This allows you the establish a relationship using the foreign key. 这使您可以使用外键建立关系。 Your query has to be changed as wel, after creating the foreign key. 创建外键后,必须将查询更改为wel。 It should be something like this 应该是这样的

select " + COLUMN_ORDER_IDq
        + "," + COLUMN_ORDER_NAME
        + "," + COLUMN_SETS
        + "," + COLUMN_REPEATS
        + "," + COLUMN_SECTION
        + " from " + TABLE_NAME_ORDERS + "," + TABLE_NAME_CUSTOMERS
        + " where " + TABLE_NAME_ORDERS +"." + COLUMN_CUSTOMER_ID
        + "=" + TABLE_NAME_CUSTOMERS + "." + COLUMN_CUSTOMER_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM