日期:2014-05-16 浏览次数:20581 次
package com.itcode.mysqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; /** * 继承SQLiteOpenHelper,创建数据库 * @author Administrator * */ public class MySQLiteOpenHelper extends SQLiteOpenHelper{ //一:声明构造函数 /** * @param context 当前应用的环境,用来确定数据库目录 * @param name 数据库文件的名字 * @param factory 游标工厂,用来创建结果集对象 * @param version 数据库版本 从1开始 */ public MySQLiteOpenHelper(Context context) { super(context, "itcode.db", null, 1); } //二:重写onCreate方法 数据库被创建时调用 public void onCreate(SQLiteDatabase db) { System.out.println("MySQLiteOpenHelper-->onCreate 被调用!"); //1.创建数据库 db.execSQL("create table account(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),balance INTEGER)"); // db.execSQL("CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20))"); // 执行一条SQL语句 } //三:重写onUpgreade方法,数据库时调用 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // System.out.println("MySQLiteOpenHelper-->onUpgrade 被调用"); // db.execSQL("alter table account ADD balance integer"); // db.execSQL("ALTER TABLE account ADD balance INTEGER"); } }
package com.itcode.mysqlite; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; public class AccountDao { //一:创建AccountDao时依据当前context创建数据库 private MySQLiteOpenHelper mySQLiteOpenHelper; public AccountDao(Context context){ mySQLiteOpenHelper = new MySQLiteOpenHelper(context); } //二:增:传进来一个Account,将此对象加入数据库 public int add(Account a){ //1.获取相应的(可写的)数据库 SQLiteDatabase writableDatabase = mySQLiteOpenHelper.getWritableDatabase(); //2.执行语句 writableDatabase.execSQL("insert into account(name,balance) values(?,?)", new Object[]{a.getName(),a.getBalance()}); //3.获取插入的数据集: Cursor rawQuery = writableDatabase.rawQuery("select _id from account order by _id desc limit 1", null); rawQuery.moveToNext(); int id = rawQuery.getInt(rawQuery.getColumnIndex("_id")); //4.关闭数据库 return id; } //三:删:根据id删除数据库中相应的数据项 public void delete(int id){ //1.获取相应的(可写的)数据库 SQLiteDatabase writableDatabase = mySQLiteOpenHelper.getWritableDatabase(); //2.执行删除语句 writableDatabase.execSQL("Delete from account where _id = ?",new Object[]{id}); //3.关闭数据库 writableDatabase.close(); } //四:改:传进来一个Account,根据Account的对象的id修改name 和balance public void update(Account a){ //1.获取相应的(可写的)数据库 SQLiteDatabase writableDatabase = mySQLiteOpenHelper.getWritableDatabase(); //2.执行更新语句 writableDatabase.execSQL("update account set name=?,balance = ? where _id = ?",new Object[]{a.getName(),a.getBalance(),a.getId()}); //3.关闭数据库 writableDatabase.close(); } //五:查:根据id查询数据库 public Account query(int id){ //1.获取相应的(可读的)数据库 SQLiteDatabase readableDatabase = mySQLiteOpenHelper.getReadableDatabase(); //2.执行更新语句 Cursor rawQuery = readableDatabase.rawQuery("select * from account where _id=?", new String[]{String.valueOf(id)}); //3.将查询到的数据赋值给Account对象 Account a = null; while(rawQuery.moveToNext()){ Integer balance = Integer.valueOf(rawQuery.getString(rawQuery.getColumnIndex("balance"))); String name = rawQuery.getString(rawQuery.getColumnIndex("name")); a = new Account(id,name,balance); } //3.关闭结果集与数据库 rawQuery.close(); readableDatabase.close(); return a; } //六:查询所有,将查询到的所有数据放在list集合中返回 public List<Acco