SQLiteDatabase 的使用

功能]

SQLiteDatabase 是关于数据库操作的 可用于 insert delete update query 等操作 可惜美中不足的是:

1. 其不支持创建数据库

2. 其不支持版本更新 或者说其不知道如何做 因为具体数据的差异



鉴于以上的缺陷 有一个辅助类可以完成上面功能 那就是:SQLiteOpenHelper





[代码]

1. 定义SQLiteOpenHelper 并完成 创建 更新 功能

Java代码
public class DBHelper extends SQLiteOpenHelper {  
      
    public static final String TB_NAME = "mycountry";  
    public static final String ID = "_id";  
    public static final String COUNTRY = "country";  
    public static final String CODE = "code";  
      
      
    public DBHelper(Context context, String name,   
            CursorFactory factory,int version) {  
        super(context, name, factory, version);  
    }  
 
    public void onCreate(SQLiteDatabase db) {  
          
        db.execSQL("CREATE TABLE IF NOT EXISTS "   
                + TB_NAME + " ("   
                + ID + " INTEGER PRIMARY KEY,"   
                + COUNTRY + " VARCHAR," 
                + CODE + " INTEGER)");  
    }  
 
    public void onUpgrade(SQLiteDatabase db,   
            int oldVersion, int newVersion) {  
          
        db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);  
        onCreate(db);  
    }  


public class DBHelper extends SQLiteOpenHelper {
   
    public static final String TB_NAME = "mycountry";
    public static final String ID = "_id";
    public static final String COUNTRY = "country";
    public static final String CODE = "code";
   
   
    public DBHelper(Context context, String name,
            CursorFactory factory,int version) {
        super(context, name, factory, version);
    }

    public void onCreate(SQLiteDatabase db) {
       
    db.execSQL("CREATE TABLE IF NOT EXISTS "
                + TB_NAME + " ("
        + ID + " INTEGER PRIMARY KEY,"
            + COUNTRY + " VARCHAR,"
            + CODE + " INTEGER)");
    }

    public void onUpgrade(SQLiteDatabase db,
            int oldVersion, int newVersion) {
       
        db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);
        onCreate(db);
    }
}







2. 从SQLiteOpenHelper 得到SQLiteDatabase的实例

Java代码
DBHelper helper = new DBHelper(this, DB_NAME, null, VERSION);  
 
SQLiteDatabase db = helper.getWritableDatabase(); 

DBHelper helper = new DBHelper(this, DB_NAME, null, VERSION);

SQLiteDatabase db = helper.getWritableDatabase();





3. SQLiteDatabase 的一些操作:

* 插入数据:

Java代码
ContentValues values = new ContentValues();  
        values.put(DBHelper.COUNTRY, "中国");  
        values.put(DBHelper.CODE, 86);  
        db.insert(DBHelper.TB_NAME,DBHelper.ID, values); 

ContentValues values = new ContentValues();
        values.put(DBHelper.COUNTRY, "中国");
        values.put(DBHelper.CODE, 86);
        db.insert(DBHelper.TB_NAME,DBHelper.ID, values);



* 改动数据

Java代码
db.insert(DBHelper.TB_NAME,DBHelper.ID,null);  
        values.clear();  
        values.put(DBHelper.COUNTRY, "意大利");  
        values.put(DBHelper.CODE, 39);  
        db.update(DBHelper.TB_NAME, values,DBHelper.ID + " = 2",null); 

db.insert(DBHelper.TB_NAME,DBHelper.ID,null);
        values.clear();
        values.put(DBHelper.COUNTRY, "意大利");
        values.put(DBHelper.CODE, 39);
        db.update(DBHelper.TB_NAME, values,DBHelper.ID + " = 2",null);

* execSQL 执行SQL语言

Java代码
db.execSQL("INSERT INTO " 
                + DBHelper.TB_NAME + "(" 
                + DBHelper.COUNTRY + "," 
                + DBHelper.CODE + ") VALUES " 
                + "('洪都拉斯',504)"); 

db.execSQL("INSERT INTO "
                + DBHelper.TB_NAME + "("
                + DBHelper.COUNTRY + ","
                + DBHelper.CODE + ") VALUES "
                + "('洪都拉斯',504)");

* 查询数据

Java代码
Cursor c = db.query(DBHelper.TB_NAME,null,null,null,null,null,  
                DBHelper.CODE+" DESC"); 

Cursor c = db.query(DBHelper.TB_NAME,null,null,null,null,null,
        DBHelper.CODE+" DESC");

* 删除数据所有数据

Java代码
db.delete(DBHelper.TB_NAME,null,null); 

你可能感兴趣的:(sql,C++,c,C#)