編輯:關於Android編程
情況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 queryAll(){
MySQLiteOpenHelper helper=new MySQLiteOpenHelper(context);
SQLiteDatabase db=helper.getWritableDatabase();
List list=new ArrayList();
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();
}
}
}
Android Fragment概述及用法
Fragment一般是宿主Activity UI的一部分或一種行為,作為Activity的整個V
Android仿淘寶商品拖動查看詳情及標題欄漸變功能
緒論最近一直比較忙,也沒抽出時間來寫博客,也不得不說是自己犯了懶癌,人要是一懶就什麼事都不想做了,如果不能堅持下來的話,那麼估計就廢了,��。
Android:ViewPager詳解(異步網絡加載圖片,帶圖片緩存,並帶導航小圓點)
android 應用中,如歡迎指引頁面, 和圖片輪播功能, 或者更多的內容在一頁顯示不了,要分成多個頁面,這時候viewpager是很好用的。 首先看下效果:
GridView
public class DragGrid extends GridView { /** 點擊時候的X位置 */ public int downX; /