Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android重复值更新_Android_Database_Android Sqlite - Fatal编程技术网

Android重复值更新

Android重复值更新,android,database,android-sqlite,Android,Database,Android Sqlite,我正在与SQLite合作。我成功地创建了一个数据库,可以向数据库中输入一些值。现在我想检查重复的值。有关更多信息,例如,我第一次输入时: Tittle: dev Description : job Price :2 第二次我输入了这个重复值,我只想改变价格值。我写了代码,但我不能改变重复值的价格 这是一个databasehelper java类: private static final int DATABASE_VERSION = 1; private static final Strin

我正在与SQLite合作。我成功地创建了一个数据库,可以向数据库中输入一些值。现在我想检查重复的值。有关更多信息,例如,我第一次输入时:

Tittle: dev 
Description : job
Price :2
第二次我输入了这个重复值,我只想改变价格值。我写了代码,但我不能改变重复值的价格

这是一个databasehelper java类:

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MainStradaDB4";
private static final String TABLE_CONTACTS = "CardTable1";
public static final String KEY_ID = "id";
private static final String KEY_Tittle = "name";
private static final String KEY_Description = "description";
private static final String KEY_Price = "price";
private static final String KEY_Counter = "counter";
private static final String KEY_Image = "image";
private final ArrayList<Contact> contact_list = new ArrayList<Contact>();
public static SQLiteDatabase db;

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_Tittle + " TEXT,"

            + KEY_Description + " TEXT,"

            + KEY_Price + " TEXT,"

            + KEY_Counter + " TEXT,"

            + KEY_Image + " TEXT"

            + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
    onCreate(db);
}

// Adding new contact
public void Add_Contact(Contact contact) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    if (!somethingExists(contact.getTitle())) {

        values.put(KEY_Tittle, contact.getTitle());
        values.put(KEY_Description, contact.getDescription());
        values.put(KEY_Price, contact.getPrice());

        values.put(KEY_Counter, contact.getCounter());
        values.put(KEY_Image, contact.getImage());


        db.insert(TABLE_CONTACTS, null, values);

        Log.e("Table Result isss", String.valueOf(values));
        db.close(); // Closing database connection

    }
    else
    {
        Update_Contact(contact);
    }


}

public void deleteUser(String userName) {
    db = this.getWritableDatabase();
    try {
        db.delete(TABLE_CONTACTS, "name = ?", new String[] { userName });

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.close();
    }
}



public int Update_Contact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_Counter, contact.getCounter());

    // updating row
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });
}



// Getting single contact
Contact Get_Contact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS,
            new String[] { KEY_ID, KEY_Tittle, KEY_Description, KEY_Price,
                    KEY_Counter, KEY_Image }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();



    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2), cursor.getString(3),
            cursor.getString(4), cursor.getString(5));
    // return contact
    cursor.close();
    db.close();

    return contact;
}

public boolean somethingExists(String x) {
    Cursor cursor = db.rawQuery("select * from " + TABLE_CONTACTS
            + " where name like '%" + x + "%'", null);
    boolean exists = (cursor.getCount() > 0);

    Log.e("Databaseeeeeeeee", String.valueOf(cursor));
    cursor.close();
    return exists;
}

public ArrayList<Contact> Get_Contacts() {
    try {
        contact_list.clear();
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setTitle(cursor.getString(1));

                contact.setDescription(cursor.getString(2));

                contact.setPrice(cursor.getString(3));
                contact.setCounter(cursor.getString(4));

                contact.setImage(cursor.getString(5));

                contact_list.add(contact);
            } while (cursor.moveToNext());

        }

        cursor.close();
        db.close();
        return contact_list;
    } catch (Exception e) {

        Log.e("all_contact", "" + e);
    }

    return contact_list;
}
private static final int DATABASE_VERSION=1;
私有静态最终字符串数据库\u NAME=“MainStradaDB4”;
私有静态最终字符串表\u CONTACTS=“CardTable1”;
公共静态最终字符串键\u ID=“ID”;
私有静态最终字符串密钥\u title=“name”;
私有静态最终字符串键\u Description=“Description”;
私有静态最终字符串键\u Price=“Price”;
私有静态最终字符串键\u Counter=“Counter”;
私有静态最终字符串键\u Image=“Image”;
private final ArrayList contact_list=新建ArrayList();
公共静态数据库sqlitedb;
公共数据库处理程序(上下文){
super(上下文、数据库名称、null、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
字符串CREATE_CONTACTS_TABLE=“CREATE TABLE”+TABLE_CONTACTS+”(“
+键ID+“整数主键,”+键标题+“文本,”
+密钥描述+文本
+键+价格+文本
+按键计数器+“文本,”
+按键图像+“文本”
+ ")";
execSQL(创建联系人表);
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
db.execSQL(“如果存在删除表”+表_联系人);
onCreate(db);
}
//添加新联系人
公共无效添加联系人(联系人联系人){
db=this.getWritableDatabase();
ContentValues=新的ContentValues();
如果(!somethingExists(contact.getTitle())){
value.put(KEY_title,contact.getTitle());
value.put(KEY_Description,contact.getDescription());
value.put(KEY_Price,contact.getPrice());
value.put(KEY_Counter,contact.getCounter());
value.put(KEY_Image,contact.getImage());
db.插入(表_触点,空,值);
Log.e(“表结果isss”,String.valueOf(values));
db.close();//关闭数据库连接
}
其他的
{
更新联系方式(联系方式);
}
}
public void deleteUser(字符串用户名){
db=this.getWritableDatabase();
试一试{
db.delete(表_CONTACTS,“name=?”,新字符串[]{userName});
}捕获(例外e){
e、 printStackTrace();
}最后{
db.close();
}
}
公共int更新_联系人(联系人){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(KEY_Counter,contact.getCounter());
//更新行
返回db.update(表联系人、值、键ID+“=?”,
新字符串[]{String.valueOf(contact.getID())};
}
//获得单一联系
联系人获取联系人(int id){
SQLiteDatabase db=this.getReadableDatabase();
Cursor=db.query(表,
新字符串[]{KEY_ID,KEY_title,KEY_Description,KEY_Price,
按键计数器,按键图像},按键ID+“=?”,
新字符串[]{String.valueOf(id)},null,null,null);
如果(光标!=null)
cursor.moveToFirst();
Contact Contact=新联系人(Integer.parseInt(cursor.getString(0)),
cursor.getString(1)、cursor.getString(2)、cursor.getString(3),
cursor.getString(4),cursor.getString(5));
//回接
cursor.close();
db.close();
回接;
}
公共布尔somethingExists(字符串x){
Cursor Cursor=db.rawQuery(“选择*自”+表\u联系人
+其中名称类似“%”+x+“%”,null);
布尔存在=(cursor.getCount()>0);
Log.e(“databaseeee”,String.valueOf(cursor));
cursor.close();
回报存在;
}
public ArrayList Get_Contacts(){
试一试{
联系人列表。清除();
String selectQuery=“SELECT*FROM”+表格\联系人;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
if(cursor.moveToFirst()){
做{
触点=新触点();
setID(Integer.parseInt(cursor.getString(0));
contact.setTitle(cursor.getString(1));
contact.setDescription(cursor.getString(2));
contact.setPrice(cursor.getString(3));
contact.setCounter(cursor.getString(4));
contact.setImage(cursor.getString(5));
联系人列表。添加(联系人);
}while(cursor.moveToNext());
}
cursor.close();
db.close();
返回联系人列表;
}捕获(例外e){
Log.e(“所有联系人”和“+e”);
}
返回联系人列表;
}
我做错了什么?如果有人知道答案,请帮助我
谢谢

我不明白你的意思。这是解决我问题的办法吗?
Try this one:-


  public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_HOUR + " TEXT NOT NULL, " +
                KEY_DAY + " TEXT NOT NULL, " +
                KEY_LOCATION + " TEXT NOT NULL, " +
                KEY_SUBJECT + " TEXT NOT NULL, " +
                KEY_START + " TEXT NOT NULL, " +
                KEY_END + " TEXT NOT NULL, " +
                "UNIQUE (" + KEY_DAY + ", " + KEY_HOUR + ") ON CONFLICT ROLLBACK);"
        );