标签:sqlite
1.SQLite数据库的特点情况4: 数据库文件存在, 版本号降低, 执行onDowngrade()方法, 方法中默认会抛出一个异常
代码:MySQLiteOpenHelper.java
package com.oterman.mysqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MySQLiteOpenHelper extends SQLiteOpenHelper { /** * 由于父类没有默认的无参数的构造函数,故需要显示的写出构造函数,然后去调用父类有参数的构造函数; * 参数1:context表示应用程序的环境,用来确定数据库文件的位置; * 参数2:数据库文件的名字; * 参数3:用来创建结果集Cursor的工厂,默认传入null; * 参数4:数据的版本号,从1开始; * @param context * @param version */ public MySQLiteOpenHelper(Context context,int version) { super(context,"myfirstdb.db",null,version); } public MySQLiteOpenHelper(Context context) { super(context,"myfirstdb.db",null,1); } /** * 如果数据库文件不存在,调用该方法; */ @Override public void onCreate(SQLiteDatabase db) { System.out.println("数据库创建啦"); db.execSQL("create table account(_id Integer primary key autoincrement,name varchar(40))"); } /** * 数据库文件存在,版本号发生变化,会调用该方法; */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("数据库升级啦"); } }
事务结束的时候, 会把最后一个成功标记之前的操作提交, 成功标记之后的操作回滚
代码:Accout.java
package domain; public class Account { private Integer id ; private String name; private Integer balance ; public Account(Integer id, String name, Integer balance) { super(); this.id = id; this.name = name; this.balance = balance; } public Account() { super(); } public Integer getId() { return id; } public Account(String name, Integer balance) { super(); this.name = name; this.balance = balance; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getBalance() { return balance; } public void setBalance(Integer balance) { this.balance = balance; } @Override public String toString() { return "Account [id=" + id + ", name=" + name + ", balance=" + balance + "]"; } }
package com.oterman.dao; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.oterman.mysqlite.MySQLiteOpenHelper; import domain.Account; public class AccountDao { private Context context; public AccountDao(Context context) { this.context = context; } public void insert(Account a){ //获取数据库; MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context); SQLiteDatabase db=helper.getWritableDatabase(); //操作数据库; db.execSQL("insert into account values(null,?,?)",new Object[]{a.getName(),a.getBalance()}); //关闭数据库; db.close(); } //删除记录; public void delete(int i) { MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context); SQLiteDatabase db=helper.getWritableDatabase();//获取数据库; db.execSQL("delete from account where _id=?",new Object[]{i}); db.close(); } //修改数据库; public void update(){ MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context); SQLiteDatabase db=helper.getWritableDatabase(); db.execSQL("update account set balance=? where _id<?",new Object[]{1000,9}); db.close(); } //查询数据库; public Account query(int i) { //获取数据库; MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context); SQLiteDatabase db=helper.getWritableDatabase(); //执行查询语句;获取结果集; Cursor c=db.rawQuery("select name,balance from account where _id=?", new String[]{i+""}); Account a=null; while(c.moveToNext()){ String name=c.getString(0); int balance=c.getInt(1); a=new Account(i,name,balance); } return a; } //查询所有; public List<Account> queryAll(){ MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context); SQLiteDatabase db=helper.getWritableDatabase(); List<Account> list=new ArrayList<Account>(); String sql="select * from account"; Cursor c=db.rawQuery(sql, null); while(c.moveToNext()){ int id=c.getInt(0); String name=c.getString(c.getColumnIndex("name")); int balance=c.getInt(c.getColumnIndex("balance")); list.add(new Account(id,name,balance)); } c.close(); db.close(); return list; } //演示事务 public void trans(int fromId,int toId, int amount) { MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context); SQLiteDatabase db=helper.getWritableDatabase(); String sql1="update account set balance=balance-? where _id=?"; String sql2="update account set balance=balance+? where _id=?"; try{ db.beginTransaction();//开启事务; db.execSQL(sql1, new Object[]{amount,fromId}); db.execSQL(sql2, new Object[]{amount,toId}); db.setTransactionSuccessful();//可以设置多个标记点;分组提交;如果在标记点之前未出现异常,则之前的所有的sql操作提交; db.execSQL(sql1, new Object[]{amount,fromId}); db.execSQL(sql2, new Object[]{amount,toId}); db.setTransactionSuccessful(); db.execSQL(sql1, new Object[]{amount,fromId}); int i=1/0; db.execSQL(sql2, new Object[]{amount,toId}); db.setTransactionSuccessful();//标记点;出现异常时,该标记点至上一个标记点的所有内容被回滚; }finally{ db.endTransaction(); db.close(); } } }
标签:sqlite
原文地址:http://blog.csdn.net/damogu_arthur/article/details/41806475