rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
 */
package com.example.datalibrary.db;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
/**
 * 数据库创建工具
 */
public class DBHelper extends SQLiteOpenHelper {
    private static final String CREATE_TABLE_START_SQL = "CREATE TABLE IF NOT EXISTS ";
    private static final String CREATE_TABLE_PRIMIRY_SQL = " integer primary key autoincrement,";
 
    /**
     * 数据库名称
     */
    private static final String DB_NAME = "face.db";
    /**
     * 数据库版本
     */
    private static final int VERSION = 1;
    /**
     * 人脸特征表
     */
    // public static final String TABLE_FEATURE = "feature";
    /**
     * 用户组表
     */
    public static final String TABLE_USER_GROUP = "user_group";
    /**
     * 用户表
     */
    public static final String TABLE_USER = "user";
 
    /**
     * 识别记录表
     */
    public static final String TABLE_RECORDS = "records";
 
    public DBHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
        createTables(db);
    }
 
    @Override
    public synchronized void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            // db.execSQL("DROP TABLE IF EXISTS " + TABLE_FEATURE);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER_GROUP);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
            onCreate(db);
        }
    }
 
    public synchronized void createTables(SQLiteDatabase db) {
        if (db == null || db.isReadOnly()) {
            db = getWritableDatabase();
        }
 
        // 创建人脸特征表的SQL语句
//        StringBuffer featureSql = new StringBuffer();
//        featureSql.append(CREATE_TABLE_START_SQL).append(TABLE_FEATURE).append(" ( ");
//        featureSql.append(" _id").append(CREATE_TABLE_PRIMIRY_SQL);
//        featureSql.append(" face_token").append(" varchar(128) default \"\" ,");
//        featureSql.append(" group_id").append(" varchar(32) default \"\" ,");
//        featureSql.append(" user_id").append(" varchar(32) default \"\" ,");
//        featureSql.append(" feature").append(" blob   ,");
//        featureSql.append(" image_name").append(" varchar(64) default \"\"  ,");
//        featureSql.append(" ctime").append(" long ,");
//        featureSql.append(" update_time").append(" long )");
 
        // 创建用户组表的SQL语句
        StringBuffer groupSql = new StringBuffer();
        groupSql.append(CREATE_TABLE_START_SQL).append(TABLE_USER_GROUP).append(" ( ");
        groupSql.append(" _id").append(CREATE_TABLE_PRIMIRY_SQL);
        groupSql.append(" group_id").append(" varchar(32) default \"\" ,");
        groupSql.append(" desc").append(" varchar(32) default \"\"  ,");
        groupSql.append(" ctime").append(" long ,");
        groupSql.append(" update_time").append(" long )");
 
        // 创建用户表的SQL语句
        StringBuffer userSql = new StringBuffer();
        userSql.append(CREATE_TABLE_START_SQL).append(TABLE_USER).append(" ( ");
        userSql.append(" _id").append(CREATE_TABLE_PRIMIRY_SQL);
        userSql.append(" user_id").append(" varchar(32) default \"\"   ,");
        userSql.append(" user_name").append(" varchar(32) default \"\"   ,");
        userSql.append(" user_info").append(" varchar(32) default \"\"   ,");
        userSql.append(" group_id").append(" varchar(32) default \"\"   ,");
        userSql.append(" face_token").append(" varchar(128) default \"\" ,");
        userSql.append(" feature").append(" blob   ,");
        userSql.append(" image_name").append(" varchar(64) default \"\"  ,");
        userSql.append(" ctime").append(" long ,");
        userSql.append(" update_time").append(" long )");
 
        // 创建识别记录的SQL语句
        StringBuffer recordSql = new StringBuffer();
        recordSql.append(CREATE_TABLE_START_SQL).append(TABLE_RECORDS).append(" ( ");
        recordSql.append(" _id").append(CREATE_TABLE_PRIMIRY_SQL);
        recordSql.append(" deviceid").append(" varchar(32) default \"\"   ,");
        recordSql.append(" user_id").append(" varchar(32) default \"\"   ,");
        recordSql.append(" user_name").append(" varchar(32) default \"\"   ,");
        recordSql.append(" group_id").append(" varchar(32) default \"\"   ,");
        recordSql.append(" face_token").append(" varchar(128) default \"\" ,");
        recordSql.append(" time").append(" datetime  ,");
        recordSql.append(" records").append(" varchar(32) default \"\"   ,");
        recordSql.append(" longId").append(" varchar(32) default \"\"   ,");
        recordSql.append(" score").append(" varchar(32) default \"\"   )");
 
        try {
            db.execSQL(groupSql.toString());
            db.execSQL(userSql.toString());
            db.execSQL(recordSql.toString());
            // db.execSQL(featureSql.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}