如果新值小于数据库中的值,则Sqlite如何更新

时间:2017-03-04 19:40:27

标签: android sqlite sql-update android-sqlite

我有一个数据库,您可以在其中输入名称和时间,但时间会更新,我只是想在新时间小于数据库中的时间时更新数据库。

例如:

我的数据库:

ID   TeamName   TeamTime

1     Team 1     3 sec.

2     Team 2     9 sec.

3     Team 3     4 sec.

第1组的新时间= 5秒

if (new_time_team_1 < team_1_actual_time) {
    myDb.update(to_new_time);
}

我该怎么做?

这是我的DatabaseHelper代码:

package com.example.caca_a_frase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import java.util.ArrayList;

public class BaseDeDados extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Caca_a_Frase.db";
public static final String TABLE_NAME = "Top5";
public static final String COL_1 = "ID";
public static final String COL_2 = "NOME";
public static final String COL_3 = "TEMPO";

public BaseDeDados(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NOME TEXT UNIQUE,TEMPO INTEGER)");
}

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

public boolean insertData(String equipa,String tempo) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2,equipa);
    contentValues.put(COL_3,tempo);
    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

public ArrayList<String> getEquipas() {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<String> nomeEquipas = new ArrayList<String>();
    Cursor cursor = db.rawQuery("SELECT " + COL_2 + " FROM " + TABLE_NAME + " ORDER BY " + COL_3 + " ASC", null);

    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();


        do {
            String name = cursor.getString(cursor.getColumnIndex(COL_2));

            nomeEquipas.add(name);

        } while (cursor.moveToNext());

    }

    cursor.close();
    db.close();

    return nomeEquipas;

}

public ArrayList<String> getTempo() {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<String> melhorTempo = new ArrayList<String>();
    String orderBy = BaseDeDados.COL_3 + " ASC";
    Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, orderBy);

    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();


        do {
            String tempo = cursor.getString(cursor.getColumnIndex(COL_3));

            melhorTempo.add(tempo);

        } while (cursor.moveToNext());

    }

    cursor.close();
    db.close();

    return melhorTempo;

}

public void updateTempo(String nome_equipa, String novo_tempo) {
    SQLiteDatabase db = this.getReadableDatabase();
    ContentValues args = new ContentValues();
    args.put(COL_3, novo_tempo);
    db.update(TABLE_NAME, args, "NOME = ?", new String[]{nome_equipa});
}

}

我希望我解释得很好。

更新

我更新了我的代码。 我决定使用一个游标来获取团队的旧时间并将其与新时间进行比较以进行更新,但他给了我一个错误。 这是LogCat:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.caca_a_frase, PID: 19421
              android.database.sqlite.SQLiteException: no such column: Jessica (code 1): , while compiling: SELECT TEMPO FROM Top5 WHERE NOME = Jessica
                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
                  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1440)
                  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1379)
                  at com.example.caca_a_frase.BaseDeDados.updateTempo(BaseDeDados.java:103)
                  at com.example.caca_a_frase.Game$4.onClick(Game.java:717)
                  at android.view.View.performClick(View.java:5197)
                  at android.view.View$PerformClick.run(View.java:20926)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:5951)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

这是代码:

public void updateTempo(String nome_equipa, String novo_tempo) {
    SQLiteDatabase db = this.getReadableDatabase();
    ContentValues args = new ContentValues();
    args.put(COL_3, novo_tempo);
    Cursor cursor = db.rawQuery("SELECT " + COL_3 + " FROM " + TABLE_NAME + " WHERE " + COL_2 + " = " + nome_equipa, null);
    String tempo_antigo = cursor.getString(cursor.getColumnIndex(COL_3));
    Integer new_time = Integer.parseInt(novo_tempo);
    Integer old_time = Integer.parseInt(tempo_antigo);
    if (new_time < old_time) {
        db.update(TABLE_NAME, args, "NOME = ?", new String[]{nome_equipa});
    }
}

更新

我去看了这个问题的答案(SQLite Exception no such column when trying to select)并仍然给出错误。

这是LogCat:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.caca_a_frase, PID: 8731
              android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
                  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
                  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                  at com.example.caca_a_frase.BaseDeDados.updateTempo(BaseDeDados.java:104)
                  at com.example.caca_a_frase.Game$4.onClick(Game.java:717)
                  at android.view.View.performClick(View.java:5197)
                  at android.view.View$PerformClick.run(View.java:20926)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:5951)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

这是新代码:

 public void updateTempo(String nome_equipa, String novo_tempo) {
    SQLiteDatabase db = this.getReadableDatabase();
    ContentValues args = new ContentValues();
    args.put(COL_3, novo_tempo);
    Cursor cursor = db.rawQuery("SELECT " + COL_3 + " FROM " + TABLE_NAME + " WHERE " + COL_2 + " =?", new String[]{nome_equipa});
    String tempo_antigo = cursor.getString(cursor.getColumnIndex(COL_3));
    Integer new_time = Integer.parseInt(novo_tempo);
    Integer old_time = Integer.parseInt(tempo_antigo);
    if (new_time < old_time) {
        db.update(TABLE_NAME, args, "NOME = ?", new String[]{nome_equipa});
    }
}

1 个答案:

答案 0 :(得分:-1)

执行类似

的操作
null

因为SQLiteDatabase db = helper.getWritableDatabase(); int id = bla bla String time = bla bla ContentValues contentValues = new ContentValues(); contentValues.put("TeamName", time); db.update("yourTableName", contentValues, "ID = "+id,null); 是您的行ID,id是您的新时间值