-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroid ORM 数据库的使用.java
643 lines (462 loc) · 15.9 KB
/
Android ORM 数据库的使用.java
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
ORM 数据库框架 ActiveAndroid 的特点和优势
ORM (Object Relational Mapping) 框架采用元数据来描述对象与关系映射细节。
把对象持久化到数据库中
就是利用 JAVA 的反射机制把对象和数据库记录映射关联起来
·基于 ORM 关系操作数据库
·方便配置
·几乎不需要编写任何 SQL 语句就能保存和检索 SQLite 数据库记录
·每一个操作都封装为一个类
·对象形式存取数据
/**
*/
基本用法:
·配置:
配置 AndroidManifest 的 application 的 name属性,同时在 meta-data 标签中可选的配置 db 的 name 和 version;
在自己的 Application 类中继承 ActiveAndroid 的 Application
<meta-data
android:name="AA_DB_NAME" //数据库的名称
android:value="Pickrand.db" />
<meta-data
android:name="AA_DB_VERSION" //数据库版本号
android:value="2" />
·定义实体类
实体类需继承 Model,可自定义表名和属性对应的字段名。
·进行增删改查操作
/**
*/
官方的Demo
activeandroid-3.1-beta.jar
1. AndroidManifest 添加 <eta-data />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fangyi.activeandroiddemo">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="AA_DB_NAME" //数据库的名称
android:value="Pickrand.db" />
<meta-data
android:name="AA_DB_VERSION" //数据库版本号
android:value="1" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
/**
*/
2. 定义一个全局的数据库的初始化
package com.example.fangyi.activeandroiddemo;
import android.app.Application;
import com.activeandroid.ActiveAndroid;
/**
* Created by FANGYI on 2016/4/3.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ActiveAndroid.initialize();
}
}
3. 在 AndroidManifest 添加
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fangyi.activeandroiddemo">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="AA_DB_NAME"
android:value="Pickrand.db" />
<meta-data
android:name="AA_DB_VERSION"
android:value="1" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
/**
*/
4.定义一个实体类 表
package com.example.fangyi.activeandroiddemo;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
/**
* Created by FANGYI on 2016/4/3.
*/
@Table(name = "Items") //表名
public class Item extends Model {
@Column(name = "Name") //字段名
public String name; //字段类型
@Column(name = "Category")
public Category category;
public Item() {
super();
}
public Item(String name, Category category) {
super();
this.name = name;
this.category = category;
}
}
/**
*/
5.
package com.example.fangyi.activeandroiddemo;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
/**
* Created by FANGYI on 2016/4/3.
*/
@Table(name = "Categoryes") //表名
public class Category extends Model {
@Column(name = "Name") //字段名
public String name; //字段类型
}
/**
*/
package com.example.fangyi.activeandroiddemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.activeandroid.query.Select;
import com.activeandroid.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//保存数据,修改方法
Category cate = new Category();
cate.name = "OutLook";
cate.save();
//给上添加关联
Item item = new Item();
item.name = "Lilei";
item.category = cate;
item.save();
// 删除操作
// Item item2 = Item.load(Item.class, 1);
// item2.delete();
// new Delete().from(Item.class).where("Name=?", "Lilei").execute();
// 对数据的查询方法
Log.i("info", new Select().from(Item.class).where("Name=?", "Lilei")
.orderBy("Name ASC").execute().size()
+ "");
}
}
/**
*/
数据库升级
1.新建文件夹 migrations,新建 2.sql
app/build/intermediates/assets/migrations
添加一个项目:
ALTER TABLE Categoryes ADD COLUMN Stuid INTEGER;
2.在 AndroidManifest 中 改成 2
<meta-data
android:name="AA_DB_VERSION" //数据库版本号
android:value="2" />
3.升级相应的表
package com.example.fangyi.activeandroiddemo;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
/**
* Created by FANGYI on 2016/4/3.
*/
@Table(name = "Categoryes") //表名
public class Category extends Model {
@Column(name = "Name") //字段名
public String name; //字段类型
@Column(name = "Stuid")
public Integer stuid; //添加这个
}
/**
*/
定义其他 meta-data
在 AndroidManifest 中 添加
<meta-data
android:name="AA_MODELL"
android:value="com.example.fangyi.activeandroiddemo.Item,com.example.fangyi.activeandroiddemo.Category" />
/**
*/
实现效果:
使用 ActiveAndroid 建立学生信息库,实现对学生信息库的增删改查操作
/**
*/
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fangyi.activeandroiddemo">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="AA_DB_NAME"
android:value="Stu.db" />
<meta-data
android:name="AA_DB_VERSION"
android:value="1" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
/**
*/
1.
package com.example.fangyi.activeandroiddemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.activeandroid.query.Select;
import com.example.fangyi.activeandroiddemo.adapter.StuAdapter;
import com.example.fangyi.activeandroiddemo.model.Student;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
assignViews();
}
private Button btnAdd;
private ListView lv;
private StuAdapter adapter;
private ArrayList<Student> lists = new ArrayList<Student>();
private void assignViews() {
btnAdd = (Button) findViewById(R.id.btn_add);
lv = (ListView) findViewById(R.id.lv);
adapter = new StuAdapter(this, lists);
lv.setAdapter(adapter);
// lists.addAll((Collection<? extends Student>) new Select().from(Student.class).execute());
// lists.addAll(new Select().from(Student.class).<Student>execute());
//查询数据库
List<Student> stus = new Select().from(Student.class).execute();
//上面那个查询数据库,效果是每次退出APK,再点开APK后,又会增加下面产生的8条数据
//修改学生信息
for (int i = 0; i< stus.size(); i++) {
Student student = stus.get(i);
student.name = " HanMeiMei";
student.save();
}
lists.addAll(stus);
//添加学生信息
for (int i = 0; i <8; i++) {
Student stu = new Student();
stu.name = "Lilei" + i;
stu.age = 10 + i;
stu.save();
lists.add(stu);
}
adapter.notifyDataSetChanged();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student stu = new Student();
stu.name = "Lucy";
stu.age = 12;
stu.save();
lists.add(stu);
adapter.notifyDataSetChanged();
}
});
}
}
/**
*/
package com.example.fangyi.activeandroiddemo;
import android.app.Application;
import com.activeandroid.ActiveAndroid;
/**
* Created by FANGYI on 2016/4/3.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ActiveAndroid.initialize(this);
}
}
/**
*/
package com.example.fangyi.activeandroiddemo.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.example.fangyi.activeandroiddemo.R;
import com.example.fangyi.activeandroiddemo.model.Student;
import java.util.ArrayList;
/**
* Created by FANGYI on 2016/4/3.
*/
public class StuAdapter extends BaseAdapter {
private Context context;
private ArrayList<Student> list;
public StuAdapter(Context context, ArrayList<Student> list) {
this.context = context;
this.list = list;
}
/**
* How many items are in the data set represented by this Adapter.
*
* @return Count of items.
*/
@Override
public int getCount() {
return list.size();
}
/**
* Get the data item associated with the specified position in the data set.
*
* @param position Position of the item whose data we want within the adapter's
* data set.
* @return The data at the specified position.
*/
@Override
public Object getItem(int position) {
return list.get(position);
}
/**
* Get the row id associated with the specified position in the list.
*
* @param position The position of the item within the adapter's data set whose row id we want.
* @return The id of the item at the specified position.
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* Get a View that displays the data at the specified position in the data set. You can either
* create a View manually or inflate it from an XML layout file. When the View is inflated, the
* parent View (GridView, ListView...) will apply default layout parameters unless you use
* {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
* to specify a root view and to prevent attachment to the root.
*
* @param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param convertView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriate type before using. If it is not possible to convert
* this view to display the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so that this View is
* always of the right type (see {@link #getViewTypeCount()} and
* {@link #getItemViewType(int)}).
* @param parent The parent that this view will eventually be attached to
* @return A View corresponding to the data at the specified position.
*/
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {//如果布局为空
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.item_stu, null);
holder.btn_del = (Button) convertView.findViewById(R.id.btn_del);
holder.tv = (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Student stu = list.get(position);
holder.tv.setText(stu.name + "\n" + stu.age);
holder.btn_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stu.delete();
list.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
class ViewHolder {
TextView tv;
Button btn_del;
}
}
/**
*/
package com.example.fangyi.activeandroiddemo.model;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
/**
* Created by FANGYI on 2016/4/3.
*/
@Table(name = "Students")
public class Student extends Model {
@Column(name = "Name")
public String name;
@Column(name = "Age")
public Integer age;
}
/**
*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fangyi.activeandroiddemo.MainActivity">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"/>
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
/**
*/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"/>
</LinearLayout>