0

重複の可能性:
Android アプリケーションで日時が「今」に設定された SQLite レコードを挿入する方法は?

名前、パスワードなどをデータベースに保存するアプリを開発しています。また、現在の日付と時刻をデータベースに保存し、後で取得したいと考えています。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);
}

}

4

2 に答える 2

2

さまざまな方法に従うことができます。DATETIME('NOW')レコードにタイムスタンプを挿入しながら使用 できますね。または、テーブルの作成中にこれを使用できます。

time TIMESTAMP DEFAULT CURRENT_TIMESTAMP

ここで、timeは現在の時間をとる列です。

詳細についてはこちらをご覧ください

于 2012-12-30T06:26:59.800 に答える
0

「開始日と終了日の間」関数などの日付/時刻データを再利用する予定がある場合は、timestamplikeDATETIME('NOW')を使用して使用することをお勧めしますlong datatype。または、記録用の場合は、を使用currentDateTime.getTime())して使用できますtext datatype

于 2012-12-30T06:38:38.300 に答える