sqlite数据的使用很常见呢,我们简单学习一下app的登录、注册、修改、删除用户吧!@H_404_2@
@H_404_2@
还有2个图片按钮(虽然不是很好看,但是将就吧→。→)@H_404_2@
@H_404_2@
EditView输入框的监听事件,2张图片的切换@H_404_2@
(为了找22,33的图片,我反编译了 bilibii 的app,想不到图片是分为4张的 →。→)@H_404_2@
一、entity:User 实体类@H_404_2@
自己写 →。→@H_404_2@
private int @H_404_2@id@H_404_2@;
@H_404_2@private @H_404_2@String uname@H_404_2@;
@H_404_2@private @H_404_2@String upwd@H_404_2@;
@H_404_2@private int @H_404_2@isDel@H_404_2@;@H_404_2@
二、dao包:@H_404_2@
DBOpenHelper:建表@H_404_2@
package @H_404_2@example.com.user_sqlite.dao;
@H_404_2@
@H_404_2@import @H_404_2@android.content.Context;
@H_404_2@import @H_404_2@android.database.sqlite.sqliteDatabase;
@H_404_2@import @H_404_2@android.database.sqlite.sqliteDatabase.CursorFactory;
@H_404_2@import @H_404_2@android.database.sqlite.sqliteOpenHelper;
@H_404_2@
@H_404_2@/** @H_404_2@ * Created by Administrator on 2017/4/5. @H_404_2@ */ @H_404_2@public class @H_404_2@DBOpenHelper extends @H_404_2@sqliteOpenHelper{
public @H_404_2@DBOpenHelper(Context context,@H_404_2@String name,@H_404_2@CursorFactory factory,int @H_404_2@version) {
super@H_404_2@(context,@H_404_2@"sqliteTest.db"@H_404_2@,null,@H_404_2@1@H_404_2@);
@H_404_2@ @H_404_2@}
@Override
@H_404_2@ @H_404_2@public void @H_404_2@onCreate@H_404_2@(sqliteDatabase sqliteDatabase) {
String sql=
"create table if not exists t_user("@H_404_2@+
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"@H_404_2@+
"uname VARCHAR(255),"@H_404_2@+
"upwd VARCHAR(255),"@H_404_2@+
"isDel INTEGER DEFAULT 0"@H_404_2@+
")"@H_404_2@;
@H_404_2@ @H_404_2@sqliteDatabase.execsql(sql);
@H_404_2@ @H_404_2@}
@Override
@H_404_2@ @H_404_2@public void @H_404_2@onUpgrade@H_404_2@(sqliteDatabase arg0,int @H_404_2@arg1,int @H_404_2@arg2) {
}
}
package @H_404_2@example.com.user_sqlite.dao;
@H_404_2@
@H_404_2@import @H_404_2@android.content.Context;
@H_404_2@import @H_404_2@android.database.Cursor;
@H_404_2@import @H_404_2@android.database.sqlite.sqliteDatabase;
@H_404_2@import @H_404_2@java.lang.reflect.Array;
@H_404_2@import @H_404_2@java.util.ArrayList;
@H_404_2@
@H_404_2@import @H_404_2@example.com.user_sqlite.entity.User;
@H_404_2@
@H_404_2@/** @H_404_2@ * Created by Administrator on 2017/3/19. @H_404_2@ */ @H_404_2@public class @H_404_2@UserDao {
private @H_404_2@DBOpenHelper dbOpenHelper@H_404_2@; @H_404_2@//创建DBOpenHelper对象
@H_404_2@ @H_404_2@private @H_404_2@sqliteDatabase sqliteDatabase@H_404_2@; @H_404_2@//创建sqliteDatabase对象
@H_404_2@ @H_404_2@public @H_404_2@UserDao(Context context){ //定义构造函数
@H_404_2@ @H_404_2@dbOpenHelper@H_404_2@=new @H_404_2@DBOpenHelper(context,@H_404_2@0@H_404_2@); @H_404_2@//初始化DBOpenHelper对象
@H_404_2@ @H_404_2@}
//插入数据
@H_404_2@ @H_404_2@public void @H_404_2@dbInsert@H_404_2@(String uname,@H_404_2@String upwd){
sqliteDatabase @H_404_2@=dbOpenHelper@H_404_2@.getWritableDatabase();
@H_404_2@ @H_404_2@String sql="insert into t_user(uname,upwd,isDel) values (?,?,0)"@H_404_2@;
@H_404_2@ @H_404_2@Object bindArgs[] = new @H_404_2@Object[]{ uname,@H_404_2@upwd};
@H_404_2@ @H_404_2@sqliteDatabase@H_404_2@.execsql(sql,@H_404_2@bindArgs);
@H_404_2@ @H_404_2@}
//查询数据
@H_404_2@ @H_404_2@public int @H_404_2@dbGetUserSize@H_404_2@(){
sqliteDatabase @H_404_2@= dbOpenHelper@H_404_2@.getWritableDatabase();
@H_404_2@ @H_404_2@String sql="select count(*) from t_user where isDel=0"@H_404_2@;
@H_404_2@ @H_404_2@Cursor cursor = sqliteDatabase@H_404_2@.rawQuery(sql,null@H_404_2@);
@H_404_2@ if @H_404_2@(cursor.moveToNext()) //判断Cursor中是否有数据
@H_404_2@ @H_404_2@{
return @H_404_2@cursor.getInt(0@H_404_2@); @H_404_2@//返回总记录数
@H_404_2@ @H_404_2@}
return @H_404_2@0@H_404_2@; @H_404_2@//如果没有数据,则返回0
@H_404_2@ @H_404_2@}
public @H_404_2@User dbQueryOneByUsername@H_404_2@(String uname){
sqliteDatabase @H_404_2@= dbOpenHelper@H_404_2@.getWritableDatabase();
@H_404_2@ @H_404_2@String sql="select * from t_user where uname=? and isDel=0"@H_404_2@;
@H_404_2@ @H_404_2@String[] selectionArgs = new @H_404_2@String[]{ uname };
@H_404_2@ @H_404_2@Cursor cursor = sqliteDatabase@H_404_2@.rawQuery(sql,@H_404_2@selectionArgs);
@H_404_2@ if @H_404_2@(cursor.moveToNext()) //判断Cursor中是否有数据
@H_404_2@ @H_404_2@{
User user=new @H_404_2@User();
@H_404_2@ @H_404_2@user.setId(cursor.getInt(cursor.getColumnIndex("id"@H_404_2@)));
@H_404_2@ @H_404_2@user.setUname(cursor.getString(cursor.getColumnIndex("uname"@H_404_2@)));
@H_404_2@ @H_404_2@user.setUpwd(cursor.getString(cursor.getColumnIndex("upwd"@H_404_2@)));
@H_404_2@ return @H_404_2@user; @H_404_2@//返回总记录行数
@H_404_2@ @H_404_2@}
return null;
@H_404_2@ @H_404_2@}
//修改密码
@H_404_2@ @H_404_2@public void @H_404_2@dbUpdatePassword@H_404_2@(String uname,@H_404_2@String newUpwd){
sqliteDatabase @H_404_2@= dbOpenHelper@H_404_2@.getWritableDatabase();
@H_404_2@ @H_404_2@String sql="update t_user set upwd=? where uname=? and isDel=0"@H_404_2@;
@H_404_2@ @H_404_2@Object bindArgs[] = new @H_404_2@Object[]{ newUpwd,@H_404_2@uname };
@H_404_2@ @H_404_2@sqliteDatabase@H_404_2@.execsql(sql,@H_404_2@bindArgs);
@H_404_2@ @H_404_2@}
//查询新增数
@H_404_2@ @H_404_2@public @H_404_2@ArrayList<User> dbQueryAll@H_404_2@(){
ArrayList<User> userArrayList = new @H_404_2@ArrayList<User>();
@H_404_2@ @H_404_2@sqliteDatabase @H_404_2@= dbOpenHelper@H_404_2@.getWritableDatabase();
@H_404_2@ @H_404_2@String sql="select * from t_user where isDel=0"@H_404_2@;
@H_404_2@ @H_404_2@Cursor cursor= sqliteDatabase@H_404_2@.rawQuery(sql,null@H_404_2@);
@H_404_2@ for @H_404_2@(cursor.moveToFirst(); @H_404_2@!(cursor.isAfterLast()); @H_404_2@cursor.moveToNext()){
if @H_404_2@(cursor.getInt(cursor.getColumnIndex("isDel"@H_404_2@))!=1@H_404_2@){
User user=new @H_404_2@User();
@H_404_2@ @H_404_2@user.setId(cursor.getInt(cursor.getColumnIndex("id"@H_404_2@)));
@H_404_2@ @H_404_2@user.setUname(cursor.getString(cursor.getColumnIndex("uname"@H_404_2@)));
@H_404_2@ @H_404_2@user.setUpwd(cursor.getString(cursor.getColumnIndex("upwd"@H_404_2@)));
@H_404_2@ @H_404_2@userArrayList.add(user);
@H_404_2@ @H_404_2@}
}
return @H_404_2@userArrayList;
@H_404_2@ @H_404_2@}
//更新数据
@H_404_2@ @H_404_2@public void @H_404_2@dbDeleteUser@H_404_2@(int @H_404_2@id){
sqliteDatabase @H_404_2@= dbOpenHelper@H_404_2@.getWritableDatabase();
@H_404_2@ @H_404_2@String sql="update t_user set isDel=1 where id=?"@H_404_2@;
@H_404_2@ @H_404_2@Object bindArgs[] = new @H_404_2@Object[]{ id };
@H_404_2@ @H_404_2@sqliteDatabase@H_404_2@.execsql(sql,@H_404_2@bindArgs);
@H_404_2@ @H_404_2@}
}
1、MainActivity@H_404_2@
package @H_404_2@example.com.user_sqlite;
@H_404_2@
@H_404_2@import @H_404_2@android.os.Bundle;
@H_404_2@import @H_404_2@android.view.View;
@H_404_2@import @H_404_2@android.view.View.OnClickListener;
@H_404_2@import @H_404_2@android.widget.Button;
@H_404_2@import @H_404_2@android.widget.EditText;
@H_404_2@import @H_404_2@android.widget.ImageView;
@H_404_2@import @H_404_2@android.widget.Toast;
@H_404_2@import @H_404_2@android.app.Activity;
@H_404_2@import @H_404_2@android.content.Intent;
@H_404_2@
@H_404_2@import @H_404_2@example.com.user_sqlite.dao.UserDao;
@H_404_2@import @H_404_2@example.com.user_sqlite.entity.User;
@H_404_2@
@H_404_2@public class @H_404_2@MainActivity extends @H_404_2@Activity {
private @H_404_2@ImageView bili1@H_404_2@,@H_404_2@bili2@H_404_2@;
@H_404_2@// 用户登录
@H_404_2@ @H_404_2@private @H_404_2@EditText editTextA1@H_404_2@;
@H_404_2@ private @H_404_2@EditText editTextA2@H_404_2@;
@H_404_2@ private @H_404_2@Button buttonA1@H_404_2@;
@H_404_2@
@H_404_2@ @H_404_2@// 数据库操作类
@H_404_2@ @H_404_2@private @H_404_2@UserDao userDao@H_404_2@;
@H_404_2@
@H_404_2@ @H_404_2@@Override
@H_404_2@ @H_404_2@protected void @H_404_2@onCreate@H_404_2@(Bundle savedInstanceState) {
super@H_404_2@.onCreate(savedInstanceState);
@H_404_2@ @H_404_2@setContentView(R.layout.activity_main);
@H_404_2@
@H_404_2@// 注册组件
@H_404_2@ @H_404_2@userDao = new @H_404_2@UserDao(this@H_404_2@);
@H_404_2@ @H_404_2@editTextA1 = (EditText) findViewById(R.id.editTextA1);
@H_404_2@ @H_404_2@editTextA2 = (EditText) findViewById(R.id.editTextA2);
@H_404_2@
@H_404_2@ @H_404_2@buttonA1 = (Button) findViewById(R.id.buttonA1);
@H_404_2@ @H_404_2@bili1=(ImageView) findViewById(R.id.bili1);
@H_404_2@ @H_404_2@bili2=(ImageView) findViewById(R.id.bili2);
@H_404_2@
@H_404_2@// 用户登录
@H_404_2@ @H_404_2@buttonA1.setOnClickListener(new @H_404_2@OnClickListener() {
@Override
public void @H_404_2@onClick(View v) {
String uname = editTextA1.getText() + ""@H_404_2@;
@H_404_2@ @H_404_2@String upwd = editTextA2.getText() + ""@H_404_2@;
@H_404_2@//一个是输入不能为空,一个是不能只打空格null
@H_404_2@ @H_404_2@if @H_404_2@(uname.equals(null@H_404_2@) || uname == "" @H_404_2@|| upwd.equals(null@H_404_2@) || upwd == ""@H_404_2@) {
Toast.makeText(MainActivity.this,@H_404_2@"用户名或密码不得为空!"@H_404_2@,@H_404_2@Toast.LENGTH_SHORT).show();
@H_404_2@ @H_404_2@} else @H_404_2@{
User user = userDao.dbQueryOneByUsername(uname);
@H_404_2@ if @H_404_2@(userDao.dbQueryOneByUsername(uname) == null@H_404_2@) {
Toast.makeText(MainActivity.this,@H_404_2@"此用户不存在!"@H_404_2@,@H_404_2@Toast.LENGTH_SHORT).show();
@H_404_2@ @H_404_2@} else @H_404_2@{
if @H_404_2@(!user.getUpwd().equals(upwd)) {
Toast.makeText(MainActivity.this,@H_404_2@"密码错误!"@H_404_2@,@H_404_2@Toast.LENGTH_SHORT).show();
@H_404_2@ @H_404_2@} else @H_404_2@{
Toast.makeText(MainActivity.this,@H_404_2@"登录成功!"@H_404_2@,@H_404_2@Toast.LENGTH_SHORT).show();
@H_404_2@ @H_404_2@Intent intent = new @H_404_2@Intent(MainActivity.this,@H_404_2@TableActivity.class@H_404_2@);
@H_404_2@ @H_404_2@startActivity(intent);
@H_404_2@ @H_404_2@}
}
}
}
});
@H_404_2@//输入框 获取/失去 焦点切换图片
@H_404_2@ @H_404_2@editTextA1@H_404_2@.setOnFocusChangeListener(new @H_404_2@android.view.View.OnFocusChangeListener(){
@Override
@H_404_2@ @H_404_2@public void @H_404_2@onFocusChange@H_404_2@(View v,boolean @H_404_2@hasFocus) {
//获取焦点的时候,图片一显示,图片二隐藏
@H_404_2@ @H_404_2@if @H_404_2@(hasFocus){
bili1@H_404_2@.setVisibility(v.VISIBLE@H_404_2@);
@H_404_2@ @H_404_2@bili2@H_404_2@.setVisibility(v.GONE@H_404_2@);
@H_404_2@ @H_404_2@}else @H_404_2@{
bili2@H_404_2@.setVisibility(v.VISIBLE@H_404_2@);
@H_404_2@ @H_404_2@bili1@H_404_2@.setVisibility(v.GONE@H_404_2@);
@H_404_2@ @H_404_2@}
}
});
@H_404_2@
@H_404_2@ @H_404_2@}
//注册
@H_404_2@ @H_404_2@public void @H_404_2@Login@H_404_2@(View view){
Intent intent = new @H_404_2@Intent(this,@H_404_2@LoginActivity.class@H_404_2@);
@H_404_2@ @H_404_2@startActivity(intent);
@H_404_2@ @H_404_2@}
//查看列表信息
@H_404_2@ @H_404_2@public void @H_404_2@Look(View view){
Intent intent = new @H_404_2@Intent(this,@H_404_2@TableActivity.class@H_404_2@);
@H_404_2@ @H_404_2@startActivity(intent);
@H_404_2@ @H_404_2@}
}
1.1:activity_main.xml@H_404_2@
<?@H_404_2@xml version=@H_404_2@"1.0" @H_404_2@encoding=@H_404_2@"utf-8"@H_404_2@?>
@H_404_2@<ScrollView @H_404_2@xmlns:@H_404_2@android@H_404_2@=@H_404_2@"http://schemas.android.com/apk/res/android"
@H_404_2@ @H_404_2@xmlns:@H_404_2@tools@H_404_2@=@H_404_2@"http://schemas.android.com/tools"
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:padding=@H_404_2@"10dp"
@H_404_2@ @H_404_2@android@H_404_2@:background=@H_404_2@"@android:color/white"
@H_404_2@ @H_404_2@tools@H_404_2@:context=@H_404_2@"example.com.user_sqlite.MainActivity"@H_404_2@>
@H_404_2@<LinearLayout
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:orientation=@H_404_2@"vertical"@H_404_2@>
@H_404_2@<!-- 用户登录: -->
@H_404_2@ @H_404_2@<ImageView
@H_404_2@ @H_404_2@android@H_404_2@:id=@H_404_2@"@+id/bili1"
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"wrap_content"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"wrap_content"
@H_404_2@ @H_404_2@android@H_404_2@:src=@H_404_2@"@drawable/bi22"
@H_404_2@ @H_404_2@android@H_404_2@:layout_gravity=@H_404_2@"center_horizontal"@H_404_2@/>
@H_404_2@ <ImageView
@H_404_2@ @H_404_2@android@H_404_2@:id=@H_404_2@"@+id/bili2"
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"wrap_content"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"wrap_content"
@H_404_2@ @H_404_2@android@H_404_2@:src=@H_404_2@"@drawable/bi33"
@H_404_2@ @H_404_2@android@H_404_2@:visibility=@H_404_2@"gone"
@H_404_2@ @H_404_2@android@H_404_2@:layout_gravity=@H_404_2@"center_horizontal"@H_404_2@/>
@H_404_2@<LinearLayout
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"wrap_content"
@H_404_2@ @H_404_2@android@H_404_2@:orientation=@H_404_2@"vertical"
@H_404_2@ @H_404_2@android@H_404_2@:layout_marginTop=@H_404_2@"10dp"@H_404_2@>
@H_404_2@
@H_404_2@ <TextView
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"wrap_content"
@H_404_2@ @H_404_2@android@H_404_2@:text=@H_404_2@"@string/textViewA"
@H_404_2@ @H_404_2@android@H_404_2@:textSize=@H_404_2@"24dp"
@H_404_2@ @H_404_2@android@H_404_2@:gravity=@H_404_2@"center_horizontal"
@H_404_2@ @H_404_2@android@H_404_2@:textColor=@H_404_2@"@android:color/holo_blue_light"@H_404_2@/>
@H_404_2@
@H_404_2@ <LinearLayout
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"wrap_content"
@H_404_2@ @H_404_2@android@H_404_2@:orientation=@H_404_2@"horizontal"
@H_404_2@ @H_404_2@android@H_404_2@:layout_marginRight=@H_404_2@"20dp"
@H_404_2@ @H_404_2@android@H_404_2@:layout_marginTop=@H_404_2@"10dp"@H_404_2@>
@H_404_2@
@H_404_2@ <TextView
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"80dp"
@H_404_2@ @H_404_2@android@H_404_2@:gravity=@H_404_2@"center_vertical"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:text=@H_404_2@"@string/textView1"
@H_404_2@ @H_404_2@android@H_404_2@:textSize=@H_404_2@"18dp"
@H_404_2@ @H_404_2@android@H_404_2@:textColor=@H_404_2@"@android:color/holo_blue_light"@H_404_2@/>
@H_404_2@
@H_404_2@ <EditText
@H_404_2@ @H_404_2@android@H_404_2@:id=@H_404_2@"@+id/editTextA1"
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:inputType=@H_404_2@"text"
@H_404_2@ @H_404_2@android@H_404_2@:textSize=@H_404_2@"18dp"
@H_404_2@
@H_404_2@ @H_404_2@android@H_404_2@:background=@H_404_2@"@drawable/editbg"@H_404_2@/>
@H_404_2@ </LinearLayout>
@H_404_2@
@H_404_2@ <LinearLayout
@H_404_2@ @H_404_2@android@H_404_2@:layout_width=@H_404_2@"match_parent"
@H_404_2@ @H_404_2@android@H_404_2@:layout_height=@H_404_2@"wrap_content"
@H_404_2@ @H_404_2@android@H_404_2@:orientation=@H_404_2@"horizontal"
@H_404_2@ @H_404_2@android@H_404_2@:layout_marginTop=@H_404_2@"10dp"
@H_404_2@ @H_404_2@android@H_404_2@:layout_marginRight=@H_404_2@"20dp"@H_