是否可以在android数据库中的文本字段上应用主键 [英] Is it possible to apply primary key on the text fields in android database
问题描述
我创建了一个简单的表格,其中包含人员的姓名和电子邮件 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:
创建表格联系人(姓名文本不为空,电子邮件文本主键不为空);"
但这行不通.运行时我没有收到任何异常或错误,主键也没有工作.在使用 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:
是否可以只在整数上应用主键?如果我想在文本字段上使用主键,会发生什么情况.谢谢你.这是我使用的代码:
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:
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;
}
}
推荐答案
按照 sqlite 文档的 faq,使用 TEXT
作为主键的数据类型应该可以工作.
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')
现在是哪里出错了.
当我再次运行时
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.
这篇关于是否可以在android数据库中的文本字段上应用主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!