sqlite是安卓自带的数据库,老师讲这个的时候耍去了,就。。。。
我运用数据库是利用导包运用的,源码如下:
user类
package com.jiang.jzj_qq.user;
public class User {
private String account;
private String name;
private String sex;
private int age;
private String adress;
private String email;
private String phone;
private String password;
public User(String account, String name, String sex, int age, String phone, String password) {
this.account = account;
this.name = name;
this.sex = sex;
this.age = age;
// this.adress = adress;
// this.email = email;
this.phone = phone;
this.password = password;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// public String getAdress() {
// return adress;
// }
//
// public void setAdress(String adress) {
// this.adress = adress;
// }
//
// public String getEmail() {
// return email;
// }
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"account='" + account + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
// ", adress='" + adress + '\'' +
// ", email='" + email + '\'' +
", phone='" + phone + '\'' +
", password='" + password + '\'' +
'}';
}
}
userDB
package com.jiang.jzj_qq.user;
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 android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class UserDB {
SQLiteDatabase writableDatabase;
private static String DB_NAME = "user.db";
private static int DB_VERSION = 1;
private static String SQL = "create table userinfo (" +
" id integer primary key autoincrement," +
" account text," +
" name text," +
" sex text," +
" age integer," +
" address text," +
" email text," +
" phone text," +
" pwd text" +
")";
public User findUserByAccount(String account) { //查找
Cursor userinfo = writableDatabase.query("userinfo",null,"account = ?",new String[]{account},null,null,null);
User user = null;
while (userinfo.moveToNext()){
String acc = userinfo.getString(userinfo.getColumnIndex("account"));
String name = userinfo.getString(userinfo.getColumnIndex("name"));
String sex = userinfo.getString(userinfo.getColumnIndex("sex"));
int age = Integer.parseInt(userinfo.getString(userinfo.getColumnIndex("age")));
String address = userinfo.getString(userinfo.getColumnIndex("address"));
String email = userinfo.getString(userinfo.getColumnIndex("email"));
String phone = userinfo.getString(userinfo.getColumnIndex("phone"));
String pwd = userinfo.getString(userinfo.getColumnIndex("pwd"));
user = new User(acc, name, sex, age, phone, pwd);
}
return user;
}
//修改的方法 update userinfo set name = ? ,phone = ? where account = ?
public long UserUpdate(String account,String name,String phone){
ContentValues contentValues = new ContentValues();
contentValues.put("name",name);
contentValues.put("phone",phone);
return writableDatabase.update("userinfo",contentValues,"account = ?",new String[]{account}) ;
}
public boolean deleteByAccount(String account) {
return writableDatabase.delete("userinfo","account=?",new String[]{account})>0;
}
public List<User> FindAlluser() {
List<User> users = new ArrayList<>();
Cursor userinfo = writableDatabase.query("userinfo", null, null, null, null, null, null, null);
while (userinfo.moveToNext()) {
String account = userinfo.getString(userinfo.getColumnIndex("account"));
String name = userinfo.getString(userinfo.getColumnIndex("name"));
String sex = userinfo.getString(userinfo.getColumnIndex("sex"));
int age = Integer.parseInt(userinfo.getString(userinfo.getColumnIndex("age")));
String address = userinfo.getString(userinfo.getColumnIndex("address"));
String email = userinfo.getString(userinfo.getColumnIndex("email"));
String phone = userinfo.getString(userinfo.getColumnIndex("phone"));
String pwd = userinfo.getString(userinfo.getColumnIndex("pwd"));
Log.d("mas", "取值");
User user = new User(account, name, sex, age, pwd, phone);
users.add(user);
}
return users;
}
public static class UserDbOpenHelper extends SQLiteOpenHelper {
private Context context;
public UserDbOpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
/*
创建数据库
*/
@Override
public void onCreate(SQLiteDatabase db) {
// 执行sql语句
db.execSQL(SQL);
Toast.makeText(context, "创建sqlite成功", Toast.LENGTH_SHORT).show();
}
/*
更新数据库(当版本号发生变化的时候)
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists userinfo");
db.execSQL(SQL);
}
}
/*
打开数据库
*/
public void OpenUserDb(Context mcontext) {
UserDbOpenHelper userDbOpenHelper = new UserDbOpenHelper(mcontext);
writableDatabase = userDbOpenHelper.getWritableDatabase();
}
/*
新增数据insert
用于用户注册(--新增语句
insert into student(name,sex,age)values
('小名','男','hello')
--字段名和值要一一对应
--删除语句
delete from student where name = '小名')
*/
public long insertUserData(User user) { //插入
String account = user.getAccount();
String name = user.getName();
String sex = user.getSex();
int age = user.getAge();
// String email = user.getEmail();
String phone = user.getPhone();
// String address = user.getAdress();
String pwd = user.getPassword();
ContentValues contentValues = new ContentValues();
contentValues.put("account", account);
contentValues.put("name", name);
contentValues.put("sex", sex);
contentValues.put("age", age);
// contentValues.put("email", email);
// contentValues.put("address", address);
contentValues.put("phone", phone);
contentValues.put("pwd", pwd);
return writableDatabase.insert("userinfo", null, contentValues);
}
public List<User> FindAllUsers() {
List<User> users = new ArrayList<>();
Cursor userinfo = writableDatabase.query("userinfo", null, null, null, null, null, null, null);
while (userinfo.moveToNext()) {
String account = userinfo.getString(userinfo.getColumnIndex("account"));
String name = userinfo.getString(userinfo.getColumnIndex("name"));
String sex = userinfo.getString(userinfo.getColumnIndex("sex"));
int age = Integer.parseInt(userinfo.getString(userinfo.getColumnIndex("age")));
String address = userinfo.getString(userinfo.getColumnIndex("address"));
String email = userinfo.getString(userinfo.getColumnIndex("email"));
String phone = userinfo.getString(userinfo.getColumnIndex("phone"));
String pwd = userinfo.getString(userinfo.getColumnIndex("pwd"));
Log.d("mas", "取值");
User user = new User(account, name, sex, age, pwd, phone);
users.add(user);
}
return users;
}
public boolean delete(String account) {
return writableDatabase.delete("userinfo", "account = ?", new String[]{account})>0;
}
}
文件夹sql类
package com.jiang.jzj_qq.sql;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.jiang.jzj_qq.R;
import com.jiang.jzj_qq.user.User;
import com.jiang.jzj_qq.user.UserDB;
import java.util.List;
//删除
public class AllUsers extends AppCompatActivity {
private ListView userlistview;
private List<User> users;
private UserDB userDB;
private AdapterView listViewLV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_users);
userDB = new UserDB();
userDB.OpenUserDb(this);
//1.获取listview
userlistview = findViewById(R.id.user_listview);
//2.添加数据源,数据库全查询(UserDB类里完成)
users = userDB.FindAllUsers();
//3.构建Adapter
UserAdapter userAdapter = new UserAdapter(users, this);
//4.显示list
userlistview.setAdapter(userAdapter);
listViewLV = findViewById(R.id.user_listview);
listViewLV.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
User user = users.get(position);
if (userDB.delete(user.getAccount())) {
Toast.makeText(AllUsers.this, "删除成功", Toast.LENGTH_LONG);
startActivity(new Intent(AllUsers.this,AllUsers.class));
} else {
Toast.makeText(AllUsers.this, "删除失败", Toast.LENGTH_LONG);
}
return false;
}
});
//添加点击事件(跳转到修改页面)
userlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(AllUsers.this,UserUapedate.class);
//携带原始数据
User user = users.get(position);
String account = user.getAccount();
intent.putExtra("account",account);
startActivity(intent);
}
});
}
}
package com.jiang.jzj_qq.sql;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class DataHelper {
/**
* sharepreference方式存储
*
* @param context XML的文件名,通常取模块名
* @param key 存储在XML中的key
* @param value 值
*/
public static void putData2SharedPreferences(Context context, String moduleName, String key, Object value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(moduleName, Context.MODE_PRIVATE); // 私有数据
Editor editor = sharedPreferences.edit();
if (value instanceof Boolean) {
editor.putBoolean(key, (Boolean) value);
} else if (value instanceof Integer) {
editor.putInt(key, (Integer) value);
} else if (value instanceof String) {
editor.putString(key, (String) value);
} else if (value instanceof Long) {
editor.putLong(key, (Long) value);
} else if (value instanceof Float) {
editor.putFloat(key, (Float) value);
}
editor.apply();
}
public static String getStringFromSharedPreferences(Context context, String name , String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE); // 私有数据
return sharedPreferences.getString(key, "");
}
public static Boolean getBooleanFromSharedPreferences(Context context, String name , String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE); // 私有数据
return sharedPreferences.getBoolean(key, false);
}
public static int getIntFromSharedPreferences(Context context, String name , String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE); // 私有数据
return sharedPreferences.getInt(key, -1);
}
}