android - 是否可以在android数据库中的文本字段上应用主键

标签 android sqlite

我创建了一个简单的表,其中包含人员的姓名和电子邮件 ID。当我给出这样的创建查询时:

"创建表联系人(姓名文本不为空,电子邮件文本主键不为空);"

但这不起作用。运行时我没有收到任何异常或错误,主键也没有工作。在使用 SQLite 浏览器浏览表格时,我发现只有四种数据类型:

enter image description here

是否可以只对整数应用主键?如果我想要文本字段上的主键是什么情况。谢谢你。这是我使用的代码:

public class DBAdapter {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "MyDB";
private static final String DATABASE_TABLE = "contacts";
private static final int DATABASE_VERSION = 2;

private static final String DATABASE_CREATE =
    "create table contacts (_id integer primary key autoincrement, "
    + "name text not null, email text not null);";

private final Context context;    

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        try {
            db.execSQL(DATABASE_CREATE);    
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }
}    

//---opens the database---
public DBAdapter open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---    
public void close() 
{
    DBHelper.close();
}

//---insert a contact into the database---
public long insertContact(String name, String email) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_EMAIL, email);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular contact---
public boolean deleteContact(long rowId) 
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

//---retrieves all the contacts---
public Cursor getAllContacts() 
{
    return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
            KEY_EMAIL}, null, null, null, null, null);
}

//---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
            KEY_NAME, KEY_EMAIL}, KEY_ROWID + "=" + rowId, null,
            null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

//---updates a contact---
public boolean updateContact(long rowId, String name, String email) 
{
    ContentValues args = new ContentValues();
    args.put(KEY_NAME, name);
    args.put(KEY_EMAIL, email);
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}

最佳答案

根据 faq 的 sqlite 文档,使用 TEXT 作为主键的 datatype 应该可以工作。

我使用了您的查询,就在这里,表已创建。

CREATE TABLE contacts ( email text primary key not null, name text not null);

INSERT INTO contacts VALUES ('sample@email.com', 'sample')
INSERT INTO contacts VALUES ('people@email.com', 'sample')

enter image description here

enter image description here

现在这里出了问题。

当我再次运行时

INSERT INTO contacts VALUES ('sample@email.com', 'sample')

什么也没发生,没有错误。 但它没有更新记录。所以我得出结论数据完整性存在,但您没有收到有关失败的任何反馈

关于android - 是否可以在android数据库中的文本字段上应用主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591492/

相关文章:

android - VideoView 播放状态或 MediaController 播放/暂停的事件

android - 在 Android 上使用 ORMLite 重新打开连接

Golang Go-SQLite3 cannot iterate over type 报错

java - 保存到内存 Android

android - 如何让webview适合全屏?

android - 为 Sprite 表使用纹理时防止间隙/边界

android - Android Studio 上的 Code Iris 插件

.net - SQL Server Compact Edition 是 SQLite 的合理替代品吗?

iphone - 在 sqlite3_column_text 中使用列名而不是数字

sqlite - 在Web SQL中存储DateTime值