如何在sqlite DB中存储登录和注销时间

我在时间类中占用了机器时间,然后将我的登录用户名和密码插入sqlite表中的登录类中,并且我必须插入从另一个类中获取的值,因为我已经创建了一个变量(timedate )在登录时从时间类中获取时间,同时调用方法insertEntry(userName,timedate),但是如果有人对此有任何想法,我无法发送,请帮助我。

这是我的代码

   import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.SimpleCursorAdapter;
    import android.widget.Toast;

    public class HomeActivity extends Activity implements OnClickListener
    {
        public static String userName;
        EditText editTextUserName,editTextpassword;
        Button btnSignIn;
        SimpleCursorAdapter adapter;
        LoginDataBaseAdapter loginDataBaseAdapter;
        String timedate = time.formattedDate;
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.loginpage);

             // create a instance of SQLite Database
             loginDataBaseAdapter=new LoginDataBaseAdapter(this);
             loginDataBaseAdapter=loginDataBaseAdapter.open();
             editTextUserName=(EditText)findViewById(R.id.editTextUserNameToLogin); 
                editTextpassword=(EditText)findViewById(R.id.editTextUserPasswordToLogin); 
                btnSignIn=(Button)findViewById(R.id.buttonSignIn); 
     btnSignIn.setOnClickListener((OnClickListener) this);

    }
    public void onClick(View v) {
        String  userName=editTextUserName.getText().toString();
            @SuppressWarnings("unused")
            String password=editTextpassword.getText().toString();


                    if(userName.equals(""))     
        {
                    Toast.makeText(getApplicationContext(), "userName does not match", Toast.LENGTH_LONG).show();
                    return;
                }
    {   
            loginDataBaseAdapter.insertEntry(userName,timedate);

            Toast.makeText(getApplicationContext(), "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
      Toast.makeText(this, timedate, Toast.LENGTH_SHORT).show();

                Intent name=new Intent(HomeActivity.this,enteredlogin.class);
                startActivity(name);
    }}

DBHelper类,

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class LoginDataBaseAdapter 
{
        static final String DATABASE_NAME = "login.db";
        static final int DATABASE_VERSION = 1;
        public static final int NAME_COLUMN = 1;

        static final String DATABASE_CREATE = "create table "+"LOGIN"+
                                     "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME  text,FORMATTEDDATE text); ";

        public  SQLiteDatabase db;
        // Context of the application using the database.
        private final Context context;

        private DataBaseHelper dbHelper;
        public  LoginDataBaseAdapter(Context _context) 
        {
            context = _context;
            dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public  LoginDataBaseAdapter open() throws SQLException 
        {
            db = dbHelper.getWritableDatabase();
            return this;
        }
        public void close() 
        {
            db.close();
        }

        public  SQLiteDatabase getDatabaseInstance()
        {
            return db;
        }

        public void insertEntry(String userName,String timedate)
        {
           ContentValues newValues = new ContentValues();

            newValues.put("USERNAME", userName);
            newValues.put("FORMATTEDDATE",timedate);

            // Insert the row into your table
            db.insert("LOGIN", null, newValues);
            ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        }

在这里,我通过将格式更改为simpledate自己得到了解决方案,然后对代码进行了一些更改

时间日期方法:

 public static String getDateTime() {
           SimpleDateFormat dateFormat = new SimpleDateFormat(
             "dd/MM/yyyy HH:mm:ss", Locale.getDefault());
            Date date = new Date();
        //  Toast.makeText(dateFormat, Toast.LENGTH_SHORT).show();
            return dateFormat.format(date);
    }

存储在数据库中的数据:

 public  void addLogin(String Name){
         SQLiteDatabase db = this.getWritableDatabase();
         ContentValues values = new ContentValues();
         values.put(KEY_USERNAME,Name);
         values.put(KEY_TIME,getDateTime()); 
         db.insert(TABLE_LOGIN,null,  values);
         db.close(); 
     }

从数据库取回:

 public Data getLogin(int id) {
            SQLiteDatabase db = this.getReadableDatabase();

            Cursor cursor = db.query(TABLE_LOGIN, new String[] { KEY_ID,
                    KEY_USERNAME, KEY_TIME }, KEY_ID + "=?",
                    new String[] { String.valueOf(id) }, null, null, null, null);
            if (cursor != null)
                cursor.moveToFirst();

            Data contact = new Data(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1), cursor.getString(2));
            // return contact
            return contact;
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章