博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android sqlite 一次创建多个表
阅读量:6230 次
发布时间:2019-06-21

本文共 10787 字,大约阅读时间需要 35 分钟。

package com.yangguangfu.database;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;/** * 默认就在数据库里创建4张表 *  * */public class DBOpenHelper extends SQLiteOpenHelper {    private static final String name = "database.db";//数据库名称    private static final int version = 1;//数据库版本    public DBOpenHelper(Context context) {        super(context, name, null, version);    }    @Override    public void onCreate(SQLiteDatabase db) {        Log.e("DBOpenHelper", "DBOpenHelperDBOpenHelperDBOpenHelperDBOpenHelper");        db.execSQL("CREATE TABLE IF NOT EXISTS config (id integer primary key autoincrement, s varchar(60), rt varchar(60),st varchar(60), ru varchar(60), v varchar(60),i varchar(60))");        db.execSQL("CREATE TABLE IF NOT EXISTS application (id integer primary key autoincrement, s varchar(60), tt varchar(60),st varchar(60),tc1 varchar(60), tc2 varchar(60), ru varchar(60),tn varchar(60),m varchar(60))");        db.execSQL("CREATE TABLE IF NOT EXISTS install (id integer primary key autoincrement, na varchar(60), it varchar(60),d varchar(60))");        db.execSQL("CREATE TABLE IF NOT EXISTS smslist (id integer primary key autoincrement, t varchar(60), st varchar(60),n1 varchar(60),n2 varchar(60),n varchar(60),m varchar(60),a varchar(60))");    }      @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        Log.e("DBOpenHelper", "onUpgradeonUpgradeonUpgradeonUpgrade");        db.execSQL("DROP TABLE IF EXISTS config");        db.execSQL("DROP TABLE IF EXISTS application");        db.execSQL("DROP TABLE IF EXISTS install");        db.execSQL("DROP TABLE IF EXISTS smslist");        onCreate(db);            }    }

数据库服务

package com.yangguangfu.database;import android.content.Context;import android.database.Cursor;import com.yangguangfu.bean.ApplicationInfo;import com.yangguangfu.bean.ConfigInfo;import com.yangguangfu.bean.InstallInfo;import com.yangguangfu.bean.SMSInfo;/** * 数据库方法封装,创建表,删除表,数据(增删该查)... *  * */public class DatabaseService {    private DBOpenHelper dbOpenHelper;    public DatabaseService(Context context) {        dbOpenHelper = new DBOpenHelper(context);    }    public void dropTable(String taleName) {        dbOpenHelper.getWritableDatabase().execSQL(                "DROP TABLE IF EXISTS " + taleName);    }            public void closeDatabase(String DatabaseName) {        dbOpenHelper.getWritableDatabase().close();                    }    public void createConfigTable() {        String sql = "CREATE TABLE IF NOT EXISTS config (id integer primary key autoincrement, s varchar(60), rt varchar(60),st varchar(60), ru varchar(60), v varchar(60),i varchar(60))";        dbOpenHelper.getWritableDatabase().execSQL(sql);    }    public void createTableApplication() {        String sql = "CREATE TABLE IF NOT EXISTS application (id integer primary key autoincrement, s varchar(60), tt varchar(60),st varchar(60),tc1 varchar(60), tc2 varchar(60), ru varchar(60),tn varchar(60),m varchar(60))";        dbOpenHelper.getWritableDatabase().execSQL(sql);    }    public void createTableInstall() {        String sql = "CREATE TABLE IF NOT EXISTS install (id integer primary key autoincrement, na varchar(60), it varchar(60),d varchar(60))";        dbOpenHelper.getWritableDatabase().execSQL(sql);    }    public void createTableSmslist() {        String sql = "CREATE TABLE IF NOT EXISTS smslist (id integer primary key autoincrement, t varchar(60), st varchar(60),n1 varchar(60),n2 varchar(60),n varchar(60),m varchar(60),a varchar(60))";        dbOpenHelper.getWritableDatabase().execSQL(sql);    }    public void saveConfigInfo(ConfigInfo configInfo) {        dbOpenHelper.getWritableDatabase().execSQL(                "insert into config (s, rt, st, ru, v,i) values(?,?,?,?,?,?)",                new Object[] { configInfo.getS(), configInfo.getRt(),                        configInfo.getSt(), configInfo.getRu(),                        configInfo.getV(), configInfo.getI() });    }    public void saveApplicationInfo(ApplicationInfo configInfo) {        dbOpenHelper                .getWritableDatabase()                .execSQL(                        "insert into application (s,tt,tc1,tc2,ru,tn,m) values(?,?,?,?,?,?,?)",                        new Object[] { configInfo.getS(), configInfo.getTt(),                                configInfo.getTc1(), configInfo.getTc2(),                                configInfo.getRu(), configInfo.getTn(),                                configInfo.getM() });    }    public void saveMsmInfo(SMSInfo configInfo) {        dbOpenHelper                .getWritableDatabase()                .execSQL(                        "insert into smslist (t,st,n1,n2,n,m,a) values(?,?,?,?,?,?,?)",                        new Object[] { configInfo.getT(), configInfo.getSt(),                                configInfo.getN1(), configInfo.getN2(),                                configInfo.getN(), configInfo.getM(),                                configInfo.getA() });    }    public void saveInstallInfo(InstallInfo configInfo) {        dbOpenHelper.getWritableDatabase().execSQL(                "insert into install (na,it,d) values(?,?,?)",                new Object[] { configInfo.getNa(), configInfo.getIt(),                        configInfo.getD() });    }    public void updateConfigInfo(ConfigInfo configInfo) {        dbOpenHelper.getWritableDatabase().execSQL(                "update config set s=?, rt=?, st=?, ru=?, v=?,i=? where id=?",                new Object[] { configInfo.getS(), configInfo.getRt(),                        configInfo.getSt(), configInfo.getRu(),                        configInfo.getV(), configInfo.getI(),                        configInfo.getId() });    }    public void updateApplicationInfo(ApplicationInfo configInfo) {        dbOpenHelper                .getWritableDatabase()                .execSQL(                        "update application set s=?, tt=?, st=?, tc1=?, tc2=?,ru=?,tn=?,m=? where id=?",                        new Object[] { configInfo.getS(), configInfo.getTt(),                                configInfo.getSt(), configInfo.getTc1(),                                configInfo.getTc2(), configInfo.getRu(),                                configInfo.getTn(),configInfo.getM(), configInfo.getId() });    }    public void updateInstallInfo(InstallInfo configInfo) {        dbOpenHelper.getWritableDatabase().execSQL(                "update install set na=?, it=?, d=? where id=?",                new Object[] { configInfo.getNa(), configInfo.getIt(),                        configInfo.getD(), configInfo.getId() });    }    public void updateSMSInfo(SMSInfo configInfo) {        dbOpenHelper                .getWritableDatabase()                .execSQL(                        "update smslist set t=?, st=?, n1=?, n2=?, n=?, m=?, a=? where id=?",                        new Object[] { configInfo.getT(), configInfo.getSt(),                                configInfo.getN1(), configInfo.getN2(),                                configInfo.getN(), configInfo.getM(),                                configInfo.getA(), configInfo.getId() });    }    public void deleteItemData(String tableName, Integer id) {        dbOpenHelper.getWritableDatabase()                .execSQL("delete from " + tableName + " where id=?",                        new Object[] { id });    }    public InstallInfo findInstallInfo(Integer id) {        Cursor cursor = dbOpenHelper.getWritableDatabase().rawQuery(                "select id,na,it,d from install where id=?",                new String[] { String.valueOf(id) });        if (cursor.moveToNext()) {            InstallInfo configInfo = new InstallInfo();            configInfo.setId((cursor.getInt(0)));            configInfo.setNa(cursor.getString(1));            configInfo.setIt(cursor.getString(2));            configInfo.setD(cursor.getString(3));            return configInfo;        }        return null;    }    public ConfigInfo findConfigInfo(Integer id) {        Cursor cursor = dbOpenHelper.getWritableDatabase().rawQuery(                "select id,s,rt,st,ru,v,i from config where id=?",                new String[] { String.valueOf(id) });        if (cursor.moveToNext()) {            ConfigInfo configInfo = new ConfigInfo();            configInfo.setId((cursor.getInt(0)));            configInfo.setS(cursor.getString(1));            configInfo.setRt(cursor.getString(2));            configInfo.setSt(cursor.getString(3));            configInfo.setRu(cursor.getString(4));            configInfo.setV(cursor.getString(5));            configInfo.setI(cursor.getString(6));            return configInfo;        }        return null;    }    public SMSInfo findSMSInfo(Integer id) {        Cursor cursor = dbOpenHelper.getWritableDatabase().rawQuery(                "select id,t,st,n1,n2,n,m,a from smslist where id=?",                new String[] { String.valueOf(id) });        if (cursor.moveToNext()) {            SMSInfo configInfo = new SMSInfo();            configInfo.setId((cursor.getInt(0)));            configInfo.setT(cursor.getString(1));            configInfo.setSt(cursor.getString(2));            configInfo.setN1(cursor.getString(3));            configInfo.setN2(cursor.getString(4));            configInfo.setN(cursor.getString(5));            configInfo.setM(cursor.getString(6));            configInfo.setA(cursor.getString(7));            return configInfo;        }        return null;    }    public ApplicationInfo findApplication(Integer id) {        Cursor cursor = dbOpenHelper                .getWritableDatabase()                .rawQuery(                        "select id,s,tt,st,tc1,tc2,ru,tn,m from application where id=?",                        new String[] { String.valueOf(id) });        if (cursor.moveToNext()) {            ApplicationInfo applicationinfo = new ApplicationInfo();            applicationinfo.setId((cursor.getInt(0)));            applicationinfo.setS(cursor.getString(1));            applicationinfo.setTt(cursor.getString(2));            applicationinfo.setSt(cursor.getString(3));            applicationinfo.setTc1(cursor.getString(4));            applicationinfo.setTc2(cursor.getString(5));            applicationinfo.setRu(cursor.getString(6));            applicationinfo.setTn(cursor.getString(7));            applicationinfo.setM(cursor.getString(8));            return applicationinfo;        }        return null;    }    public long getDataCount(String tableName) {        Cursor cursor = dbOpenHelper.getReadableDatabase().rawQuery(                "select count(*) from " + tableName, null);        cursor.moveToFirst();        return cursor.getLong(0);    }    public void close() {        dbOpenHelper.close();    }}

 

转载地址:http://witna.baihongyu.com/

你可能感兴趣的文章
Navicat 如何调整栏位结构
查看>>
食品安全溯源区块链解决方案探索
查看>>
关于Spring Data JPA的save()保存,MySQL字段默认值无效
查看>>
数据结构——二叉树(PHP)
查看>>
MySQL实时性能监控工具doDBA tools
查看>>
ListView 局部刷新实现思路
查看>>
JSON笔记之在PHP语言中使用JSON
查看>>
函数的指针
查看>>
Jquery AJAX使用踩坑小记
查看>>
ubuntu下安装Apache+PHP+Mysql
查看>>
Bootstrap 过渡效果(Transition)插件
查看>>
[Linux]-Linux 命令大全
查看>>
mysql将查询到的数据导出到Excel
查看>>
Android 切换系统语言源码分析
查看>>
API 调用次数限制实现
查看>>
我的网站搭建 (第十八天) 自定义用户模型
查看>>
排序应该在数据库还是在应用程序中进行?
查看>>
java过滤特殊字符的正则表达式,正则表达式学习
查看>>
VM VirtualBox安装CentOS 7 64位实践
查看>>
如何搭建一个独立博客——简明Github Pages与Hexo教程
查看>>