我的bean类中有两个日期变量
public class bean {
private Date dos;
private Date doe;
}
在我的DAO课程中
// columns in my sqlite database table
public class TaskDAO extends SQLiteOpenHelper {
public static final String C_DOS = "d1";
public static final String C_DOE = "d2";
@Override
public void onCreate(SQLiteDatabase db) {
StringBuilder sb = new StringBuilder();
sb.append("CREATE TABLE "+TBNAME);
sb.append(" ("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, ");
sb.append(C_DOS+" INTEGER,");
sb.append(C_DOE+" INTEGER)");
String sql = sb.toString();
db.execSQL(sql);
}
void addbean(bean b)
{
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(C_DOS,b.getdos);
values.put(C_DOE,b.getdoe);
database.insert(TBNAME,null,values);
}
ContentValues put给出了不兼容类型的错误。 我如何解决这个问题,以便我可以提取后者在其他DAO CRUD函数中使用它?
答案 0 :(得分:0)
使用Long
作为类型。
因此,您可以将mDate.getTime()
存储在您的数据库中,并使用new Date(millies)
public boolean addBean(Bean bean) {
long result;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID, bean.getId());
values.put(C_DOS, formatDateToLong(bean.getDos()));
values.put(C_DOE, formatDateToLong(bean.getDoe()));
result = db.insert(TBNAME, null, values);
db.close();
return result > 0;
}
因此您可以使用辅助函数。
public static Date formatLongToDate(Long date){
return new Date(date);
}
public static Long formatDateToLong(Date date){
return date.getTime();
}
如果您使用数据库,最好在模型中使用id:
public class Bean {
private Long id;
private Date dos;
private Date doe;
}
要从数据库中获取数据,请尝试:
public Bean getBean(Long id) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TBNAME, new String[] { ID, C_DOS, C_DOE}, ID + " = " + id, null, null, null, null);
Bean mBean = null;
if (cursor.moveToFirst()){
new mBean (
cursor.getLong(0),
DateHelper.formatLongToDate(cursor.getLong(1)),
DateHelper.formatLongToDate(cursor.getLong(2))
);
}
cursor.close();
db.close();
return mBean ;
}
答案 1 :(得分:0)
你可以用这个
public class SQLiteHandler extends SQLiteOpenHelper {
private static final String TAG = SQLiteHandler.class.getSimpleName();
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "user_data";
// Login table name
private static final String TABLE_USER = "user";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";
public SQLiteHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_UID + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE," + KEY_NAME + " TEXT,"
+ KEY_CREATED_AT + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
* */
public void addUser(String uid, String email, String name, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_UID, uid); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_NAME, name); // Email
values.put(KEY_CREATED_AT, created_at); // Created At
// Inserting Row
long id = db.insert(TABLE_USER, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
}
/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_USER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("uid", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("name", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
/**
* Re crate database Delete all tables and create them again
* */
public void deleteUsers() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_USER, null, null);
db.close();
Log.d(TAG, "Deleted all user info from sqlite");
}
}
之后你可以插入这样的数据......
SQLiteHandler db = new SQLiteHandler(this);
db.addUser(_uni_id,_email,_username,_reg_date);
你也可以获得这样的数据......
SQLiteHandler db = new SQLiteHandler(getApplicationContext());
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
String name = user.get("username_key");
for drop all(您可以编辑SQLiteHandler类来删除特定行)...
db.deleteUsers();
我希望它对你有用
答案 2 :(得分:0)
我不明白以下代码:
values.put(C_DOS,b.getdos);
values.put(C_DOE,b.getdoe);
是吗
values.put(C_DOS,b.getdos());
values.put(C_DOE,b.getdoe());
你正试图做什么?
如果要将时间存储为整数,请将以上两行修改为:
values.put(C_DOS,b.getdos().getTime());
values.put(C_DOE,b.getdoe().getTime());
我假设您getdos
班级中定义了getdoe
和bean
,并且返回类型为Date
。
是否可以编辑您的问题以包含您正在显示的错误消息?
在编译时不会检查使用ContentValues
添加到put
的值的类型,因此,不兼容的类型错误可能是由其他内容引起的,而不是由于Date
类型引起的。