admin
2022-01-07 8dfe5354073b700af45d5cb472dd5f003e6f3f25
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
package com.ysvideo.zhibo.app.db;
 
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
 
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
 
    private static final String TAG = "MySQLiteOpenHelper";
 
    /**
     * 数据库名称
     */
    private static final String DATABASE_NAME = "zhibo.db";
 
    /**
     * 数据库版本
     */
    private static int DATABASE_VERSION = 3;
 
    public MySQLiteOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.i(TAG, "MySQLiteOpenHelper");
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(TAG, "onCreate");
        WatchHistoryTable.createTables(db);
    }
 
    //原来的数据库表重命名
    public static final String TEMP_SQL_CREATE_TABLE_SUBSCRIBE = "alter table " + WatchHistoryTable.TABLE_NAME + " rename to temp_" + WatchHistoryTable.TABLE_NAME;
    public static final String TEMP_SQL_ADD_LINE = "ALTER TABLE " + WatchHistoryTable.TABLE_NAME + " ADD column " + WatchHistoryTable.VIDEO_RESOURCE_ID + " TEXT";
 
    public static final String INSERT_SUBSCRIBE = "select insert into " + WatchHistoryTable.TABLE_NAME + " from temp_" + WatchHistoryTable.TABLE_NAME;
    public static final String DROP_TEMP_SUBSCRIBE = "drop table if exists temp_" + WatchHistoryTable.TABLE_NAME;
    public static final String DROP_TEMP_SUBSCRIBE1 = "drop table if exists " + WatchHistoryTable.TABLE_NAME;
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("mResult", "onUpgrade");
        for (int i = oldVersion; i <= newVersion; i++) {
            switch (i) {
                case 2: {
                    Log.i(TAG, "onUpgrade创建新表" + newVersion);
//                    MessageTable.createTables(db);
                }
                break;
                case 3: {
                    Log.i("mResult", "onUpgrade" + newVersion);
                    db.execSQL("alter table " + WatchHistoryTable.TABLE_NAME + " add column " + WatchHistoryTable.VIDEO_RESOURCE + " TEXT");
                    db.execSQL("alter table " + WatchHistoryTable.TABLE_NAME + " add column " + WatchHistoryTable.VIDEO_RESOURCE_ID + " TEXT");
                    Cursor cursor = db.query(WatchHistoryTable.TABLE_NAME, null,
                            WatchHistoryTable.VIDEO_ID + " = ? ",
                            new String[]{"1090602"}, null, null, null);
                    Log.i("mResult", "表的列数有:" + cursor.getColumnCount());
                }
                break;
                case 4: {
 
                }
                break;
                default:
                    break;
            }
        }
    }
 
 
}