【问题标题】:How to add datetime function in SQLite [duplicate]如何在 SQLite 中添加日期时间函数 [重复]
【发布时间】:2012-12-30 06:20:55
【问题描述】:

可能重复:
How to insert a SQLite record with a datetime set to ‘now’ in Android application?

我正在开发一个将名称、密码等存储到数据库的应用程序。而且我还想将当前日期和时间存储到数据库中并稍后检索。我无法破解如何在 SQLite 数据库中存储日期和时间。我应该使用日期时间功能吗?如果是,如何在我的后续示例中添加它。请帮忙。

提前致谢。

public class DBAdapter{public static final String ROWID = "_id";
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String PASSWORD = "password";
public static final String TYPE = "type";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "primus_db";
private static final String DATABASE_TABLE = "user_db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table user_db (_id integer primary key autoincrement, "
        + "name text not null, "
        + "email text not null, "
        + "password text not null," + "type text not null );";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

/**
 * 
 */
public DBAdapter(Context ctx) {
    this.context = ctx;
    this.DBHelper = new DBAdapter.DatabaseHelper(this.context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }
}

// ---opens the database---
public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close() {
    DBHelper.close();
}

// ---insert a contact into the database---
public long insertContact(String name, String email, String password,
        String type) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(NAME, name);
    initialValues.put(EMAIL, email);
    initialValues.put(PASSWORD, password);
    initialValues.put(TYPE, type);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// ---retrieves all the contacts---
public Cursor getAllContacts() {
    return db.query(DATABASE_TABLE, new String[] { ROWID, NAME, EMAIL,
            PASSWORD, TYPE }, null, null, null, null, null);
}

}

【问题讨论】:

    标签: android sqlite timestamp


    【解决方案1】:

    您可以遵循不同的方式。您可以在将时间戳插入记录时使用DATETIME('NOW'),不是吗?或者您可以在创建表时使用它。

    time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    

    其中 time 是采用当前时间的列。

    更多详情请咨询this

    【讨论】:

      【解决方案2】:

      如果您计划重复使用“开始日期和结束日期之间”函数或类似的日期/时间数据,我建议您使用 timestamp(如 DATETIME('NOW'))并使用 long datatype。或者如果只是为了记录,你可以使用currentDateTime.getTime())text datatype

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-03
        • 2019-10-30
        • 2017-01-16
        • 2020-11-29
        • 1970-01-01
        • 2015-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多