是否有可能适用的主键在Android的数据库中的文本字段有可能、字段、数据库中、主键

2023-09-12 06:41:33 作者:怀里的折耳猫

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

I have created a simple table that contains the name and email id's of the persons. When i am giving the create query like this:

创建表的联系人(name文本不为空,电子邮件文本主键NOT NULL);

不过,这是行不通的。运行,也没有主键工作时我没有得到任何异常或错误。虽然使用SQLite的浏览器在浏览表我观察,目前只有四种数据类型:

But this is not working. I'm not getting any exception or error while running nor the primary key is working. While browsing the table using SQLite browser i observed that there are only four data types:

是否有可能适用于主键只在整数?是这样的,如果我想要在文本字段主键。谢谢。这是code,我用:

Is it possible to apply primary key only on integer? What is the case if i want primary key on text fields. Thank you. This is the code that i used:

公共类DBAdapter {

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;
}

}

}

推荐答案

根据在 常见问题解答 源码文件,使用文本为数据类型的主键应该工作。

as per the faq of sqlite documentation, using TEXT as a datatype for primary key should work.

我用您的查询,这里是,在创建表。

i used your query and here it is, the table is created.

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')

现在这里是哪里出了问题。

now here is where it went wrong.

当我跑这又

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

什么都没有发生,没有错误。 但它并没有更新记录。所以我的结论数据的完整性是有,但你没有得到有关失败的任何反馈

nothing happened, no errors. But it did not update the record. so i conclude data integrity is there but you don't get any feedback about the failure.

 
精彩推荐
图片推荐