更新表android sqlite时出错

时间:2012-01-25 14:32:04

标签: android sqlite

`// * ** * ** * ** * ** < em> * ** 在Logcat中显示错误 ** * *** android.database.sqlite.SQLiteException:没有这样的列:me :,编译时:UPDATE TemplateTable SET user =?,alert = ?, mycard =?用户=我

// * ** * ** * ** * ** * ** * * 设置活动 * ** * ** * ** * ** *

按钮保存=(按钮)findViewById(R.id.save_templates);         save.setOnClickListener(new View.OnClickListener(){

        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            // Save templates selected 
            CardsDatabase db = new CardsDatabase(Settings.this);
            db.open();
            ContentValues cv=new ContentValues();
            cv.put(CardsDatabase.USERTEMPLATE, "me");
            cv.put(CardsDatabase.ALERT, Integer.toString(alert));
            cv.put(CardsDatabase.MYCARD, Integer.toString(mycard));
            db.updateTemplate(cv);
            db.close();

            Toast.makeText(Settings.this, "Settings saved successfully.", Toast.LENGTH_SHORT).show();

        }
    }); 

// * ** * ** * ** * ** * ** * ** 数据库类 * * * * ** * ** * ** * ***

公共类CardsDatabase {

public static String USERTEMPLATE = "user";
public static String MYCARD = "mycard";
public static String ALERT = "alert";

public static String USER = "user";
public static String NAME = "name";
public static String COMPANY = "company";
public static String TITLE = "title";
public static String ADDRESS = "address";
public static String PHONE = "phone";
public static String CEL = "cel";
public static String EMAIL = "email";
public static String PHOTO = "photo";

public static String MAC = "macaddress";

private static String DATABASE_NAME = "CardsDatabase";
public static String DATABASE_TABLE_TEMPLATE = "TemplateTable";
public static String DATABASE_TABLE_MYTABLE = "MYTable";
public static String DATABASE_TABLE_CONTACTS = "ContactsTable";

private static int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        // Log.d(Tag,"Inner on create called");
        db.execSQL("CREATE TABLE " + DATABASE_TABLE_TEMPLATE + " ("
                + USERTEMPLATE + " TEXT NOT NULL," + MYCARD
                + " TEXT NOT NULL," + ALERT + " TEXT NOT NULL" + ");");

        db.execSQL("CREATE TABLE " + DATABASE_TABLE_MYTABLE + " (" + USER
                + " TEXT NOT NULL," + NAME + " TEXT NOT NULL," + COMPANY
                + " TEXT," + TITLE + " TEXT," + ADDRESS + " TEXT," + PHONE
                + " TEXT NOT NULL," + CEL + " TEXT," + EMAIL
                + " TEXT NOT NULL," + PHOTO + " BLOB" + ");");

        db.execSQL("CREATE TABLE " + DATABASE_TABLE_CONTACTS + " (" + MAC
                + " TEXT NOT NULL" + ");");
    }

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

}

public CardsDatabase(Context c) {
    ourContext = c;
}

public CardsDatabase open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public void createEntry(int type, ContentValues cv) {
    // TODO Auto-generated method stub

    switch (type) {
    case 1:
        ourDatabase.insert(DATABASE_TABLE_TEMPLATE, null, cv);
        break;
    case 2:
        ourDatabase.insert(DATABASE_TABLE_MYTABLE, null, cv);
        break;
    case 3:
        ourDatabase.insert(DATABASE_TABLE_CONTACTS, null, cv);
        break;
    }

}

public Cursor getTemplate() throws SQLException {
    // TODO Auto-generated method stub

    String[] columns = new String[] { USERTEMPLATE, MYCARD, ALERT };

    Cursor c = ourDatabase.query(DATABASE_TABLE_TEMPLATE, columns, null,
            null, null, null, null);

    return c;
}

public Cursor getMyData() throws SQLException {
    // TODO Auto-generated method stub

    String[] columns = new String[] { USER, NAME, COMPANY, TITLE, ADDRESS,
            PHONE, CEL, EMAIL, PHOTO };

    Cursor c = ourDatabase.query(DATABASE_TABLE_MYTABLE, columns, null,
            null, null, null, null);

    return c;
}

public boolean contactExist(String mac) throws SQLException {
    // TODO Auto-generated method stub

    String[] columns = new String[] { MAC };

    Cursor c = ourDatabase.query(DATABASE_TABLE_CONTACTS, columns, MAC
            + "=" + mac, null, null, null, null);

    return c.getCount() > 0 ? true : false;
}

public void updateTemplate(ContentValues cv) {
    String m = "me";
    ourDatabase.update(DATABASE_TABLE_TEMPLATE, cv, USERTEMPLATE + "=" + m,
            null);
}

public void updateMytable(ContentValues cv) {
    String m = "me";
    ourDatabase.update(DATABASE_TABLE_MYTABLE, cv, USER + "=" + m, null);
}

}`

2 个答案:

答案 0 :(得分:1)

您需要提供更多详细信息,但我愿意打赌您更新了架构,而不会在SQLiteOpenHelper实现中增加版本号。

修改

查看代码后,需要在update方法中为WHERE子句添加单引号。如果你不使用引号,db引擎会将它视为表中的一列,因此错误“no such column:me”

你有:

public void updateTemplate(ContentValues cv) {
    String m = "me";
    ourDatabase.update(DATABASE_TABLE_TEMPLATE, cv, USERTEMPLATE + "=" + m,
            null);
}

应该是这样的:

public void updateTemplate(ContentValues cv) {
    String m = "me";
    String WHERE = String.format("%s='%s'", USERTEMPLATE, m);
    ourDatabase.update(DATABASE_TABLE_TEMPLATE, cv, WHERE, null);
}

答案 1 :(得分:0)

在查询中,您需要在“我”周围使用单引号