本文实例讲述了Android创建或升级数据库时执行的语句,如果是创建或升级数据库,请使用带List参数的构造方法,带SQL语句的构造方法将在数据库创建或升级时执行。
具体程序代码如下:
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
import java.util.List; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class SimpleSQLiteOpenHelper extends SQLiteOpenHelper { private static final int INIT_VERSION = 1 ; /** * 创建或升级数据库时执行的语句。 */ private List<String> sqlStatementExed; /** * 如果是创建或升级数据库,请使用带List参数的构造方法。 * * @param context * to use to open or create the database * @param name * of the database file, or null for an in-memory database * @param factory * to use for creating cursor objects, or null for the default * @param version * number of the database (starting at 1); if the database is * older, onUpgrade(SQLiteDatabase, int, int) will be used to * upgrade the database; if the database is newer, * onDowngrade(SQLiteDatabase, int, int) will be used to * downgrade the database */ public SimpleSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) { super (context, name, factory, version); sqlStatementExed = null ; } /** * 带SQL语句的构造方法。此SQL语句将在数据库创建或升级时执行。 * * @param context * to use to open or create the database * @param name * of the database file, or null for an in-memory database * @param factory * to use for creating cursor objects, or null for the default * @param version * number of the database (starting at 1); if the database is * older, onUpgrade(SQLiteDatabase, int, int) will be used to * upgrade the database; if the database is newer, * onDowngrade(SQLiteDatabase, int, int) will be used to * downgrade the database * @param sqlStatementExed * 在数据库创建或升级的时候将执行的语句。 */ public SimpleSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version, List<String> sqlStatementExed) { super (context, name, factory, version); this .sqlStatementExed = sqlStatementExed; } /** * 如果是创建或升级数据库,请使用带List参数的构造方法。 * @param context * to use to open or create the database * @param name * of the database file, or null for an in-memory database * @param version * number of the database (starting at 1); if the database is * older, onUpgrade(SQLiteDatabase, int, int) will be used to * upgrade the database; if the database is newer, * onDowngrade(SQLiteDatabase, int, int) will be used to * downgrade the database */ public SimpleSQLiteOpenHelper(Context context, String name, int version) { super (context, name, null , version); sqlStatementExed = null ; } /** * 如果是创建或升级数据库,请使用带List参数的构造方法。 * @param context * to use to open or create the database * @param name * of the database file, or null for an in-memory database */ public SimpleSQLiteOpenHelper(Context context, String name) { super (context, name, null , INIT_VERSION); sqlStatementExed = null ; } /** * 如果是创建或升级数据库,请使用带List参数的构造方法。 * * @param context * to use to open or create the database * @param name * of the database file, or null for an in-memory database * @param version * number of the database (starting at 1); if the database is * older, onUpgrade(SQLiteDatabase, int, int) will be used to * upgrade the database; if the database is newer, * onDowngrade(SQLiteDatabase, int, int) will be used to * downgrade the database * @param sqlCreateStatement * 在创建或升级数据库时要执行的语句。 */ public SimpleSQLiteOpenHelper(Context context, String name, int version, List<String> sqlCreateStatement) { super (context, name, null , version); this .sqlStatementExed = sqlCreateStatement; } /** * @param context * @param name * @param sqlCreateStatement * 在创建或升级数据库时要执行的语句。 */ public SimpleSQLiteOpenHelper(Context context, String name, List<String> sqlCreateStatement) { super (context, name, null , INIT_VERSION); this .sqlStatementExed = sqlCreateStatement; } /* * (non-Javadoc) * @see * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite * .SQLiteDatabase) */ @Override @Deprecated public void onCreate(SQLiteDatabase db) { exeSqlStatementExed(db); } /* * (non-Javadoc) * @see * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite * .SQLiteDatabase, int, int) */ @Override @Deprecated public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (newVersion > oldVersion) { exeSqlStatementExed(db); } } /** * 初始化或升级数据库时执行的SQL语句。。 */ private void exeSqlStatementExed(SQLiteDatabase db) { if (sqlStatementExed != null ) { for (String statement : sqlStatementExed) { db.execSQL(statement); } } } } |
希望本文所述方法对于大家进行Android程序开发能够起到一定的帮助作用。