如何使用MAC地址作为主键? (sqlite android)

时间:2016-03-26 02:05:59

标签: android sql sqlite

如何使用MAC地址作为我的sqlite数据库的主键:我可以轻松填充数据库,并选择*。

问题是我将MAC地址定义为CHAR(17)以获取所有字符和collins

private static final String SQL_CREATE_TABLE_DEVICE = "CREATE TABLE " + TABLE_DEVICE + "("
        + COLUMN_DEVICE_MAC + " CHAR(17) PRIMARY KEY, "
        + COLUMN_DEVICE_LAST_TIME_DETECTION + " NUMERIC, "
        + COLUMN_DEVICE_LAST_TIME_RANGE + " REAL, "
        + COLUMN_DEVICE_DETECTION_FREQUENCY + " INTEGER, "
        + COLUMN_DEVICE_CUMULATIVE_DETECTION_DURATION + " REAL, "
        + COLUMN_DEVICE_PHONE_NUMBER + " VARCHAR(50), "
        + COLUMN_DEVICE_DESCRIPTIVE_NAME + " TEXT "
        +");";

但是,当我根据主键获取一行时,我遇到了一个问题,因为我将MAC作为字符串从MAIN活动中传递出去,而且我对如何使用引用感到困惑:

public Cursor getDatabyMAC(String MAC)
{
    SQLiteDatabase db =this.getWritableDatabase();
    Cursor result = db.rawQuery("select * from "+TABLE_DEVICE+" where _Did =? ",new String[]{MAC});
    return result;
}

public Cursor getDatabyMAC(String MAC)
{
    SQLiteDatabase db =this.getWritableDatabase();
    Cursor result = db.rawQuery("select * from "+TABLE_DEVICE+" where _Did = '"+device_ID+"'",null);
    return result;
}

顺便说一句。

(以下是主要电话)

public void onbtnclick2(View view) {

    Cursor result2 = myDb.getDatabyMAC("01:53:45:67:87:AE");
    txt_ReadSpecific.setText(result2.getCount());



}

2 个答案:

答案 0 :(得分:0)

尝试在查询结尾处添加COLLATE NOCASE,这可能是区分大小写的问题,并且会使其搜索String,无论是大写还是大写。< / p>

例如:"select * from "+TABLE_DEVICE+" where _Did = '"+device_ID+"' COLLATE NOCASE"

答案 1 :(得分:-1)

这里是:

select hex(mac_addr) from table_Name;

INSERT INTO table_Name (mac_addr) VALUES (x'0123456789AB');

使mac_addr成为主键:

ALTER TABLE table_Name

ADD PRIMARY KEY (mac_addr)