zoukankan      html  css  js  c++  java
  • SCRUM第二阶段第六天

    今天对数据库进行了完善

    package com.example.runapp.DataBase;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    
    import com.example.runapp.R;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DatabaseHelper extends SQLiteOpenHelper {
    
        private static final String DATABASE_NAME = "account.db";
        private static final int DATABASE_VERSION = 1;
    
        public DatabaseHelper(Context context) {
            //建库
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // 建表
            //收入类别
           String sql = "CREATE TABLE order_list(id integer primary key autoincrement," +
                    "kind text,"+
                    "describe text," +
                    "money text," +
                    "date text)";
            db.execSQL(sql);
            sql = "CREATE TABLE accountincometype(id integer primary key autoincrement,category text,icon integer)";
            db.execSQL(sql);
            //收入明细表(id,类别,金额,备注,日期时间)
            sql = "CREATE TABLE accountincome(id integer primary key autoincrement,category text,"+
                    "money double,remark text,date text)";
            db.execSQL(sql);
    
            //支出类别
            sql = "CREATE TABLE accountoutlaytype(id integer primary key autoincrement,category text,icon integer)";
            db.execSQL(sql);
            //支出明细表(id,类别,金额,备注,日期时间)
            sql = "CREATE TABLE accountoutlay(id integer primary key autoincrement,category text,"+
                    "money double,remark text,date text)";
            db.execSQL(sql);
    
            initData(db);
        }
        //自动增长的列表,不需要给值;某个字段不想给值,不出现在表名后的列表中
        private void initData(SQLiteDatabase db) {
            //收入类别
         String sql;
            sql = String.format("insert into accountincometype(category,icon) values('兼职收入',%d)", R.drawable.baby_icon);
            db.execSQL(sql);
    
    
            sql = String.format("insert into accountoutlaytype(category,icon) values('图书',%d)", R.drawable.book_icon);
            db.execSQL(sql);
    
    
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String currentDate = sdf.format(new Date());
            //收入明细
            sql = "insert into accountincome(category,money,date) values('工资',10000,'"+currentDate+"')";
            db.execSQL(sql);
            sql = "insert into accountincome(category,money,date) values('奖金',1000,'"+currentDate+"')";
            db.execSQL(sql);
    
            //支出明细
            sql = "insert into accountoutlay(category,money,date) values('交通',100,'"+currentDate+"')";
            db.execSQL(sql);
            sql = "insert into accountoutlay(category,money,date) values('食物',200,'"+currentDate+"')";
            db.execSQL(sql);
            sql = "insert into accountoutlay(category,money,date) values('图书',150,'"+currentDate+"')";
            db.execSQL(sql);
            sql = "insert into accountoutlay(category,money,date) values('电影',100,'"+currentDate+"')";
            db.execSQL(sql);
    
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
    
        }
    
    }
    View Code
    package com.example.runapp.DataBase;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    /**
     * Created by admin on 2018/6/11.
     */
    public class OpenHelper extends SQLiteOpenHelper {
    
        //建表语句(创建用户表)
        public static final String CREATE_USER = "create table user ("
                + "userid integer primary key autoincrement, "
                + "username text, "
                + "password text)";
    
        /**
         * 构造方法
         * @param context
         * @param name
         * @param factory
         * @param version
         */
        public OpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
                          int version) {
            super(context, name, factory, version);
        }
    
        /**
         * 初次创建
         * @param db
         */
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(CREATE_USER);//创建用户表
        }
    
        /**
         * 当数据库版本出现改变时
         * @param db
         * @param oldVersion
         * @param newVersion
         */
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
    
        }
    }
    View Code
    package com.example.runapp.DataBase;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    
    import com.example.runapp.DataBase.OpenHelper;
    
    public class SQLiteDB {
    
        /**  数据库名 */
        public static final String DB_NAME = "SQLite_Test";
    
        /** 数据库版本 */
        public static final int VERSION = 1;
    
        /** 数据库 */
        private static SQLiteDB sqliteDB;
    
        private SQLiteDatabase db;
    
        private SQLiteDB(Context context) {
            /** 初始化数据库 */
            OpenHelper dbHelper = new OpenHelper(context, DB_NAME, null, VERSION);
            /** 获取db */
            db = dbHelper.getWritableDatabase();
        }
    
        /**
         * 获取SqliteDB实例
         * @param context
         */
        public synchronized static SQLiteDB getInstance(Context context) {
            if (sqliteDB == null) {
                sqliteDB = new SQLiteDB(context);
            }
            return sqliteDB;
        }
    }
    View Code
  • 相关阅读:
    USACO 5.5 Twofive
    USACO 5.4 Telecowmunication
    USACO 5.4 Character Recognition
    BZOJ 1001: [BeiJing2006]狼抓兔子
    USACO 5.4 Canada Tour
    USACO 5.3 Big Barn
    USACO 5.3 Network of Schools
    USACO 5.3 Window Area
    2017辽宁冬令营-4.蚂蚁
    2017辽宁冬令营-3.斐波那契
  • 原文地址:https://www.cnblogs.com/w669399221/p/13085388.html
Copyright © 2011-2022 走看看