Android Studio—增删改查—登录功能

SQLite数据库的常用操作:

create table if not exists 表名(字段1 类型(长度),字段2 类型(长度),...)//       建表

drop table if exists 表名//删除表

insert into 表名 (字段1,字段2,字段3 ...) values (值1,值2,值3 ...);//
insert into 目标数据表 select * from 源数据表;

delete from 表名 where 条件表达式//

update 表名 set 字段1=值1,字段2=值2... where 条件表达式//

select * from 表名 where 条件表达式//查

简单小实例:(注:“ALT+ENTER”组合键导入class

 非常简单的例子,只用到了一个界面

        1、首先先创建一个DBHelper类(DBOpenHelper.java)

        在这里会执行建库、建表的操作

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context,String name, CursorFactory factory,
int version){
super(context, name, factory, version);
}
@Override
//首次创建数据库的时候调用,一般可以执行建库,建表的操作
//Sqlite没有单独的布尔存储类型,它使用INTEGER作为存储类型,0为false,1为true
public void onCreate(SQLiteDatabase db){
//user table
db.execSQL("create table if not exists user(name text not null,pwd text not null)");
}
@Override//当数据库的版本发生变化时,会自动执行
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
}
}

   2、进入登录界面

 

        在点击登录按钮时,会去数据库里面进行查询,判断账号是否存在(Query查询范例)

//判断账号/密码是否输入正确
public void OnMyLoginClick(View v){
EditText name=(EditText)findViewById(R.id.editText2);
EditText pwd=(EditText)findViewById(R.id.editText4);

//调用DBOpenHelper (user.db是创建的数据库的名称)
DBOpenHelper helper = new DBOpenHelper(this,"test.db",null,1);
SQLiteDatabase db = helper.getWritableDatabase();
//根据画面上输入的账号/密码去数据库中进行查询(user是表名)
Cursor c = db.query("user",null,"name=? and pwd=?",new String[]{name.getText().toString(),pwd.getText().toString()},null,null,null);
//如果有查询到数据
if(c!=null && c.getCount() >= 1){
Toast.makeText(this, "输入正确!", Toast.LENGTH_SHORT).show();
//可以把查询出来的值打印出来在后台显示/查看
String[] cols = c.getColumnNames();
while(c.moveToNext()){
for(String ColumnName:cols){
Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
}
}
c.close();
db.close();
this.finish();
}
//如果没有查询到数据
else{
Toast.makeText(this, "输入错误!", Toast.LENGTH_SHORT).show();
}
}

3、如果账号不存在,则需要去注册一个新账号(Insert新增范例)

//添加
public void OnMyRegistClick(View v){
EditText name=(EditText)findViewById(R.id.editText2);
EditText pwd=(EditText)findViewById(R.id.editText4);
//对用户输入的值的格式进行判断的处理...
//调用DBOpenHelper
DBOpenHelper helper = new DBOpenHelper(this,".db",null,1);
SQLiteDatabase db = helper.getWritableDatabase();
//根据画面上输入的账号去数据库中进行查询
Cursor c = db.query("user",null,"name=?",new String[]{name.getText().toString()},null,null,null);
//如果有查询到数据,则说明账号已存在
if(c!=null && c.getCount() >= 1){
Toast.makeText(this, "该用户已存在", Toast.LENGTH_SHORT).show();
String[] cols = c.getColumnNames();
while(c.moveToNext()){
for(String ColumnName:cols){
Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
}
}
c.close();
}
//如果没有查询到数据,则往数据库中insert一笔数据
else{
//insert data
ContentValues values= new ContentValues();
values.put("name",name.getText().toString());
values.put("pwd",pwd.getText().toString());
long rowid = db.insert("user",null,values);
Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();//提示信息
//this.finish();
}
db.close();
}

 

 

界面代码;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.tzk.sqliteloginapplication.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="13dp"
        android:id="@+id/editText4"
        android:text="000000" />

    <Button
        android:text="登录"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:id="@+id/button"
        android:layout_below="@+id/editText4"
        android:layout_alignStart="@+id/editText4"
        android:onClick="OnMyLoginClick"/>

    <Button
        android:text="注册"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button"
        android:layout_toEndOf="@+id/button"
        android:layout_marginStart="18dp"
        android:id="@+id/button2"
        android:onClick="OnMyRegistClick"/>

</RelativeLayout>

MainActivity:

package com.tzk.sqliteloginapplication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //判断账号/密码是否输入正确
    public void OnMyLoginClick(View v){
        EditText name=(EditText)findViewById(R.id.editText2);
        EditText pwd=(EditText)findViewById(R.id.editText4);

        //调用DBOpenHelper (user.db是创建的数据库的名称)
        DBOpenHelper helper = new DBOpenHelper(this,"test.db",null,1);
        SQLiteDatabase db = helper.getWritableDatabase();
        //根据画面上输入的账号/密码去数据库中进行查询(user是表名)
        Cursor c = db.query("user",null,"name=? and pwd=?",new String[]{name.getText().toString(),pwd.getText().toString()},null,null,null);
        //如果有查询到数据
        if(c!=null && c.getCount() >= 1){
            Toast.makeText(this, "输入正确!", Toast.LENGTH_SHORT).show();
            //可以把查询出来的值打印出来在后台显示/查看
            String[] cols = c.getColumnNames();
            while(c.moveToNext()){
                for(String ColumnName:cols){
                    Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
                }
            }
            c.close();
            db.close();
            this.finish();
        }
        //如果没有查询到数据
        else{
            Toast.makeText(this, "输入错误!", Toast.LENGTH_SHORT).show();
        }
    }

    //添加
    public void OnMyRegistClick(View v){
        EditText name=(EditText)findViewById(R.id.editText2);
        EditText pwd=(EditText)findViewById(R.id.editText4);
        //对用户输入的值的格式进行判断的处理...
        //调用DBOpenHelper
        DBOpenHelper helper = new DBOpenHelper(this,".db",null,1);
        SQLiteDatabase db = helper.getWritableDatabase();
        //根据画面上输入的账号去数据库中进行查询
        Cursor c = db.query("user",null,"name=?",new String[]{name.getText().toString()},null,null,null);
        //如果有查询到数据,则说明账号已存在
        if(c!=null && c.getCount() >= 1){
            Toast.makeText(this, "该用户已存在", Toast.LENGTH_SHORT).show();
            String[] cols = c.getColumnNames();
            while(c.moveToNext()){
                for(String ColumnName:cols){
                    Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
                }
            }
            c.close();
        }
        //如果没有查询到数据,则往数据库中insert一笔数据
        else{
            //insert data
            ContentValues values= new ContentValues();
            values.put("name",name.getText().toString());
            values.put("pwd",pwd.getText().toString());
            long rowid = db.insert("user",null,values);
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();//提示信息
            //this.finish();
        }
        db.close();
    }
}
View Code

代码部分摘自https://www.jb51.net/article/148293.htm

原文地址:https://www.cnblogs.com/sengzhao666/p/11011215.html