package com.gxa.db; public class User { private int id; private String name; private String pwd; private int age; private String sex; public User(int id, String name, String pwd, int age, String sex) { this.id = id; this.name = name; this.pwd = pwd; this.age = age; this.sex = sex; } public User(String name, String pwd, int age, String sex) { this.name = name; this.pwd = pwd; this.age = age; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", age=" + age + ", sex=" + sex + "]"; } }
package com.gxa.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MyDatabaseHelper extends SQLiteOpenHelper { //TEXT字符串 VARCHAR INT INTEGER numeric(10,2) real小数 blob二进制数据 /** * unique 唯一性约束 * not null 不为空约束 * check 检查性约束 * primary key 主键约束 */ public static final String TAB_USER = "CREATE TABLE IF NOT EXISTS T_USER(" +"id integer primary key autoincrement," +"name varchar(20) UNIQUE," +"pwd text default(‘123‘)," +"age integer NOT NULL," +"sex text check(sex in(‘f‘,‘m‘))" +")"; public MyDatabaseHelper(Context context) { super(context, "user.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(TAB_USER); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
package com.gxa.db; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class UserDao { public MyDatabaseHelper helper; public UserDao(Context context){ helper = new MyDatabaseHelper(context); } public void addUser(User user){ //id NULL SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("insert into t_user (name,pwd,age,sex) VALUES(?,?,?,?)", new Object[]{user.getName(),user.getPwd(),user.getAge(),user.getSex()}); db.close(); } public void update(int id,String name){ SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("update t_user set name=? where id= ?", new Object[]{name,id}); db.close(); } public void delete(int id){ SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("delete from t_user where id=?",new Object[]{id}); db.close(); } public List<User> queryAll(){ List<User> users = new ArrayList<User>(); SQLiteDatabase db = helper.getReadableDatabase(); User user = null; Cursor cursor = db.query("t_user", null, null, null, null, null, null); if (cursor!=null) { while(cursor.moveToNext()){ int id = cursor.getInt(cursor.getColumnIndex("id")); int age = cursor.getInt(cursor.getColumnIndex("age")); String name = cursor.getString(cursor.getColumnIndex("name")); String pwd = cursor.getString(cursor.getColumnIndex("pwd")); String sex = cursor.getString(cursor.getColumnIndex("sex")); user = new User(id, name, pwd, age, sex); users.add(user); } cursor.close(); } db.close(); return users; } }
package com.gxa.db; public class Constant { public static final String TAB_NAME = "t_user"; }
package com.gxa.db; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
package com.gxa.db; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
时间: 2024-10-07 20:18:46