无法更新我的SQLiteDatabase中的现有记录

时间:2015-08-30 08:27:45

标签: java android android-sqlite

我创建了一个名为DatabaseHelper.java的独立类,并希望创建一个名为sensors的简单表。

我希望使用name作为where来更新表。但我似乎无法使其发挥作用。请检查我的代码:

public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Readings.db";
public static final String SENSORS_TABLE_NAME = "sensors";
public static final String SENSORS_COLUMN_ID = "Id";
public static final String SENSORS_COLUMN_NAME = "Name";
public static final String SENSORS_COLUMN_1 = "Reading1";
public static final String SENSORS_COLUMN_2 = "Reading2";
public static final String SENSORS_COLUMN_3 = "Reading3";
public static final String SENSORS_COLUMN_4 = "Reading4";
public static final String SENSORS_COLUMN_STATUS = "Status";

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

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE" + SENSORS_TABLE_NAME + "(" + SENSORS_COLUMN_ID + "INTEGER PRIMARY KEY AUTO INCREMENT," + SENSORS_COLUMN_NAME + "STRING" +
                            SENSORS_COLUMN_1 + "STRING," + SENSORS_COLUMN_2 + "FLOAT," +
                    SENSORS_COLUMN_3 + "FLOAT," + SENSORS_COLUMN_4 + "FLOAT," + SENSORS_COLUMN_STATUS + "BOOLEAN)"
    );
    db.setLocale(Locale.getDefault());
    db.setVersion(1);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS" + SENSORS_TABLE_NAME);
    onCreate(db);
}

public boolean insertSensor(String name, boolean status)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("Name", name);
    contentValues.put("Reading1", 0);
    contentValues.put("Reading2", 0);
    contentValues.put("Reading3", 0);
    contentValues.put("Reading4", 0);
    contentValues.put("Status", status);
    db.insert("sensors", null, contentValues);
    return true;
}

public Cursor getData(String Name){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from contacts where Name="+Name+"", null );
    return res;
}

public boolean updateSensor (String Name, float Reading1, float Reading2, float Reading3, float Reading4, boolean Status)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("Reading1", 0);
    contentValues.put("Reading2", 0);
    contentValues.put("Reading3", 0);
    contentValues.put("Reading4", 0);
    contentValues.put("Status", Status);
    db.update("sensors", contentValues, "Name="+Name, new String[]{Name});
    return true;
}
}

[编辑]

public boolean updateSensor(String Name, float Reading1, float Reading2, float Reading3, float Reading4, boolean Status) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("Reading1", Reading1);
    contentValues.put("Reading2", Reading2);
    contentValues.put("Reading3", Reading3);
    contentValues.put("Reading4", Reading4);
    contentValues.put("Status", Status);
    db.update("sensors", contentValues, " = ?", new String[]{Name});
    return true;
}

android.database.sqlite.SQLiteException:near" =&#34 ;:语法错误(代码1):,编译时:UPDATE sensors SET Reading3 = ?, Status = ?, Reading2 = ?, Reading1 = ?,Reading4 =? WHERE =

仍然没有工作 - 累了

卸载应用并多次重新安装。

[编辑]

代码仍无效:

public boolean updateSensor(String Name, float Reading1, float Reading2, float Reading3, float Reading4, boolean Status) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(SENSORS_COLUMN_1, Reading1);
    contentValues.put(SENSORS_COLUMN_2, Reading2);
    contentValues.put(SENSORS_COLUMN_3, Reading3);
    contentValues.put(SENSORS_COLUMN_4, Reading4);
    contentValues.put(SENSORS_COLUMN_STATUS, Status);
    db.update("sensors", contentValues, "Name = ?", new String[]{Name});
    return true;
}

这次的错误是:

android.database.sqlite.SQLiteException:没有这样的列:Reading1(代码1):,编译时:UPDATE传感器SET Reading3 = ?, Status = ?, Reading2 = ?, Reading1 = ?, Reading4 =?姓名=?

[编辑]

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE " + SENSORS_TABLE_NAME + "(" + SENSORS_COLUMN_ID + " INTEGER PRIMARY KEY," + SENSORS_COLUMN_NAME + " TEXT" +
                    SENSORS_COLUMN_1 + " REAL," + SENSORS_COLUMN_2 + " REAL," +
                    SENSORS_COLUMN_3 + " REAL," + SENSORS_COLUMN_4 + " REAL," + SENSORS_COLUMN_STATUS + " INTEGER)"
    );
    db.setLocale(Locale.getDefault());
    db.setVersion(1);
}

2 个答案:

答案 0 :(得分:1)

你的表创建错误:你弄乱了一堆空格

db.execSQL("CREATE TABLE" + SENSORS_TABLE_NAME + "(" + SENSORS_COLUMN_ID + "INTEGER PRIMARY KEY AUTO INCREMENT," + SENSORS_COLUMN_NAME + "STRING" +
                        SENSORS_COLUMN_1 + "STRING," + SENSORS_COLUMN_2 + "FLOAT," +
                SENSORS_COLUMN_3 + "FLOAT," + SENSORS_COLUMN_4 + "FLOAT," + SENSORS_COLUMN_STATUS + "BOOLEAN)"
);

db.execSQL("DROP TABLE IF EXISTS" + SENSORS_TABLE_NAME);

应该是

db.execSQL("CREATE TABLE " + SENSORS_TABLE_NAME + "(" + SENSORS_COLUMN_ID + 
    " INTEGER PRIMARY KEY AUTO INCREMENT," + SENSORS_COLUMN_NAME + " TEXT" +
    SENSORS_COLUMN_1 + " TEXT," + SENSORS_COLUMN_2 + " FLOAT," +
    SENSORS_COLUMN_3 + " FLOAT," + SENSORS_COLUMN_4 + " FLOAT," + 
    SENSORS_COLUMN_STATUS + " BOOLEAN)"
);

db.execSQL("DROP TABLE IF EXISTS " + SENSORS_TABLE_NAME);

注意:保持代码格式良好会有很大帮助。

在处理字符串值时,您还会错过一些'

Cursor res =  db.rawQuery( "select * from contacts where Name="+Name+"", null );

db.update("sensors", contentValues, "Name="+Name, new String[]{Name});

应该是:

Cursor res =  db.rawQuery( "select * from contacts where Name='" + Name + "'", null );

db.update("sensors", contentValues, "Name='" + Name + "'", new String[]{Name});

我的建议是使用绑定参数(使用占位符和String数组逐句替换它们的语法)。

完成所有代码修改后,只需卸载并重新安装该应用程序。

<强> [编辑]

你改变了你的代码(为什么?!),因此我的答案有一半无效 - 我讨厌发生这种情况!!我回到原来的帖子。

好的,现在看到你的更新代码......它错误

这是正确的语法

update(String table, ContentValues values, String whereClause, String[] whereArgs)

这个

db.update("sensors", contentValues, DATABASE_NAME + " = ?" + Name, new String[] {Name});

应该是

db.update("sensors", contentValues, "Name = ?", new String[]{Name});

答案 1 :(得分:-1)

SQLite使用更通用的动态类型系统。请查看必须将每个值存储在SQLite数据库中的存储类: https://www.sqlite.org/lang.html 1. onCreate

  

“创建表格”+ SENSORS_TABLE_NAME +   “(”+ SENSORS_COLUMN_ID +“INTEGER PRIMARY KEY AUTO INCREMENT,”+

看看:creat table - &gt; column-def - &gt;列约束 AUTOINCREMENT只是一个关键词

  

SENSORS_COLUMN_NAME +“STRING”+ SENSORS_COLUMN_1 +“STRING”,+

https://www.sqlite.org/datatype3.html 如果查看关联名称示例,则没有数据类型STRING,也没有。 拿文字,或者,如果你不想给长度,VARCHAR(长度)

  

SENSORS_COLUMN_2 +“FLOAT,”+ SENSORS_COLUMN_3 +“FLOAT,”+   SENSORS_COLUMN_4 +“FLOAT,”+

“FLOAT”没问题,它转换成“REAL”

  

SENSORS_COLUMN_STATUS +“BOOLEAN”“

SQLite没有单独的布尔存储类。相反,布尔值存储为整数0(假)和1(真)。(1.1布尔数据类型) 我想,你的陈述可能是:

db.execSQL("CREATE TABLE" + SENSORS_TABLE_NAME + 
"(" + SENSORS_COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + 
SENSORS_COLUMN_NAME + "TEXT" + 
SENSORS_COLUMN_1 + "TEXT," + 
SENSORS_COLUMN_2 + "FLOAT," +
SENSORS_COLUMN_3 + "FLOAT," + 
SENSORS_COLUMN_4 + "FLOAT," + 
SENSORS_COLUMN_STATUS + "INTEGER )"
);
  1. onCreate中的“setVersion”和“setLocale” 我不知道是否有必要。 在构造函数中,您已经为版本1提供了: super(context,DATABASE_NAME,null,1); 请查看reference / android / database / sqlite / SQLiteOpenHelper 更好的是为版本带一个字符串,因为当你有一个新版本时会采取升级。

    public static final int DB_VERSION = 1;

  2. 而不是:

    super(context, DATABASE_NAME, null, DB_VERSION);