我正在学习如何使用sql数据库并遇到麻烦。我已经在我的数据库中添加了多个列,但是当我尝试将一个项目放入一个列时,它会将其添加到所有列中。
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_PLACES, "string one");
values.put(MySQLiteHelper.COLUMN_PLACES2, "string two");
System.out.println(values.get(MySQLiteHelper.COLUMN_PLACES));
System.out.println(values.get(MySQLiteHelper.COLUMN_PLACES2));
我在这里做的是把#34;字符串#"进入COLUMN_PLACES并放入"字符串2"进入COLUMN_PLACES2。 当我打电话给系统输出打印时,两个调用都返回"字符串2"。为什么"字符串2"被保存到两列?
编辑:这里有更多的代码:
//这是我的助手类
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
public static final String COLUMN_COMMENT2 = "comment";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_COMMENT
+ " text not null, " + COLUMN_COMMENT2
+ " text not null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
onCreate(db);
}
}
//这是我试图调用代码的地方
public class objDataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_COMMENT,
MySQLiteHelper.COLUMN_COMMENT2};
public objDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public objectaaa createObject(String lol) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_COMMENT, lol);
values.put(MySQLiteHelper.COLUMN_COMMENT2, "second");
System.out.println(values.get(MySQLiteHelper.COLUMN_COMMENT));
System.out.println(values.get(MySQLiteHelper.COLUMN_COMMENT2));
long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
objectaaa newObj = cursorToObj(cursor);
cursor.close();
return newObj;
}
private objectaaa cursorToObj(Cursor cursor) {
objectaaa obj = new objectaaa();
obj.setId(cursor.getLong(0));
obj.setComment(cursor.getString(1));
return obj;
}
public List<objectaaa> getAllObjects() {
List<objectaaa> objects = new ArrayList<objectaaa>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
objectaaa object = cursorToObj(cursor);
System.out.println("FOUND AN OBJECT AT: " + object.getId() + " with the comment: " + object.getComment());
objects.add(object);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return objects;
}
public void deleteObject(objectaaa object) {
long id = object.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}
}
答案 0 :(得分:1)
ContentValues
是一种Map
(内部有HashMap
,每个条目都以其密钥引用。
现在你有两个相同的键:
public static final String COLUMN_COMMENT = "comment";
public static final String COLUMN_COMMENT2 = "comment";
所以很明显,相同的键会在这里获得相同的对象:
values.put(MySQLiteHelper.COLUMN_COMMENT, lol);
values.put(MySQLiteHelper.COLUMN_COMMENT2, "second");
System.out.println(values.get(MySQLiteHelper.COLUMN_COMMENT));
System.out.println(values.get(MySQLiteHelper.COLUMN_COMMENT2));
并且第二个put()
调用将覆盖先前放在同一个键下的值。