SQLiteOpenHelper的使用及表更新关联操作

来源:互联网 发布:qq音乐网站源码 编辑:程序博客网 时间:2024/06/02 05:07

1 insert into 表1(字段1,字段2...) select *from 表2(字段1,,字段2...)where 条件   将一个表的数据插入另一个表前提字段为相同类型

2 sqlite不支持删除colum

create table A as select *from B where 1=2  where只复制表结构不复制表内容

那我们需要什么结构就怎么写如下(假设B的字段为abcd而A需要adc)

create table A as  select a,b,c from B where 1=2

走到这里我们需要偷梁换柱了

删除老表也就是B将A改为B

drop table B

alter table A rename to B

到此我们就完成如表需要删除字段

1和2配套使用才能保证数据

3 增加字段 支持

alter table A add column name varchar 


4 如何实现2个表关联呢

SQLiteDatabase db1 = tracks.getWritableDatabase(); 
    ContentValues tracksvalues = new ContentValues(); 
    tracksvalues.put(COL1, '1'); 
    tracksvalues.put(COL2, '2'); 
    Long insertid=db1.insertOrThrow(TABLE_NAME1, null, tracksvalues); 
    if (insertid!=-1) { 

        SQLiteDatabase db2 = waypoints.getWritableDatabase(); 
        ContentValues waypointvalues = new ContentValues(); 
        waypointvalues.put(LONGITUDE, loc.getLongitude()); 
        waypointvalues.put(LATITUDE, loc.getLatitude()); 
        waypointvalues.put(TIME, System.currentTimeMillis()); 
        waypointvalues.put(TRACK_ID_FK, insertid); 
        db2.insertOrThrow(TABLE_NAME2, null, waypointvalues); 
 
    } 
将TABLE_NAME1中插入tracksvalues,并返回一个vid  Long insertid
然后将这个id放入另一个表中存储

正题

下面进入正题使用SQLiteHelper

我先说大概思路,首先我们需要继承SQH重写它的方法构造 ,还需要一个常量配置类,还需要一个管理类就是初始化这个单例模式

直接上代码

package com.sun.myapplication;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;/** * Created by sunxin on 2017/9/8. */public class MySQliteHelper extends SQLiteOpenHelper{    /**     * 此构造方法的四个参数     * @param context 上下文环境     * @param name 数据库名称     * @param factory 工厂类可传null     * @param version 当前数据库版本 版本必须大于等于一     *  大家看到了这里参数很麻烦每次要搞很麻烦,我们可以去定义一个常量类ConfigHelp     */    public MySQliteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {        super(context, name, factory, version);    }    /**     * 上面的构造方法是为了展示参数的而这个构造是为了方便创建时不用那么麻烦的传递参数     * @param context     */    public MySQliteHelper(Context context) {        super(context,ConfigHelp.DB_NAME,null,ConfigHelp.DB_VERSION);    }    /**     * 数据库对象     * @param sqLiteDatabase     */    @Override    public void onCreate(SQLiteDatabase sqLiteDatabase) {        String sql="create table teacher (_id Integer Prima Key)";//我们通过sq语句创建表    }    /**     *     * @param sqLiteDatabase  数据库对象     * @param i 数据库旧版本     * @param i1 数据库新版本     */    @Override    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {    }    /**     *打开数据库是调用     * @param db 数据库对象     */    @Override    public void onOpen(SQLiteDatabase db) {        super.onOpen(db);    }}

package com.sun.myapplication;import android.content.Context;/** * Created by sunxin on 2017/9/8.\ * 数据库管理类 */public class DBManager {public  static MySQliteHelper helper;    public  static MySQliteHelper getInstance(Context context){        if (helper==null){            helper=new MySQliteHelper(context);        }        return helper;    }}
package com.sun.myapplication;/** * Created by sunxin on 2017/9/8. * 数据库配置 */public class ConfigHelp {    public static final String DB_NAME="my.db";//数据库名称    public static final int DB_VERSION=1;//数据库版本 默认必须大于等于1    public static final String DB_TABLENAME="";//表名}
我这里使用的截图工具是PICpick

我们的onupgrade()方法呢只有newversion大于oldversion的时候才会调用。

这里会用到下面的方法

/*** 检查表中某列是否存在* @param db* @param tableName 表名* @param columnName 列名* @return*/private boolean checkColumnExists2(SQLiteDatabase db, String tableName       , String columnName) {    boolean result = false ;    Cursor cursor = null ;    try{        cursor = db.rawQuery( "select * from sqlite_master where name = ? and sql like ?"           , new String[]{tableName , "%" + columnName + "%"} );        result = null != cursor && cursor.moveToFirst() ;    }catch (Exception e){        Log.e(TAG,"checkColumnExists2..." + e.getMessage()) ;    }finally{        if(null != cursor && !cursor.isClosed()){            cursor.close() ;        }    }    return result ;}
如果你再更新增加字段的时候不去检查字段是否存在的话,会报错,当然这里需要借鉴别人的博客了我就不写了。很多。这些就是SQlite的大概认识吧。我还是习惯框架去做数据库。推荐一个https://github.com/greenrobot/greenDAO