2024/03/24(2024春季)
今日学习时长:2小时
代码行数:127行
博客数量:1篇
今天主要是完成了Android stdio项目作业中本地数据库操作部分,并且已经能成功的完成自动登录,记住密码,登录注册等操作。
package com.example.myapplication.dphelper; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import com.example.myapplication.pojo.User; import java.util.List; public class UserDbHelper extends SQLiteOpenHelper { private static final int DB_VERSION = 1; private static final String DB_NAME = "user.db"; private static final String TABLE_NAME = "user"; private static UserDbHelper userDbHelper = null; private SQLiteDatabase userRDB = null;//读操作 private SQLiteDatabase userWDB = null;//写操作 public boolean deleteDatabase(Context context) {//删除数据库 return context.deleteDatabase(DB_NAME); } private UserDbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } //单例模式获取数据库的唯一实例 public static UserDbHelper getInstance(Context context) { if (userDbHelper == null) { userDbHelper = new UserDbHelper(context); } System.out.println(userDbHelper); return userDbHelper; } //打开数据库的读连接 public SQLiteDatabase openReadLink() { if (userRDB == null || !userRDB.isOpen()) { userRDB = userDbHelper.getReadableDatabase(); } Log.d("已经打开了","123"); return userRDB; } //打开数据库的写连接 public SQLiteDatabase openWriteLink() { if (userWDB == null || !userWDB.isOpen()) { userWDB = userDbHelper.getWritableDatabase(); } Log.d("已经打开了","321"); return userWDB; } //关闭数据库 public void close() { if (userRDB != null || userRDB.isOpen()) { userRDB.close(); userRDB = null; } Log.i("close","全部关闭了"); if (userWDB != null || userWDB.isOpen()) { userWDB.close(); userWDB = null; } } //创建数据库 @Override public void onCreate(SQLiteDatabase db) { String sql = "create table if not exists " + TABLE_NAME + "(" + "id Integer primary key autoincrement not null ," + "username varchar(20) not null unique," + "name varchar(20) not null," + "phone varchar(11) not null," + "classroom varchar(20) not null," + "password varchar(20) not null)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } // public User login(User user) // { // String condition="username=? and password=?"; // User user2=null; // Cursor cursor = userRDB.query(TABLE_NAME, null, condition, new String[]{user.getUsername(), user.getPassword()}, null, null, null, null); // if(cursor.moveToNext()) // { // User user1=new User(); // user1.setUsername(cursor.getString(1)); // user1.setName(cursor.getString(2)); // user1.setPhone(cursor.getString(3)); // user1.setClass_no(cursor.getString(4)); // user2=user1; // } // return user2; // } public void print() { System.out.println("7777777777777777777"); } public long insert(User user) { ContentValues values=new ContentValues(); values.put("username",user.getUsername()); values.put("name",user.getName()); values.put("phone",user.getPhone()); values.put("classroom",user.getClass_no()); values.put("password",user.getPassword()); Log.i("这里","hdoahdajdo"); return userWDB.insert(TABLE_NAME,null,values); } }