数据库案例-2

public MyDBOpenHelper(Contextcontext) {
   super(context, "hello.db",null,1);    }
   //数据库第一次创建时调用,在这里面可以在数据库名为hello.db下创建一张表。
@Override
public void onCreate(SQLiteDatabasedb) {
   System.out.println("数据库创建");     
   db.execSQL("create table person (id INTEGER primary keyautoincrement,name varchar(20),balance varchar(20))");   
}

//onUpGrade是通过检查数据库版本是否改变,如果改变了则执行该方法,否则不执行
@Override
public voidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub      
}
}
Contextcontext;
MyDBOpenHelperdb;
publicClassicPersonDAO(Context context) {
   this.context = context;
   db = newMyDBOpenHelper(context); // 创建dbopenHelper
} 
// 插入
public void insert(Person p) {
// 获得一个可写的数据库
SQLiteDatabasedataBase = db.getWritableDatabase();
// 向数据库中插入一条数据
dataBase.execSQL("insert into person (name,balance)values(?,?)",
new Object[] { p.getName(),p.getBalance() });
dataBase.close();// 将数据库关闭
}
// 删除一条数据
public void delete(int id) {
   SQLiteDatabasedatabase = db.getWritableDatabase();
   database.execSQL("delete from person where id=?",
   newObject[] { id+ "" });
   database.close();
}
// 更新数据库中的某条记录
public void update(Person p) {
SQLiteDatabasedata = db.getWritableDatabase();
data.execSQL("update person set name=?,balance=? where id=?",
   new Object[] { p.getName(),p.getBalance(), p.getId() });
   data.close();
}
// 根据id查询数据
public Person query(int i) {
SQLiteDatabasedata = db.getReadableDatabase();
 Cursorc = data.rawQuery("select * from person whereid=?",
         new String[] { i + "" });
   Personp = newPerson();
   while (c.moveToNext()) {
       Integerid = c.getInt(c.getColumnIndex("id"));
       Stringname = c.getString(c.getColumnIndex("name"));
       Stringbalance = c.getString(c.getColumnIndex("balance"));
       p.setId(id);
       p.setName(name);
       p.setBalance(balance);
   }
   db.close();
   return p;
}
// 查询全部
public List<Person>queryAll() {
   SQLiteDatabasedata = db.getReadableDatabase();
   Cursorc = data.rawQuery("select * from person", null);
   List<Person>list = newArrayList<Person>();
   while (c.moveToNext()) {
       Integerid = c.getInt(c.getColumnIndex("id"));
       Stringname = c.getString(c.getColumnIndex("name"));
       Stringbalance = c.getString(c.getColumnIndex("balance"));
       Personp = newPerson();
       p.setId(id);
       p.setName(name);
       p.setBalance(balance);
       list.add(p);
   }
   return list;
}
// 分页查询
public List<Person>queryPage() {
   SQLiteDatabasedata = db.getReadableDatabase();
   Cursorc = data.rawQuery("select * from person limit20,30", null);
   List<Person>list = newArrayList<Person>();
   while (c.moveToNext()) {
       Integerid = c.getInt(c.getColumnIndex("id"));
       Stringname = c.getString(c.getColumnIndex("name"));
       Stringbalance = c.getString(c.getColumnIndex("balance"));
       Personp = newPerson();
       p.setId(id);
       p.setName(name);
       p.setBalance(balance);
       list.add(p);
   }
   return list;
}
}

ContentValues
在添加、修改:代表意义包装一行数据。
本质: Map集合;key=>代表的数据库表中字段, value=>字段设定值

whereClause
用占位符填充:条件表达式name=?
用值直接填充: :条件表达式name=‘AAA’;whereArgs可以不用传值

//1. 创建SQLi teOpenlleIper对象
helper=new DBHelper ( context: MainActivity. this) ;
//2. HeIper创建数据库操作对象;Statement
db=helper. getReadabl eDatabase();
//3.操作数据库

//查洵
public void queryAll(View view) {
StringBuffer buffer=new Str ingBuffer() ;
//查洵
Cursor cr = db. query( table: " user", columns: null, selection: null, selectionArgs: null, groupBy; null, having: null, orderBy: null) ;
//结果遍历
//判断是否有数据
if (cr!=null) {
while(cr. moveToNext() {
//取值
int id=cr. getInt ( columnIndex: 0) ;
String name=cr. getString( columnIndex: 1);
int age=cr. getInt ( columnIndex: 2) ;
buffer. append(id) ;
buffer. append(", “);
buffer. append (name) ;
buffer. append(”,");
buffer. append(age) ;
buffer. append(" \n");
}
}
//关闭游标
cr. close() ;
db. close() ;

//添加
public void add(View view) {
//insert into table 0) values 0);
ContentValues values=new ContentValues() ;
values. put(" name", “CCC”);
db. insert( table:”user", nullColumnHack: null, values);
}

//删除
public void delete(View view) {
db=he lper. getWri tableDatabase() ;
int row= db. delete( table: “user”, whereClause:”age = ?” , new String[]{“20”});
System. out. println(" row-==”+row);
db. close();

//修改
public void update(View view) {
db=helper. getWritableDatabase();
ContentValues values=new ContentValues();
values. put(" name", “DD”);
db. update( table:" user’ , values, whereClause: age= ?" , new String[]{"28’ });
db. close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值