简体   繁体   English

更新表android sqlite时出错

[英]error while updating table android sqlite

`// * ** * ** * ** * ** * ** `// * ** * ** * ** * ** * ** Error showing in Logcat 在Logcat中显示错误 ** * *** android.database.sqlite.SQLiteException: no such column: me: , while compiling: UPDATE TemplateTable SET user=?, alert=?, mycard=? ** * *** android.database.sqlite.SQLiteException:否这样的栏:me :,而在编译时:UPDATE TemplateTable SET user = ?、 alert = ?、 mycard =? WHERE user=me 用户=我

// * ** * ** * ** * ** * ** * * Settings Activity * ** * ** * ** * ** * // * ** * ** * ** * ** * ** * *设置活动* ** * ** * ** * ** *

Button save=(Button) findViewById(R.id.save_templates); 按钮save =(按钮)findViewById(R.id.save_templates); save.setOnClickListener(new View.OnClickListener() { 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();

        }
    }); 

// * ** * ** * ** * ** * ** * ** // * ** * ** * ** * ** * ** * ** Database class * ** * ** * ** * ** * *** 数据库类 * ** * ** * ** * ** * ***

public class CardsDatabase { 公共类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);
}

}` }`

You need to provide more details, but I'd be willing to bet you updated your schema without incrementing your version number in your SQLiteOpenHelper implementation. 您需要提供更多详细信息,但是我愿意打赌您在不增加SQLiteOpenHelper实现中的版本号的情况下更新了架构。

EDIT 编辑

After looking through your code, you need to add single quotes to your WHERE clause in the update method. 浏览完代码后,您需要在update方法中的WHERE子句中添加单引号。 If you don't use quotes, the db engine treats it as a column in your table, hence the error "no such column: me" 如果您不使用引号,则数据库引擎会将其视为表中的一列,因此出现错误“没有这样的列:我”

You have: 你有:

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

should be something like: 应该是这样的:

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

您的查询中需要在“我”周围加上单引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM