Code Monkey home page Code Monkey logo

androiddatabase's Introduction

AndroidDataBase

数据库架构实现增删改查,多表分库,通过xml全局升级

多表分库针对每一个登陆用户会创建单独的数据库,总表记录了每一个分库的路径以获取对应的DataBase。

首先应该写入当前的版本号和最新的版本号到本地,可以写入file的形式。升级的时候会读取此文件,到xml中找到要升级的版本,获取sql语句来实现升级的功能。

升级步骤,

1、执行rename操作。

2、执行新建表操作。

3、讲rename表中的数据copy到新建的表中,然后删除rename表。

考虑到安全问题,可以在执行升级之前把当前数据库中的数据备份,完成升级后删除即可。

xml升级,xml定义格式如下:

<createVersion version="V003">

    <createDb name="user">
    
        <!-- 设备与软件关联信息 -->
        
        <sql_createTable>
        
            create table if not exists tb_user(
            
            name TEXT,
            
            password TEXT,
            
            loginName TEXT,
            
            lastLoginTime TEXT,
            
            _id Text
            
            );
            
        </sql_createTable>
        
    </createDb>
    
    <createDb name="login">
    
        <!-- 设备与软件关联信息 -->
        
        <sql_createTable>
        
            create table if not exists tb_photo(
            
            time TEXT,
            
            path TEXT,
            
            to_user TEXT,
            
            sendTime TEXT
            
            );
            
        </sql_createTable>
        
    </createDb>
    
</createVersion>

<updateStep

    versionFrom="V002"
    
    versionTo="V003">
    
    <updateDb name="login">
    
        <sql_before>alter table tb_photo rename to bak_tb_photo;</sql_before>
        
        <sql_after>
        
            insert into tb_photo(time,
            
            path)
            
            select time,path
            
            from bak_tb_photo;
            
        </sql_after>
        
        <sql_after>
        
            drop table if exists bak_tb_photo;
            
        </sql_after>
        
    </updateDb>
    
    <updateDb name="user">
    
        <sql_before>alter table tb_user rename to bak_tb_user;</sql_before>
        
        <sql_after>
        
            insert into tb_user(name,
            
            password,_id)
            
            select name,password,_id
            
            from bak_tb_user;
            
        </sql_after>
        
        <sql_after>
        
            drop table if exists bak_tb_user;
            
        </sql_after>
        
    </updateDb>
    
</updateStep>

androiddatabase's People

Contributors

wangchao0837 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.