parent
d0b75280dd
commit
cf41a715d9
@ -0,0 +1,47 @@
|
|||||||
|
package com.example.Cat.activity.activity;
|
||||||
|
|
||||||
|
public class UserInfo {
|
||||||
|
int id;
|
||||||
|
String username;
|
||||||
|
String paswd, sex;
|
||||||
|
String city;
|
||||||
|
public UserInfo(int id,String username, String paswd, String sex, String city){
|
||||||
|
this.id=id;
|
||||||
|
this.username = username;
|
||||||
|
this.paswd = paswd;
|
||||||
|
this.sex = sex;
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
public void setPaswd(String paswd) {
|
||||||
|
this.paswd = paswd;
|
||||||
|
}
|
||||||
|
public void setSex(String sex) {
|
||||||
|
this.sex = sex;
|
||||||
|
}
|
||||||
|
public void setCity(String city) {
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
public String getPaswd() {
|
||||||
|
return paswd;
|
||||||
|
}
|
||||||
|
public String getSex() {
|
||||||
|
return sex;
|
||||||
|
}
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,114 @@
|
|||||||
|
package com.example.Cat.activity.activity;
|
||||||
|
|
||||||
|
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 java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class User_Database extends SQLiteOpenHelper {
|
||||||
|
Context context;
|
||||||
|
public User_Database(Context context) {
|
||||||
|
super(context, "user_db", null, 1);
|
||||||
|
this.context=context;
|
||||||
|
System.out.println("in User_Database ,context="+ context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(SQLiteDatabase sqLiteDatabase) {
|
||||||
|
System.out.println("onCreate");
|
||||||
|
String sql = "create table user(" +
|
||||||
|
"id integer primary key autoincrement," +
|
||||||
|
"username varchar(20),paswd varchar(20)," +
|
||||||
|
"sex varchar(20)," +
|
||||||
|
"city longtext)";
|
||||||
|
sqLiteDatabase.execSQL(sql);
|
||||||
|
Log.i("SQLite","execSQL(sql)");
|
||||||
|
}
|
||||||
|
|
||||||
|
//数据库版本号更新时调用
|
||||||
|
@Override
|
||||||
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
|
}
|
||||||
|
|
||||||
|
//添加数据
|
||||||
|
|
||||||
|
public void adddata(SQLiteDatabase sqLiteDatabase, String username, String paswd, String sex, String city) {
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put("username", username);
|
||||||
|
values.put("paswd", paswd);
|
||||||
|
values.put("sex", sex);
|
||||||
|
values.put("city", city);
|
||||||
|
sqLiteDatabase.insert("user", null, values);
|
||||||
|
//sqLiteDatabase.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
//删除数据
|
||||||
|
public void delete(SQLiteDatabase sqLiteDatabase, int id) {
|
||||||
|
/*第一个参数:表名;第二个参数:需要删除的属性名,?代表占位符;第三个参数:属性名的属性值*/
|
||||||
|
sqLiteDatabase.delete("user", "id=?", new String[]{id + ""});
|
||||||
|
sqLiteDatabase.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
//更新数据
|
||||||
|
public void update(SQLiteDatabase sqLiteDatabase, int id, String username,
|
||||||
|
String paswd, String sex, String city) {
|
||||||
|
//创建一个ContentValues对象
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
//以键值对的形式插入
|
||||||
|
values.put("username", username);
|
||||||
|
values.put("paswd", paswd);
|
||||||
|
values.put("sex", sex);
|
||||||
|
values.put("city", city);
|
||||||
|
//执行修改的方法
|
||||||
|
sqLiteDatabase.update("user", values, "id=?", new String[]{id + ""});
|
||||||
|
sqLiteDatabase.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
//查询全部数据
|
||||||
|
public List<UserInfo> querydata(SQLiteDatabase sqLiteDatabase) {
|
||||||
|
Cursor cursor = sqLiteDatabase.query("user", null, null, null, null, null, "id ASC");
|
||||||
|
List<UserInfo> list = new ArrayList<UserInfo>();
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
int id = cursor.getInt(cursor.getColumnIndex("id"));
|
||||||
|
Log.i("SQLite","id="+id);
|
||||||
|
String username = cursor.getString(1);
|
||||||
|
Log.i("SQLite","username="+username);
|
||||||
|
String paswd = cursor.getString(2);
|
||||||
|
Log.i("SQLite","paswd="+paswd);
|
||||||
|
String sex = cursor.getString(3);
|
||||||
|
Log.i("SQLite","sex="+sex);
|
||||||
|
String city = cursor.getString(4);
|
||||||
|
Log.i("SQLite","city="+city);
|
||||||
|
list.add(new UserInfo(id, username, paswd, sex, city));
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
sqLiteDatabase.close();
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
public List<UserInfo> queryByname(SQLiteDatabase sqLiteDatabase,String name) {
|
||||||
|
Cursor cursor = sqLiteDatabase.query("user", null, "name=?", new String[]{name}, null, null, "id ASC");
|
||||||
|
List<UserInfo> list = new ArrayList<UserInfo>();
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
int id = cursor.getInt(cursor.getColumnIndex("id"));
|
||||||
|
Log.i("SQLite","id="+id);
|
||||||
|
String username = cursor.getString(1);
|
||||||
|
Log.i("SQLite","username="+username);
|
||||||
|
String paswd = cursor.getString(2);
|
||||||
|
Log.i("SQLite","paswd="+paswd);
|
||||||
|
String sex = cursor.getString(3);
|
||||||
|
Log.i("SQLite","sex="+sex);
|
||||||
|
String city = cursor.getString(4);
|
||||||
|
list.add(new UserInfo(id, username, paswd, sex, city));
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
sqLiteDatabase.close();
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue