andorid WCDB数据库创建(1)
腾讯推出的WCDB开源数据库架构对我们开发者来说更加简单了。它的基本功能和优点小伙伴们可以去官网上去看一下,这里我就不多说了,下面我就直接入主题了。
1,添加WCDB的依赖
dependencies { compile 'com.tencent.wcdb:wcdb-android:1.0.0'} 也可以直接导入jar和so到我们的项目中来,这个等下在我的项目
地址上面会有。
2,我们新建一个数据库管理类DataBaseManger,首先初始化数据库,
public class DatabaseManager {@SuppressLint("StaticFieldLeak")private static DatabaseManager instance = new DatabaseManager(); private volatile SQLiteDatabase database; private static final int CURRENT_VERSION = 1; //数据库版本号 private volatile boolean inited = false; @SuppressLint("StaticFieldLeak")private static Context mContext; private DatabaseManager(){}public static void init(Context context){mContext = context.getApplicationContext(); }public static DatabaseManager getInstance(){return instance; }/** * 初始化数据库 */ public synchronized void initDatabase(int id){if (inited) //这里默认为false 保证数据库只初始化一次{return; }String dbName = String.valueOf(id); createDirsInCache(dbName); dbName += ".db"; if (BuildConfig.DEBUG){File file = null; if (!exist(id, dbName)){file = createUserDbForDebug(id, dbName);//创建db }else {file = getUserDbForDebug(id, dbName); }if (file == null){throw new RuntimeException("db file is not found"); }database = SQLiteDatabase.openOrCreateDatabase(file, null); openForeignKeySupport(); }else {database = SQLiteDatabase.openOrCreateDatabase(mContext.getDatabasePath(dbName), null); openForeignKeySupport(); }if (BuildConfig.DEBUG){database.setTraceCallback(new SQLiteTrace(){@Override public void onSQLExecuted(SQLiteDatabase sqLiteDatabase, String s, int i, long l){}@Override public void onConnectionPoolBusy(SQLiteDatabase db, String sql, List<String> requests, String message){for (String req : requests){}db.dump(new Printer(){@Override public void println(String x){}}, true); }@Override public void onDatabaseCorrupted(SQLiteDatabase sqLiteDatabase){}}); }try {database.beginTransaction(); //开启事务 if (0 != database.getVersion()&& CURRENT_VERSION > database.getVersion()){// 升级表 }else {
//创建表 }database.setVersion(CURRENT_VERSION); database.setTransactionSuccessful(); }finally {database.endTransaction(); }inited = true; }/** * 在app缓存目录中创建自定义目录 */ public static void createDirsInCache(String dirPath) {createDirs(getExternalFilesPath() + File.separator + dirPath); }/** * 获取到app缓存目录 getPath()会忽略最后的“/” */ public static String getExternalFilesPath() {return getExternalFilesDir().getPath(); }/** * 获取到app缓存目录 */ public static File getExternalFilesDir() {return mContext.getExternalFilesDir(null); }/** * 创建多级目录 */ public static boolean createDirs(String path) {if (TextUtils.isEmpty(path)){return false; }try {File f = new File(path); if (!f.exists()){return f.mkdirs(); }}catch (Exception e){e.printStackTrace(); }return false; }/** * 开启Sqlite的外键支持 */ private void openForeignKeySupport() {if (!database.isReadOnly()){database.execSQL("PRAGMA foreign_keys=ON;"); } }private boolean exist(int id, String filename) {File file = getUserDbForDebug(id, filename); return null != file && file.exists() && file.length() > 0; }private static File getUserDbForDebug(int id, String name) {if (TextUtils.isEmpty(name)){return null; }return new File(mContext.getExternalFilesDir(null), id + File.separator + name); }private static File createUserDbForDebug(int id, String name) {if (TextUtils.isEmpty(name)){return null; }File file = new File(mContext.getExternalFilesDir(null), id + File.separator + name); if (!file.exists()){try {file.createNewFile(); }catch (IOException e){file = null; }}return file; }/** * 释放数据库 */ public synchronized void releaseDatabase() {if (null != database){database.close(); database = null; inited = false; } }
需要注意的是导包的时候注意一下是tencent包下面的SQLiteDatabase
3,数据库创建好了,下面我们来创建表
推荐大家使用
public interface IBaseTable {/** * * 创建数据库表 * * @param db */ void createTable(SQLiteDatabase db); /** * 更新数据库表 * * @param db */ void updateTable(SQLiteDatabase db); }
然后需要建什么表就实现这个接口,规范子类必须实现这2个方法,以免漏掉
子类实现方法, 创建表 %s与下面的常量一一对应
@Override public void createTable(SQLiteDatabase db) {String sql = String.format(Locale.getDefault(), "CREATE TABLE IF NOT EXISTS %s (%s INTEGER ,%s TEXT)", TABLE_NAME, ID, SCHOOL_NAME); db.execSQL(sql); }@Override public void updateTable(SQLiteDatabase db) {createTable(db); }
到这里数据库创建表创建就完成了
项目地址https://download.csdn.net/download/lmy545x/10500316
andorid WCDB数据库创建(1)相关推荐
- Andorid SQLite数据库开发基础教程(3)
Andorid SQLite数据库开发基础教程(3) 数据库生成方式 数据库的生成有两种方式,一种是使用数据库管理工具生成的数据库,我们将此类数据库称为预设数据库,另一种是使用代码生成的数据库. 使用 ...
- android数据库降级_Android SQLite (二.数据库创建,升级及降级)
上篇文章简介和常用语法介绍了SQLite数据库的基本信息和一些常用的语法操作,本篇文章主要介绍Android开发过程中SQLite数据库的创建使用和常见问题处理. 一.SQLiteOpenHelper ...
- Unity + SQL数据库创建管理玩家排行榜学习教程
Unity + SQL Databases Player Management Leaderboards + More! Unity + SQL数据库玩家管理排行榜+更多! MP4 |视频:h264, ...
- R操作MySQL数据库创建表、删除表、增删改查(CRUD)
R操作MySQL数据库创建表.删除表.增删改查(CRUD) 关系数据中的数据是按照一定范式去存储的.当我们需要非常高级和复杂的Sql查询就可以使用关系数据库的数据资产.不光java和python可以容 ...
- SQLAlchemy的使用---外键ForeignKey数据库创建与连接
SQLAlchemy的使用---外键ForeignKey数据库创建与连接 # 一对多建表操作 from sqlalchemy.ext.declarative import declarative_ba ...
- python 笔记 之 sqlalchemy操作数据库-创建表
2019独角兽企业重金招聘Python工程师标准>>> ''' pip install SQLAlchemy 操作数据库-创建表 ''' import sqlalchemy''' s ...
- 数据库软件安装和数据库创建的几种方法
数据库软件安装: 图形界面安装 静默方式安装 通过复制已经存在的数据库安装 数据库创建的几种方法 图形界面创建 静默方式创建 利用已经存在的实例创建 通过手工创建 先做总结后期完善. 转载于:http ...
- Andorid SQLite数据库开发基础教程(2)
Andorid SQLite数据库开发基础教程(2) 数据库生成方式 数据库的生成有两种方式,一种是使用数据库管理工具生成的数据库,我们将此类数据库称为预设数据库,另一种是使用代码生成的数据库.
- sybase 数据导入mysql_Windows环境下Sybase12.5 数据库创建与导入数据库.docx
Windows环境下Sybase12.5 数据库创建与导入数据库?? 现在的情况是,Sybase数据库已经安装完成了,需重新创建一个叫ptms的数据库,指定一个用户名sybase/sybase,具备全 ...
最新文章
- ASP.NET Web API Selfhost宿主环境中管道、路由
- 说明使用assert和防错代码的区别
- OS - 计算机基本组成
- 企业微信 添加白名单_企业微信群为什么只能加200人?企业微信群怎么申请扩容?...
- html5输入框自动放大镜,JS 仿支付宝input输入显示数字放大镜
- hibernate框架 最新_Java 15 个框架
- 蓝屏代码大全 蓝屏全攻略
- linux dev 下放的什么意思,详解Linux系统下的/dev目录
- 规范完整APP开发制作流程
- uniapp css实现轮播图片逐渐放大效果
- 顶点计划2-2调研报告
- 你知道怎样进行芝麻动态vps速度测试吗?
- JAVA NIO文件映射、通道、流读写文件示例
- 我的世界服务器修改皮肤指令,我的世界怎么拿指令来改皮肤的 | 手游网游页游攻略大全...
- Autoware.universe 和 carla simulator 联合仿真
- SOUI总结之盒子模型
- 用户分群模型,这么建才有用
- 懂得感恩,是收获幸福的源泉。懂得感恩,你会发现原来自己周围的一切都是那样的美好
- speedoffice表格中如何插入子表
- 用cpp画出哆啦A梦