android stuio中的MySQLiteOpenHelper

时间: 2023-11-28 19:49:42 浏览: 29
MySQLiteOpenHelper 是 Android 中用于管理 SQLite 数据库的一个辅助类,它提供了创建、升级和打开数据库的方法。 当我们需要使用 SQLite 数据库时,我们可以继承 MySQLiteOpenHelper 类,然后重写其中的 onCreate()、onUpgrade() 和 onDowngrade() 方法。 onCreate() 方法在第一次创建数据库时被调用,我们可以在这个方法中创建数据库的表格和初始化数据。 onUpgrade() 方法在数据库版本升级时被调用,我们可以在这个方法中做一些数据迁移的工作或者修改表格结构。 onDowngrade() 方法在数据库版本降级时被调用,通常情况下我们不需要实现这个方法。 在实现 MySQLiteOpenHelper 类时,我们需要传入数据库名称和版本号,同时还可以在构造函数中传入一个 CursorFactory 对象,用于创建 Cursor 对象。例如: ```java public class MyDatabaseHelper extends MySQLiteOpenHelper { private static final String DB_NAME = "my_database.db"; private static final int DB_VERSION = 1; public MyDatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // 创建数据库表格和初始化数据 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // 数据库版本升级时的数据迁移或表格结构修改 } } ``` 在使用 MySQLiteOpenHelper 类时,我们可以通过 getWritableDatabase() 或者 getReadableDatabase() 方法获取一个 SQLiteDatabase 对象,然后通过这个对象进行数据库的增删改查等操作。例如: ```java MyDatabaseHelper dbHelper = new MyDatabaseHelper(context); SQLiteDatabase db = dbHelper.getWritableDatabase(); // 执行数据库操作 db.close(); ```

相关推荐

package com.example.myapplication; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; import com.example.myapplication.pass.User; public class MYsqliteopenhelper extends SQLiteOpenHelper { private static final String DB_NAME="MYsqlite.db"; private static final String create_users="create table users(name varchar(32) primary key,password varchar(32));"; private static final String create_int="create table int(name varchar(32)primary key,content varchar);"; public MYsqliteopenhelper(@Nullable Context context) { super(context, DB_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(create_users); sqLiteDatabase.execSQL(create_int); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } public long register(User u){ SQLiteDatabase db=getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put("name",u.getName()); cv.put("password",u.getPassword()); long users=db.insert("users",null,cv); return users; } public boolean change(String name){ SQLiteDatabase db1=getReadableDatabase(); boolean result=false; Cursor users =db1.query("users",null,"name like ?",new String[]{name},null,null,null); if(users !=null){ while (users.moveToNext()){ return result; } } return false; } public boolean login(String name,String password){ SQLiteDatabase db1=getReadableDatabase(); boolean result=false; Cursor users =db1.query("users",null,"name like ?",new String[]{name},null,null,null); if(users !=null){ while (users.moveToNext()){ String password1=users.getString(1); result=password1.equals(password); return result; } } return false; } }

请解释public void login(View view) {//这里对应登陆按钮里的onclick,按钮按下便会调用此函数 EditText account = findViewById(R.id.account);//获取id为account的EditText输入值 EditText password = findViewById(R.id.password);//获取id为password的EditText输入值 String got_account = account.getText().toString();//将得到的值转为字符串 String got_password = password.getText().toString(); SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this); SQLiteDatabase db = helper.getReadableDatabase();//登陆用到的是读操作,这里用写操作也不会有问题 if (db.isOpen()) {//判断数据库是否成功打开 Cursor cursor = db.rawQuery("select * from users", null);//取users数据表中所有用户数据,cursor是迭代游标,用于遍历操作 while (cursor.moveToNext()) {//遍历所有用户数据 @SuppressLint("Range") String _account = cursor.getString(cursor.getColumnIndex("_account"));//获取每一个用户的账号 @SuppressLint("Range") String _password = cursor.getString(cursor.getColumnIndex("_password"));//获取每一个用户的密码 if (Objects.equals(_account, got_account) && Objects.equals(_password, got_password)) {//判断此次遍历到的用户账号和密码是否和用户输入的账号和密码相同 Log.e("login success", _account); Intent ma2 = new Intent(loginActivity.this, loginsuccessActivity.class);//跳转至登陆成功的界面,MainActivity是当前界面的java文件名,MainActivity2是要跳转的界面对应的java文件 startActivity(ma2); } } cursor.close();//关闭迭代游标 db.close();//关闭数据库 } }

最新推荐

recommend-type

H11111111111111111111111111111111111111111111111111111

sadfasfdgsdgsdfgsdgsdg
recommend-type

16路舵机机械臂.zip16路舵机机械臂_.zip

16路舵机机械臂.zip16路舵机机械臂_
recommend-type

Python_来自微软的熟悉的、友好的、现代的表情符号集合.zip

Python_来自微软的熟悉的、友好的、现代的表情符号集合
recommend-type

Fortran语言语法快速入门

Fortran语言语法快速入门 简单教程。
recommend-type

Python_The official repo of Qwen 通义千问 chat pretrained larg.zip

Python_The official repo of Qwen 通义千问 chat pretrained larg
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略

![MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略](http://xiaoyuge.work/explain-sql/index/2.png) # 1. MySQL数据库性能优化概述** MySQL数据库性能优化是一项至关重要的任务,可以显著提高应用程序的响应时间和整体用户体验。优化涉及识别和解决影响数据库性能的因素,包括硬件资源瓶颈、软件配置不当和数据库设计缺陷。通过采取适当的优化策略,可以显著提升数据库性能,满足业务需求并提高用户满意度。 # 2. MySQL数据库性能下降的幕后真凶 ### 2.1 硬件资源瓶颈 #### 2.1.1 CPU利用率过高 **症状:
recommend-type

如何在unity创建按钮

在 Unity 中创建按钮的步骤如下: 1. 在 Unity 中创建一个 UI Canvas,选择 GameObject -> UI -> Canvas。 2. 在 Canvas 中创建一个按钮,选择 GameObject -> UI -> Button。 3. 在场景视图中调整按钮的位置和大小。 4. 在 Inspector 中设置按钮的文本、颜色、字体等属性。 5. 添加按钮的响应事件,选择按钮,在 Inspector 的 On Click () 中添加相应的方法。 这样就可以创建一个按钮了,你可以在游戏中使用它来触发相应的操作。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。